VB.NET制作图片按钮实现步骤一一讲解

开发 后端
VB.NET制作图片按钮的思路很简单,只要把编程思路理清,再根据我们提前设计好的步骤,一一进行,就能很好的实现这一功能。

VB.NET是目前应用比较广泛的编程语言。它在文件处理,移动设备操作,图形界面的处理方面都能够体现强大的作用。那么今天我们就一起学习一个其中的应用技巧,VB.NET制作图片按钮的实际操作方法。

VB.NET制作图片按钮思路:很简单,就是在一个picturebox控件上放置一个button控件,然后将这个button添加进picturebox上(确保先拖拽picturebox,后拖拽button),设置这个button的背景色(这个时候是相对于picturebox)为透明。

  1. Imports System.ComponentModel   
  2. Public Class picturebutton   
  3. Inherits System.Windows.Forms.UserControl   
  4. #Region " Windows 窗体设计器生成的代码 "   
  5. 'UserControl 重写 dispose 以清理组件列表。   
  6. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   
  7. If disposing Then   
  8. If Not (components Is Nothing) Then   
  9. components.Dispose()   
  10. End If   
  11. End If   
  12. MyBase.Dispose(disposing)   
  13. End Sub   
  14. 'Windows 窗体设计器所必需的   
  15. Private components As System.ComponentModel.IContainer  

注意:以下VB.NET制作图片按钮的过程是 Windows 窗体设计器所必需的

可以使用 Windows 窗体设计器修改此过程。

不要使用代码编辑器修改它。

  1. Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox   
  2. Friend WithEvents Button1 As System.Windows.Forms.Button   
  3. <System.Diagnostics.DebuggerStepThrough()> 
    Private Sub InitializeComponent()   
  4. Me.PictureBox1 = New System.Windows.Forms.PictureBox()   
  5. Me.Button1 = New System.Windows.Forms.Button()   
  6. Me.SuspendLayout()   
  7. 'PictureBox1   
  8. Me.PictureBox1.Name = "PictureBox1"   
  9. Me.PictureBox1.Size = New System.Drawing.Size(136, 40)   
  10. Me.PictureBox1.TabIndex = 0   
  11. Me.PictureBox1.TabStop = False   
  12. 'Button1   
  13. Me.Button1.Name = "Button1"   
  14. Me.Button1.TabIndex = 1   
  15. Me.Button1.Text = "Button1"   
  16. 'picturebutton   
  17. Me.Controls.AddRange(New System.Windows.Forms.Control() 
    {Me.Button1, Me.PictureBox1})   
  18. Me.Name = "picturebutton"   
  19. Me.ResumeLayout(False)   
  20. End Sub   
  21. #End Region   
  22. Public Sub New()   
  23. MyBase.New()  

该调用是 Windows 窗体设计器所必需的。

  1. InitializeComponent()   
  2. '在 InitializeComponent() 调用之后添加任何初始化   
  3. Me.Button1.Width = 100 ‘设置按钮的初始大小   
  4. Me.Button1.Height = 23   
  5. Me.Button1.BackColor = Color.Transparent ‘背景色透明   
  6. Me.Button1.ForeColor = Color.Black   
  7. Me.PictureBox1.Controls.Add(Me.Button1)   
  8. End Sub   
  9. Private m_text As String ‘设置按钮标题   
  10. Private a As Integer   
  11. 'Private m_image As Image   
  12. <Description("picturebox图片。")> _   
  13. Public Property image() As image   
  14. Get   
  15. Return Me.PictureBox1.Image   
  16. End Get   
  17. Set(ByVal Value As image)   
  18. Me.PictureBox1.Image = Value   
  19. Invalidate()   
  20. End Set   
  21. End Property   
  22. Shadows Property forecolor() As Color   
  23. Get   
  24. Return Me.Button1.ForeColor   
  25. End Get   
  26. Set(ByVal Value As Color)   
  27. Me.Button1.ForeColor = Value   
  28. Invalidate()   
  29. End Set   
  30. End Property   
  31. Shadows Sub ResetForeColor()   
  32. Me.Button1.ForeColor = SystemColors.ControlText   
  33. End Sub  

