自訂控制項--圖片按鈕

來源:互聯網
上載者:User
按鈕|控制項 圖片按鈕:

思路:很簡單,就是在一個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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。