日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解VB開發(fā)定制控件

本文向大家介紹VB開發(fā)定制控件,可能好多人還不了解VB開發(fā)定制控件,沒有關系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站設計、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的吐魯番網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

我們的定制類是通過繼承UserControl類而生成的,由于UserControl也是由繼承Control類而生成的,我們的定制類將會繼承 Control類的所有有用的方法、屬性和事件。例如,由于是繼承Control類生成的,我們的定制類會自動地擁有事件處理程序。

在VB開發(fā)定制控件時特別重要的一個問題是如何顯示定制控件的用戶界面。無論如何組織定制控件,需要注意的是,定制控件有時會重新顯示。因此,當定制控件重繪時,必須重新繪制用戶界面??紤]到控件每次重繪時,都會調(diào)用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. Public Class RoundButton : Inherits UserControl
  4. Public BackgroundColor As ColorColor = Color.Blue
  5. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  6. Dim graphics As Graphics = e.Graphics
  7. Dim penWidth As Integer = 4
  8. Dim pen As Pen = New Pen(Color.Black, 4)
  9. Dim fontHeight As Integer = 10
  10. Dim font As Font = New Font("Arial", fontHeight)
  11. Dim brush As SolidBrush = New SolidBrush(BackgroundColor)
  12. graphics.FillEllipse(brush, 0, 0, Width, Height)
  13. Dim textBrush As SolidBrush = New SolidBrush(Color.Black)
  14. graphics.DrawEllipse(pen, CInt(penWidth / 2), _
  15. CInt(penWidth / 2), Width - penWidth, Height - penWidth)
  16. graphics.DrawString(Text, font, textBrush, penWidth, _
  17. Height / 2 - fontHeight)
  18. End Sub
  19. End Class

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

表2:RoundButton控件的調(diào)用

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

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


新聞名稱:詳解VB開發(fā)定制控件
網(wǎng)站鏈接:http://m.5511xx.com/article/dhjepcg.html