详解VB开发定制控件

本文向大家介绍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. 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对象表示我们的定制控件的绘制区,无论在该Graphics对象上绘制什么东西,它都会显示为定制用户控件的界面。

表2:RoundButton控件的调用

 
 
 
  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. '这个调用是Windows Form Designer所要求的
  8. InitializeComponent()
  9. '在InitializeComponent()调用后,可以添加任意的实例化代码
  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开发定制控件时需要理解的System.Windows.Forms名字空间中二个重要的类:Control和UserControl。另外,我们还介绍了如何通过直接扩充UserControl类开发自己的定制控件以及如何在 Windows表单中使用定制控件。

分享标题:详解VB开发定制控件
标题URL:http://www.shufengxianlan.com/qtweb/news1/137301.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联