摘要:本文介绍了如何定制.NET中的Windows控件,并给出定制带图形的菜单和可以折行显示的下拉列表控件的源代码。
关键词:控件;菜单;下拉列表 .NET是Microsoft公司提供解决未来计算需要的工具.在.NET Framework中提供了许多控件,可以解决编程中用户界面的设计和实现,但在实际应用中可能需要对系统提供的控件进行改进,如.NET的菜单不能带图形,下拉列表不能折行显示。本文通过对这两个控件的改进介绍,如何定制NET的Windows控件(用VB.NET实现)。
1 .NET中设计控件
.NET将控件放在UserControl对象上,可以自己编程绘制定制控件,也可以组合现有的控件形成复合控件或对现有控件进行改进。当选择建立Windows Control Library时系统自动创建一个类,代码如下:
Public Class UserControl1 Inherits System.Windows.Forms.UserControl | UserControl1是控件名称,需要时可以更改, UserControl是所有控件的父类,如果要对现有控件进行改进,可以用现有控件类名代替UserControl。
在该类中可以用属性过程实现控件的属性,用公用过程实现控件的方法,如果要自己编程绘制控件,代码应放在OnPaint方法中。对于自画控件编程用下面两个方法:MeasureItem和DrawItem,MeasureItem事件确定发生绘图过程的矩形尺寸,DrawItem事件中进行绘制过程。该控件类编译后生成DLL文件。
2 设计带图形的菜单
.NET中,MainMenu 控件表示窗体菜单结构的容器。菜单由表示菜单结构中单个菜单命令的MenuItem对象组成,每个MenuItem可以成为应用程序的命令或其他子菜单项的父菜单,要设计带图形的菜单就要对MenuItem进行定制。下面是一个定制的MenuItem的类代码,在类中主要实现MeasureItem和DrawItem两个事件:
Public Class myMenuItem Inherits System.Windows.Forms.MenuItem Private m_picture As Image #Region … #End Region '下面代码实现Picture属性 Public Property picture() As Image Get picture = m_picture End Get Set(ByVal Value As Image) m_picture = Value End Set End Property '下面代码首先在菜单项上绘制图形,然后绘制菜单内容,图形在左边,然后是菜单内容 Private Sub myMenuItem_DrawItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem Dim r As New RectangleF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y, e.Bounds.Width, _ e.Bounds.Height) Dim r1 As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height) e.Graphics.DrawImage(m_picture, r1) e.Graphics.DrawString(Me.Text, New Font("Times New Roman", 10, FontStyle.Regular), _ Brushes.Black, r) End Sub '下面代码用来确定菜单的矩形尺寸,注意最后的语句将宽度加宽 Private Sub myMenuItem_MeasureItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.MeasureItemEventArgs) Handles MyBase.MeasureItem Dim drawSize As SizeF drawSize = e.Graphics.MeasureString(Me.Text, New Font("Times New Roman", 10, _ FontStyle.Regular)) e.ItemHeight = Drawsize.Height e.ItemWidth = Drawsize.Width + Drawsize.Height End Sub End Class |
3 设计能自动折行的下拉列表
.NET中,ComboBox控件实现下拉列表,在该控件中每项占用一行,如果有选择项的内容长度超过下拉列表的宽度,则超过部分不显示,这样就可能造成用户所见的内容不完全而无法选择的情况.我们对该控件进行改进,当一行显示不完全某项时进行折行显示,为了防止用户将折行的项误认为是两个选择项,我们将不同的选项用相互间隔的颜色区分.类代码如下:
Public Class myComboBox Inherits System.Windows.Forms.ComboBox
#Region " Windows 窗体设计器生成的代码 " … #End Region '下面代码用不同的颜色显示选项 Private Sub myComboBox_DrawItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem If e.Index < 0 Then Exit Sub Dim txtColor As SolidBrush Dim bgColor As SolidBrush Dim txtfnt As Font txtColor = New SolidBrush(Color.Black) If e.Index / 2 = CInt(e.Index / 2) Then bgColor = New SolidBrush(Color.White) Else bgColor = New SolidBrush(Color.LightYellow) End If If e.State And DrawItemState.Selected Then txtColor = New SolidBrush(Color.Blue) End If e.Graphics.FillRectangle(bgColor, e.Bounds) e.Graphics.DrawRectangle(Pens.Black, e.Bounds) Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.DrawString(Items(e.Index).ToString, Me.Font, txtColor, r) End Sub '下面代码计算每行选项需要的尺寸 Private Sub myComboBox_MeasureItem(ByVal sender As Object, ByVal e As _ System.Windows.Forms.MeasureItemEventArgs) Handles MyBase.MeasureItem Dim lsize As SizeF lsize = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, New SizeF(Me.Width, 200)) e.ItemHeight = lsize.Height e.ItemWidth = lsize.Width End Sub End Class |
4 使用定制的控件
上面两个类编译后生成DLL文件,有了DLL文件就可以在你的项目中使用上述定制控件。打开Customize Toolbox,然后单击.NET Framework Components标签,通过Browse找到DLL文件,然后选择控件名前面的复选框,两个控件就出现在你的工具箱。
使用myComboBox时,和普通控件一样使用,不过要将其DrawMode属性设置为OwnerDrawVariable,否则MeasureItem和DrawItem事件代码不执行,执行效果如图1。
 图1 可折行显示的下拉列表效果 图2 带图形的菜单效果
使用myMenuItem时要注意,在窗体上建立菜单时,系统自动创建MainMenu,而菜单项默认为MenuItem,要使用myMenuItem必须手工改写,将下列代码:
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem … Me.MenuItem1 = New System.Windows.Forms.MenuItem() |
改写为:
Friend WithEvents MenuItem1 As myMenuItem.myMenuItem … Me.MenuItem1 = New myMenuItem.myMenuItem () |
还要将所有myMenuItem的菜单项的OwnerDraw属性设置为True.否则不能得到预期效果.执行效果如图2.

5 结束语
本文以带图形的菜单和可以折行显示的下拉列表为例讲解如何定制.NET的控件。通过定制控件,可以实现更友好的用户界面。 |