自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解VB開發(fā)定制控件

開發(fā) 后端
這里介紹在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控件

  1. Imports System.Windows.Forms  
  2. Imports System.Drawing  
  3.  
  4. Public Class RoundButton : Inherits UserControl  
  5.  
  6. Public BackgroundColor As ColorColor = Color.Blue  
  7. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)  
  8.  
  9. Dim graphics As Graphics = e.Graphics  
  10. Dim penWidth As Integer = 4 
  11. Dim pen As Pen = New Pen(Color.Black, 4)  
  12.  
  13. Dim fontHeight As Integer = 10 
  14. Dim font As Font = New Font("Arial", fontHeight)  
  15.  
  16. Dim brush As SolidBrush = New SolidBrush(BackgroundColor)  
  17. graphics.FillEllipse(brush, 0, 0, Width, Height)  
  18. Dim textBrush As SolidBrush = New SolidBrush(Color.Black)  
  19.  
  20. graphics.DrawEllipse(pen, CInt(penWidth / 2), _  
  21. CInt(penWidth / 2), Width - penWidth, Height - penWidth)  
  22.  
  23. graphics.DrawString(Text, font, textBrush, penWidth, _  
  24. Height / 2 - fontHeight)  
  25. End Sub  
  26. End Class 

表1中的代碼非常地簡單,簡直令人不能相信。我們的定制類只有一個方法:OnPaint。簡單地說,該方法傳遞一個PaintEventArgs對象,從中我們可以獲得System.Drawing.Graphics對象。這一Graphics對象表示我們的定制控件的繪制區(qū),無論在該Graphics對象上繪制什么東西,它都會顯示為定制用戶控件的界面。

表2:RoundButton控件的調用

  1. Public Class MyForm  
  2. Inherits System.Windows.Forms.Form  
  3.  
  4. #Region " Windows Form Designer generated code "  
  5.  
  6. Private WithEvents roundButton As RoundButton  
  7. Public Sub New()  
  8. MyBase.New()  
  9.  
  10. '這個調用是Windows Form Designer所要求的  
  11. InitializeComponent()  
  12.  
  13. '在InitializeComponent()調用后,可以添加任意的實例化代碼  
  14.  
  15. End Sub  
  16.  
  17. '表單覆蓋,整理組件列表  
  18. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)  
  19. If disposing Then  
  20. If Not (components Is Nothing) Then  
  21. components.Dispose()  
  22. End If  
  23. End If  
  24. MyBase.Dispose(disposing)  
  25. End Sub  
  26.  
  27. 'Windows Form Designer所要求的  
  28. Private components As System.ComponentModel.IContainer  
  29.  
  30. '注意:下面的過程是Windows Form Designer所要求的,  
  31. '可以使用Windows Form Designer對它進行修改,  
  32. '但不要使用軟件編輯程序進行修改  
  33. Private Sub InitializeComponent()  
  34. '  
  35. 'MyForm  
  36. '  
  37. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)  
  38. Me.ClientSize = New System.Drawing.Size(292, 273)  
  39. Me.Name = "MyForm" 
  40. Me.Text = "Using Custom Control" 
  41.  
  42. roundButton = New RoundButton()  
  43. AddHandler roundButton.Click, AddressOf roundButton_Click  
  44. roundButton.Text = "Click Here!" 
  45. roundButton.BackgroundColor = System.Drawing.Color.White  
  46. roundButton.Size = New System.Drawing.Size(80, 80)  
  47. roundButton.Location = New System.Drawing.Point(100, 30)  
  48. Me.Controls.Add(roundButton)  
  49.  
  50. End Sub  
  51.  
  52. #End Region  
  53.  
  54. Private Sub roundButton_Click(ByVal source As Object, ByVal e As EventArgs)  
  55. MessageBox.Show("Thank you.")  
  56. End Sub  
  57. Public Shared Sub Main()  
  58. Dim form As MyForm = New MyForm()  
  59. Application.Run(form)  
  60. End Sub  
  61.  
  62. End Class 

在本篇文章中,我們介紹了VB開發(fā)定制控件時需要理解的System.Windows.Forms名字空間中二個重要的類:Control和UserControl。另外,我們還介紹了如何通過直接擴充UserControl類開發(fā)自己的定制控件以及如何在 Windows表單中使用定制控件。

【編輯推薦】

  1. 詳細分析VB Update方法
  2. 詳細講解VB開發(fā)IIS應用程序
  3. VB ConsoleProgressBar簡單介紹
  4. 描述VB ConsoleProgressBar類
  5. 概述VB 2005新型控制臺
責任編輯:佚名 來源: IT168
相關推薦

2009-10-14 17:21:47

VB.NET定制Win

2010-01-19 10:12:39

VB.NET Butt

2009-10-27 18:06:41

VB.NET開發(fā)控件

2009-10-10 16:44:52

VB.NET開發(fā)控件

2009-08-04 10:43:59

ASP.NET控件開發(fā)

2009-12-30 13:30:16

Silverlight

2010-01-13 10:53:24

VB.NET控件

2009-10-14 10:19:57

VB.NET Doma

2009-10-14 16:04:43

VB.NET Noti

2009-10-20 10:16:24

VB.NET COMB

2009-10-23 13:14:38

2022-03-13 09:12:00

瀏覽器webCSS 樣

2009-10-14 11:27:20

VB.NET Grou

2009-10-23 13:10:14

VB.NET List

2009-10-16 14:31:48

VB.NET Noti

2009-10-12 15:02:51

VB.NET動態(tài)控件

2009-08-11 15:46:15

C#日歷控件

2009-12-30 11:16:36

Silverlight

2009-10-29 10:45:01

VB.NET設置控件

2009-10-16 13:19:04

VB.NET Data
點贊
收藏

51CTO技術棧公眾號