摘要:MenuItem with Hotkey
因為2003 不支援 更改menuitem既font size 等等
所以就開發這個function 來 設定每張form 既 menuitem
只需簡單的pass form 進這個function
就可以了~~
Draw the own MenuItem with Hotkey in vs2003

"Menubar MenuItem of the Form"#Region "Menubar MenuItem of the Form"
Private Const FONT_NAME As String = "Microsoft Sans Serif"
Private Const FONT_SIZE As Single = 10
Private Const FONT_STYLE As FontStyle = FontStyle.Regular
Private Text_Size As SizeF 
Private FontMenu As New Font() FontMenu As New Font(FONT_NAME, FONT_SIZE, FONT_STYLE)
Private formatFar As New StringFormat
Private formatNear As New StringFormat

Public Sub CreateMenuItem() Sub CreateMenuItem(ByVal pForm As Form)
Try
formatFar.Alignment = StringAlignment.Far
formatNear.Alignment = StringAlignment.Near
Call mnuAddHandler(pForm.Menu.MenuItems)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub mnuAddHandler() Sub mnuAddHandler(ByVal pMenuItemCollection As System.Windows.Forms.MenuItem.MenuItemCollection)
Try
Dim MenuItem As System.Windows.Forms.MenuItem
For Each MenuItem In pMenuItemCollection
If (MenuItem.Text <> "-") Then
AddHandler MenuItem.MeasureItem, AddressOf mnu_MeasureItem
AddHandler MenuItem.DrawItem, AddressOf mnu_DrawItem
If MenuItem.OwnerDraw = False Then
MenuItem.OwnerDraw = True
End If
End If
If MenuItem.MenuItems.Count > 0 Then
Call mnuAddHandler(MenuItem.MenuItems)
End If
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub mnu_MeasureItem() Sub mnu_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) 'Handles MenuItem.MeasureItem
Try
' See how big the text will be.
Text_Size = e.Graphics.MeasureString(CombineAllText(sender), FontMenu)
' Set the necessary size.
e.ItemHeight = Text_Size.Height + 4
e.ItemWidth = Text_Size.Width
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Function CombineAllText() Function CombineAllText(ByVal sender As Object) As String
Dim lsSpace As String = vbTab & vbTab
If sender.Shortcut.ToString() <> "None" Then
CombineAllText = sender.text + lsSpace + sender.shortcut.ToString()
Else
CombineAllText = sender.text
End If
End Function 
Private Sub mnu_DrawItem() Sub mnu_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 'Handles MenuItem.DrawItem
Try
' See if the mouse is over the menu item.
Dim dColorStart As Double
Dim dColorDegree As Double
Dim Y As Long
Dim liBoundY As Integer = e.Bounds.Y
Dim liBoundX As Integer = e.Bounds.X
Dim liBoundHeight As Integer = e.Bounds.Height - 1
Dim liBoundWidth As Integer = e.Bounds.Width - 1
Dim lBrushEnabledTxtColor As System.Drawing.Brush = System.Drawing.Brushes.Black
Dim lBrushDisabledTxtColor As System.Drawing.Brush = System.Drawing.Brushes.DarkGray
Dim lBrushEnabledHLTxtColor As System.Drawing.Brush = System.Drawing.Brushes.AliceBlue
Dim lBrushDisabledHLTxtColor As System.Drawing.Brush = System.Drawing.Brushes.DarkGray
Dim lBrushControlColor As System.Drawing.Brush = System.Drawing.SystemBrushes.Control
'Dim MenuItem As System.Windows.Forms.MenuItem
'MenuItem.Enabled()
If sender.Enabled = False Then
If e.State And DrawItemState.Selected Then
'' The mouse is over the Enable item.
'' Draw a shaded background.
dColorStart = 255
dColorDegree = 128 / liBoundHeight
For Y = liBoundY To liBoundY + liBoundHeight
e.Graphics.DrawLine( _
New Pen(Color.FromArgb(0, 0, dColorStart)), _
liBoundX, Y, liBoundX + liBoundWidth, _
Y)
dColorStart -= dColorDegree
Next Y
' Draw the text.
sDrawText(sender, e, lBrushDisabledHLTxtColor, _
liBoundX, liBoundY, liBoundWidth)
Else
' The mouse is not over the item.
' Erase the background.
e.Graphics.FillRectangle(lBrushControlColor, e.Bounds)
sDrawText(sender, e, lBrushDisabledTxtColor, _
liBoundX, liBoundY, liBoundWidth)
End If
Else
If e.State And DrawItemState.Selected Then
'' The mouse is over the Disable item.
'' Draw a shaded background.
dColorStart = 255
dColorDegree = 128 / liBoundHeight
For Y = liBoundY To liBoundY + liBoundHeight
e.Graphics.DrawLine( _
New Pen(Color.FromArgb(0, 0, dColorStart)), _
liBoundX, Y, liBoundX + liBoundWidth, _
Y)
dColorStart -= dColorDegree
Next Y
sDrawText(sender, e, lBrushEnabledHLTxtColor, _
liBoundX, liBoundY, liBoundWidth)
Else
' The mouse is not over the item.
' Erase the background.
e.Graphics.FillRectangle(lBrushControlColor, e.Bounds)
sDrawText(sender, e, lBrushEnabledTxtColor, _
liBoundX, liBoundY, liBoundWidth)
End If
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Private Function sDrawText() Function sDrawText(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs, ByVal pBrushes As System.Drawing.Brush, ByVal piBoundX As Integer, ByVal piBoundY As Integer, ByVal piBoundWidth As Integer) As String
Dim liShorcutStartPosition As Integer = piBoundX + piBoundWidth - 50
Dim lsDisplayShorcut As String
Dim lsDisplayText As String
lsDisplayText = sender.text.Replace("&", "")
If sender.Shortcut.ToString() <> "None" Then
e.Graphics.DrawString(lsDisplayText, FontMenu, _
pBrushes, piBoundX, piBoundY + 2, formatNear)
lsDisplayShorcut = sender.shortcut.ToString()
lsDisplayShorcut = lsDisplayShorcut.Replace("Ctrl", "Ctrl+")
lsDisplayShorcut = lsDisplayShorcut.Replace("Alt", "Alt+")
lsDisplayShorcut = lsDisplayShorcut.Replace("Shift", "Shift+")
e.Graphics.DrawString(lsDisplayShorcut, FontMenu, _
pBrushes, liShorcutStartPosition, piBoundY + 2, formatNear)
Else
e.Graphics.DrawString(lsDisplayText, FontMenu, _
pBrushes, piBoundX, piBoundY + 2, formatNear)
End If
End Function
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life