詳解VB開(kāi)發(fā)定制控件
本文向大家介紹VB開(kāi)發(fā)定制控件,可能好多人還不了解VB開(kāi)發(fā)定制控件,沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
我們的定制類是通過(guò)繼承UserControl類而生成的,由于UserControl也是由繼承Control類而生成的,我們的定制類將會(huì)繼承 Control類的所有有用的方法、屬性和事件。例如,由于是繼承Control類生成的,我們的定制類會(huì)自動(dòng)地?fù)碛惺录幚沓绦颉?/P>
在VB開(kāi)發(fā)定制控件時(shí)特別重要的一個(gè)問(wèn)題是如何顯示定制控件的用戶界面。無(wú)論如何組織定制控件,需要注意的是,定制控件有時(shí)會(huì)重新顯示。因此,當(dāng)定制控件重繪時(shí),必須重新繪制用戶界面。考慮到控件每次重繪時(shí),都會(huì)調(diào)用Control類的OnPaint方法,使用新的繪制定制控件用戶界面的OnPaint方法覆蓋該方法就能保證定制控件的保持一定的外觀。
表1中的代碼是一個(gè)名稱為RoundButton的控件,在圖1中,表單上有一個(gè)RoundButton定制控件,表2是其代碼。我們需要作的工作基本上就是覆蓋OnPaint方法。系統(tǒng)向該方法傳遞一個(gè)PaintEventArgs對(duì)象,從該方法中我們可以獲得控件的 System.Drawing.Graphics對(duì)象,然后使用它的方法繪制定制控件的用戶界面。
表1:RoundButton控件
- Imports System.Windows.Forms
- Imports System.Drawing
- Public Class RoundButton : Inherits UserControl
- Public BackgroundColor As ColorColor = Color.Blue
- Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
- Dim graphics As Graphics = e.Graphics
- Dim penWidth As Integer = 4
- Dim pen As Pen = New Pen(Color.Black, 4)
- Dim fontHeight As Integer = 10
- Dim font As Font = New Font("Arial", fontHeight)
- Dim brush As SolidBrush = New SolidBrush(BackgroundColor)
- graphics.FillEllipse(brush, 0, 0, Width, Height)
- Dim textBrush As SolidBrush = New SolidBrush(Color.Black)
- graphics.DrawEllipse(pen, CInt(penWidth / 2), _
- CInt(penWidth / 2), Width - penWidth, Height - penWidth)
- graphics.DrawString(Text, font, textBrush, penWidth, _
- Height / 2 - fontHeight)
- End Sub
- End Class
表1中的代碼非常地簡(jiǎn)單,簡(jiǎn)直令人不能相信。我們的定制類只有一個(gè)方法:OnPaint。簡(jiǎn)單地說(shuō),該方法傳遞一個(gè)PaintEventArgs對(duì)象,從中我們可以獲得System.Drawing.Graphics對(duì)象。這一Graphics對(duì)象表示我們的定制控件的繪制區(qū),無(wú)論在該Graphics對(duì)象上繪制什么東西,它都會(huì)顯示為定制用戶控件的界面。
表2:RoundButton控件的調(diào)用
- Public Class MyForm
- Inherits System.Windows.Forms.Form
- #Region " Windows Form Designer generated code "
- Private WithEvents roundButton As RoundButton
- Public Sub New()
- MyBase.New()
- '這個(gè)調(diào)用是Windows Form Designer所要求的
- InitializeComponent()
- '在InitializeComponent()調(diào)用后,可以添加任意的實(shí)例化代碼
- End Sub
- '表單覆蓋,整理組件列表
- 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 Form Designer所要求的
- Private components As System.ComponentModel.IContainer
- '注意:下面的過(guò)程是Windows Form Designer所要求的,
- '可以使用Windows Form Designer對(duì)它進(jìn)行修改,
- '但不要使用軟件編輯程序進(jìn)行修改
- Private Sub InitializeComponent()
- '
- 'MyForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
- Me.ClientSize = New System.Drawing.Size(292, 273)
- Me.Name = "MyForm"
- Me.Text = "Using Custom Control"
- roundButton = New RoundButton()
- AddHandler roundButton.Click, AddressOf roundButton_Click
- roundButton.Text = "Click Here!"
- roundButton.BackgroundColor = System.Drawing.Color.White
- roundButton.Size = New System.Drawing.Size(80, 80)
- roundButton.Location = New System.Drawing.Point(100, 30)
- Me.Controls.Add(roundButton)
- End Sub
- #End Region
- Private Sub roundButton_Click(ByVal source As Object, ByVal e As EventArgs)
- MessageBox.Show("Thank you.")
- End Sub
- Public Shared Sub Main()
- Dim form As MyForm = New MyForm()
- Application.Run(form)
- End Sub
- End Class
在本篇文章中,我們介紹了VB開(kāi)發(fā)定制控件時(shí)需要理解的System.Windows.Forms名字空間中二個(gè)重要的類:Control和UserControl。另外,我們還介紹了如何通過(guò)直接擴(kuò)充UserControl類開(kāi)發(fā)自己的定制控件以及如何在 Windows表單中使用定制控件。
【編輯推薦】