詳解VB開發(fā)定制控件
本文向大家介紹VB開發(fā)定制控件,可能好多人還不了解VB開發(fā)定制控件,沒有關系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。
我們的定制類是通過繼承UserControl類而生成的,由于UserControl也是由繼承Control類而生成的,我們的定制類將會繼承 Control類的所有有用的方法、屬性和事件。例如,由于是繼承Control類生成的,我們的定制類會自動地擁有事件處理程序。
在VB開發(fā)定制控件時特別重要的一個問題是如何顯示定制控件的用戶界面。無論如何組織定制控件,需要注意的是,定制控件有時會重新顯示。因此,當定制控件重繪時,必須重新繪制用戶界面??紤]到控件每次重繪時,都會調用Control類的OnPaint方法,使用新的繪制定制控件用戶界面的OnPaint方法覆蓋該方法就能保證定制控件的保持一定的外觀。
表1中的代碼是一個名稱為RoundButton的控件,在圖1中,表單上有一個RoundButton定制控件,表2是其代碼。我們需要作的工作基本上就是覆蓋OnPaint方法。系統(tǒng)向該方法傳遞一個PaintEventArgs對象,從該方法中我們可以獲得控件的 System.Drawing.Graphics對象,然后使用它的方法繪制定制控件的用戶界面。
表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中的代碼非常地簡單,簡直令人不能相信。我們的定制類只有一個方法:OnPaint。簡單地說,該方法傳遞一個PaintEventArgs對象,從中我們可以獲得System.Drawing.Graphics對象。這一Graphics對象表示我們的定制控件的繪制區(qū),無論在該Graphics對象上繪制什么東西,它都會顯示為定制用戶控件的界面。
表2:RoundButton控件的調用
- Public Class MyForm
- Inherits System.Windows.Forms.Form
- #Region " Windows Form Designer generated code "
- Private WithEvents roundButton As RoundButton
- Public Sub New()
- MyBase.New()
- '這個調用是Windows Form Designer所要求的
- InitializeComponent()
- '在InitializeComponent()調用后,可以添加任意的實例化代碼
- 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
- '注意:下面的過程是Windows Form Designer所要求的,
- '可以使用Windows Form Designer對它進行修改,
- '但不要使用軟件編輯程序進行修改
- 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開發(fā)定制控件時需要理解的System.Windows.Forms名字空間中二個重要的類:Control和UserControl。另外,我們還介紹了如何通過直接擴充UserControl類開發(fā)自己的定制控件以及如何在 Windows表單中使用定制控件。
【編輯推薦】