VB如何開發(fā)定制控件

這篇文章主要介紹了VB如何開發(fā)定制控件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)是一家專業(yè)的成都網站建設公司,我們專注網站制作、成都網站制作、網絡營銷、企業(yè)網站建設,外鏈,一元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網理念。從網站結構的規(guī)劃UI設計到用戶體驗提高,創(chuàng)新互聯(lián)力求做到盡善盡美。

我們的定制類是通過繼承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ā)定制控件”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!

網頁題目:VB如何開發(fā)定制控件
轉載源于:http://bm7419.com/article6/ijpeig.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供全網營銷推廣、電子商務、域名注冊、小程序開發(fā)、企業(yè)建站、App開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化