温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB如何开发定制控件

发布时间:2021-12-03 09:21:43 来源:亿速云 阅读:132 作者:小新 栏目:编程语言

这篇文章主要介绍了VB如何开发定制控件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

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

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

表1中的代码是一个名称为RoundButton的控件,在图1中,表单上有一个RoundButton定制控件,表2是其代码。我们需要作的工作基本上就是覆盖OnPaint方法。系统向该方法传递一个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对象表示我们的定制控件的绘制区,无论在该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如何开发定制控件”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vb
AI