|
新建一个vb.net2003应用程序,然后将Form1的代码改为下面的即可:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MnuFileNew As System.Windows.Forms.MenuItem
Friend WithEvents MnuFile As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MnuFile = New System.Windows.Forms.MenuItem
Me.MnuFileNew = New System.Windows.Forms.MenuItem
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuFile})
'
'MnuFile
'
Me.MnuFile.Index = 0
Me.MnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuFileNew})
Me.MnuFile.OwnerDraw = True
Me.MnuFile.Text = "文件(&F)"
'
'MnuFileNew
'
Me.MnuFileNew.Index = 0
Me.MnuFileNew.OwnerDraw = True
Me.MnuFileNew.Text = "新建(&N)"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub MnuFileNew_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MnuFileNew.DrawItem
'通过编译、执行程序会发现留有一个16*16的位置,那是为了画图标。
'画图标的函数有: e.Graphics.DrawIcon等
Dim rfBound As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height)
Dim rfBound1 As Rectangle = New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1)
e.Graphics.FillRectangle(New SolidBrush(Color.White), rfBound)
'画背景
e.Graphics.DrawString("新建(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16, e.Bounds.Top + 3)
e.Graphics.DrawString("N", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3.5, e.Bounds.Top + 3)
'画文字
If e.State = (DrawItemState.NoAccelerator Or DrawItemState.Selected) Then
'如果选中菜单
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.FromArgb(CType(194, Byte), CType(207, Byte), CType(229, Byte))), rfBound)
'画背景
e.Graphics.DrawString("新建(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16, e.Bounds.Top + 3)
e.Graphics.DrawString("N", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3.5, e.Bounds.Top + 3)
'画文字
e.Graphics.DrawRectangle(New Pen(System.Drawing.Color.FromArgb(CType(51, Byte), CType(94, Byte), CType(168, Byte))), rfBound1)
'画边框
End If
If e.State And DrawItemState.Selected Then
'如果通过键盘控制选中菜单
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.FromArgb(CType(194, Byte), CType(207, Byte), CType(229, Byte))), rfBound)
'画背景
e.Graphics.DrawString("新建(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16, e.Bounds.Top + 3)
e.Graphics.DrawString("N", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 16 * 3.5, e.Bounds.Top + 3)
'画文字
e.Graphics.DrawRectangle(New Pen(System.Drawing.Color.FromArgb(CType(51, Byte), CType(94, Byte), CType(168, Byte))), rfBound1)
'画边框
End If
e.Graphics.Dispose()
'销毁画笔
End Sub
Private Sub MnuFileNew_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MnuFileNew.MeasureItem
e.ItemWidth = 60
'设定菜单项的宽度
e.ItemHeight = 18
'设定菜单项的高度
End Sub
Private Sub MnuFile_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MnuFile.MeasureItem
e.ItemWidth = 50
'设定菜单项的宽度
e.ItemHeight = 18
'设定菜单项的高度
End Sub
Private Sub MnuFile_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MnuFile.DrawItem
Dim rfBound As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height)
Dim rfBound1 As Rectangle = New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height)
e.Graphics.FillRectangle(New SolidBrush(Color.White), rfBound)
'画背景
e.Graphics.DrawString("文件(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 3)
e.Graphics.DrawString("F", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 1 * 35, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 45, e.Bounds.Top + 3)
'画文字
If e.State = (DrawItemState.NoAccelerator Or DrawItemState.Selected) Then
'如果单击菜单
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.FromArgb(CType(194, Byte), CType(207, Byte), CType(229, Byte))), rfBound)
'画背景
e.Graphics.DrawString("文件(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 3)
e.Graphics.DrawString("F", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 1 * 35, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 45, e.Bounds.Top + 3)
'画文字
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.Black), 1), rfBound1)
'画边框
End If
If e.State And DrawItemState.Selected Then
'如果通过键盘选中菜单
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.FromArgb(CType(238, Byte), CType(237, Byte), CType(240, Byte))), rfBound)
'画背景
e.Graphics.DrawString("文件(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 3)
e.Graphics.DrawString("F", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 1 * 35, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 45, e.Bounds.Top + 3)
'画文字
e.Graphics.DrawRectangle(New Pen(System.Drawing.Color.FromArgb(CType(134, Byte), CType(133, Byte), CType(136, Byte))), rfBound1)
'画边框
End If
If e.State And DrawItemState.HotLight Then
'如果选中菜单
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.FromArgb(CType(194, Byte), CType(207, Byte), CType(229, Byte))), rfBound)
'画背景
e.Graphics.DrawString("文件(", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 3)
e.Graphics.DrawString("F", New Font("宋体", 9, FontStyle.Underline), New SolidBrush(Color.Black), e.Bounds.Left + 1 * 35, e.Bounds.Top + 3)
e.Graphics.DrawString(")", New Font("宋体", 9), New SolidBrush(Color.Black), e.Bounds.Left + 45, e.Bounds.Top + 3)
'画文字
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.Black), 1), rfBound1)
'画边框
End If
e.Graphics.Dispose()
'销毁画笔
End Sub
End Class |
|