VB.NET中怎么制作一个图片按钮

发布时间:2021-08-06 15:52:11 作者:Leah
来源:亿速云 阅读:159

VB.NET中怎么制作一个图片按钮,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

Imports System.ComponentModel   Public Class picturebutton   Inherits System.Windows.Forms.UserControl   #Region " Windows 窗体设计器生成的代码 "   'UserControl 重写 dispose 以清理组件列表。   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   If disposing Then   If Not (components Is Nothing) Then   components.Dispose()   End If   End If   MyBase.Dispose(disposing)   End Sub   'Windows 窗体设计器所必需的   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 窗体设计器所必需的。

InitializeComponent()   '在 InitializeComponent() 调用之后添加任何初始化   Me.Button1.Width = 100 &lsquo;设置按钮的初始大小   Me.Button1.Height = 23   Me.Button1.BackColor = Color.Transparent &lsquo;背景色透明   Me.Button1.ForeColor = Color.Black   Me.PictureBox1.Controls.Add(Me.Button1)   End Sub   Private m_text As String &lsquo;设置按钮标题   Private a As Integer   'Private m_image As Image   <Description("picturebox图片。")> _   Public Property image() As image   Get   Return Me.PictureBox1.Image   End Get   Set(ByVal Value As image)   Me.PictureBox1.Image = Value   Invalidate()   End Set   End Property   Shadows Property forecolor() As Color   Get   Return Me.Button1.ForeColor   End Get   Set(ByVal Value As Color)   Me.Button1.ForeColor = Value   Invalidate()   End Set   End Property   Shadows Sub ResetForeColor()   Me.Button1.ForeColor = SystemColors.ControlText   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属性时也要重绘控件

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

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

Dim btnSide As Integer = Button1.Height   Dim btnwidth As Integer = Button1.Width   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如何制作图片按钮效果讲解
  2. html按钮如何制作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vb.net

上一篇:C++中怎么读取CSV文件

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》