浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

自定义控件--图片按钮

 

图片按钮:

思路:很简单,就是在一个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

 

    '注意:以下过程是 Windows 窗体设计器所必需的

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

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

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

    Friend WithEvents Button1 As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.PictureBox1 = New System.Windows.Forms.PictureBox()

        Me.Button1 = New System.Windows.Forms.Button()

        Me.SuspendLayout()

        '

        'PictureBox1

        '

        Me.PictureBox1.Name = "PictureBox1"

        Me.PictureBox1.Size = New System.Drawing.Size(136, 40)

        Me.PictureBox1.TabIndex = 0

        Me.PictureBox1.TabStop = False

        '

        'Button1

        '

        Me.Button1.Name = "Button1"

        Me.Button1.TabIndex = 1

        Me.Button1.Text = "Button1"

        '

        'picturebutton

        '

        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox1})

        Me.Name = "picturebutton"

        Me.ResumeLayout(False)

 

    End Sub

 

#End Region

    Public Sub New()

        MyBase.New()

 

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

        InitializeComponent()

 

        ' InitializeComponent() 调用之后添加任何初始化

        Me.Button1.Width = 100 ‘设置按钮的初始大小

        Me.Button1.Height = 23

      

 

        Me.Button1.BackColor = Color.Transparent 背景色透明

        Me.Button1.ForeColor = Color.Black

        Me.PictureBox1.Controls.Add(Me.Button1)

    End Sub

    Private m_text As String ‘设置按钮标题

    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

    '////

    '按钮的单击事件

    Event BtnClick(ByVal sender As Object, ByVal e As System.EventArgs)

 

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        RaiseEvent BtnClick(Me, e)

    End Sub

    '////

 

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

    Private Sub FileTextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        RedrawControls()

    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 '获得工作区宽

        '以按钮的高度来确定控件高度

        Dim btnSide As Integer = Button1.Height

        Dim btnwidth As Integer = Button1.Width

        If Me.ClientRectangle.Height <> btnSide Then

            '设置控件工作区的大小

            'Me.SetClientSizeCore(btnwidth, btnSide)

            Me.SetClientSizeCore(width, btnSide) '这里使用工作区的宽是因为:按钮和picturebox可以调整宽度

            '上面的语句激发了嵌套的Resize事件,因此需要立即退出,如果不退出,就会反复调用进入死循环

            Exit Sub

        End If

 

        '调整子控件的大小

        'Txt.SetBounds(0, 0, width, btnSide)

        'Btn.SetBounds(width - 19, 2, 17, btnSide - 4)

 

        Me.PictureBox1.SetBounds(0, 0, width, btnSide)

        Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

        Me.Button1.SetBounds(0, 0, width, btnSide)

 

    End Sub

End Class




相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...