找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 564|回复: 3

vb.net2003:xp风格菜单的实现

[复制链接]
发表于 2008-12-21 06:56:07 | 显示全部楼层 |阅读模式 IP:江苏扬州
新建一个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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 11:39 , Processed in 0.273741 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表