VB.NET制作图片按钮实现步骤一一讲解
创始人
2024-06-21 13:20:22
0

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.  
    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.  _   
  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对象序列剧本概念剖析

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...