VB.NET制作图片按钮的单击事件

  1. Event BtnClick(ByVal sender As Object, ByVal e As System.EventArgs)   
  2. Private Sub Button1_Click(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Button1.Click   
  3. RaiseEvent BtnClick(Me, e)   
  4. End Sub  

控件改变大小时,需重绘控件,以使子控件排位美观

  1. Private Sub FileTextBox_Resize(ByVal sender As Object,
     ByVal e As System.EventArgs) Handles MyBase.Resize   
  2. RedrawControls()   
  3. End Sub  

子控件会自动继续容器的Font属性,所以改变容器的Font属性时也要重绘控件

  1. Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)   
  2. '让基控件更新文本框   
  3. MyBase.OnFontChanged(e)   
  4. '重绘控件   
  5. RedrawControls()   
  6. End Sub   
  7. '重绘控件   
  8. Private Sub RedrawControls()   
  9. '控件宽度   
  10. Dim width As Integer = Me.ClientRectangle.Width '获得工作区宽  

以VB.NET制作图片按钮的高度来确定控件高度

  1. Dim btnSide As Integer = Button1.Height   
  2. Dim btnwidth As Integer = Button1.Width   
  3. If Me.ClientRectangle.Height <> btnSide Then  

设置控件工作区的大小

  1. 'Me.SetClientSizeCore(btnwidth, btnSide)   
  2. Me.SetClientSizeCore(width, btnSide) 
  3. '这里使用工作区的宽是因为:按钮和picturebox可以调整宽度   
  4. '上面的语句激发了嵌套的Resize事件,因此需要立即退出,
    如果不退出,就会反复调用进入死循环   
  5. Exit Sub   
  6. End If  

调整子控件的大小

  1. 'Txt.SetBounds(0, 0, width, btnSide)   
  2. 'Btn.SetBounds(width - 19, 2, 17, btnSide - 4)   
  3. Me.PictureBox1.SetBounds(0, 0, width, btnSide)   
  4. Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage   
  5. Me.Button1.SetBounds(0, 0, width, btnSide)   
  6. End Sub   
  7. End Class  

VB.NET制作图片按钮的相关实现方法就为大家介绍到这里。

【编辑推荐】

  1. VB.NET MyClass使用方法细讲
  2. VB.NET磁盘格式化小心使用
  3. VB.NET删除文件夹实现方法介绍
  4. VB.NET安装工程具体应用方法解析
  5. VB.NET对象序列剧本概念剖析
责任编辑:曹凯 来源: wewill.cn
相关推荐

2010-01-08 18:37:08

VB.NET显示图片

2009-11-10 16:20:25

VB.NET全局热键

2009-10-23 13:22:25

VB.NET实现拖动图

2009-10-20 10:16:24

VB.NET COMB

2010-01-11 16:04:10

VB.NET使用wit

2009-10-14 09:58:43

VB.NET程序

2009-10-23 15:35:42

VB.NET实用教程

2009-10-12 13:54:22

VB.NET Data

2009-10-23 13:10:14

VB.NET List

2009-10-15 11:42:05

VB.Net赋值语句

2010-01-22 18:08:18

VB.NET与GDI结

2011-05-20 16:56:11

VB.NETGDI

2009-11-10 15:18:35

VB.NET封面

2009-10-16 09:35:24

VB.NET制作透明窗

2010-01-18 18:20:49

VB.NET使用API

2009-10-13 14:42:30

VB.NET静态成员

2009-10-14 17:21:47

VB.NET定制Win

2010-01-07 17:51:36

VB.NET实现Sin

2010-01-14 15:44:17

VB.NET数据绑定

2009-10-28 13:24:25

VB.NET文件
点赞
收藏

51CTO技术栈公众号