详解VB开发定制控件
创始人
2024-06-08 12:40:31
0

本文向大家介绍VB开发定制控件,可能好多人还不了解VB开发定制控件,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

我们的定制类是通过继承UserControl类而生成的,由于UserControl也是由继承Control类而生成的,我们的定制类将会继承 Control类的所有有用的方法、属性和事件。例如,由于是继承Control类生成的,我们的定制类会自动地拥有事件处理程序。

在VB开发定制控件时特别重要的一个问题是如何显示定制控件的用户界面。无论如何组织定制控件,需要注意的是,定制控件有时会重新显示。因此,当定制控件重绘时,必须重新绘制用户界面。考虑到控件每次重绘时,都会调用Control类的OnPaint方法,使用新的绘制定制控件用户界面的OnPaint方法覆盖该方法就能保证定制控件的保持一定的外观。

表1中的代码是一个名称为RoundButton的控件,在图1中,表单上有一个RoundButton定制控件,表2是其代码。我们需要作的工作基本上就是覆盖OnPaint方法。系统向该方法传递一个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对象表示我们的定制控件的绘制区,无论在该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开发定制控件时需要理解的System.Windows.Forms名字空间中二个重要的类:Control和UserControl。另外,我们还介绍了如何通过直接扩充UserControl类开发自己的定制控件以及如何在 Windows表单中使用定制控件。

【编辑推荐】

  1. 详细分析VB Update方法
  2. 详细讲解VB开发IIS应用程序
  3. VB ConsoleProgressBar简单介绍
  4. 描述VB ConsoleProgressBar类
  5. 概述VB 2005新型控制台

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...