Help with VB... maybe? (original) (raw)

VB and C# are a like in someways... and a lot of people know both Langs. (like me).m Below is the code I am having trouble with. It runs great, and does everything it is susspose to... but, it turns all the other Windows Menus black in visible. Can you guys try and figure it out, why it does that? Thanks.

'HERE IT THE CODE FOR THE MODULE_

#Region "XP Menus"

#Region "Imports"
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms
Imports System.Drawing.SystemColors
Imports System.Drawing.Color
#End Region

Module XPMenus

#Region "Some Needed Values"
Dim showSelectOverSidebar As Boolean = True
Dim BorderAroundRectangle As Boolean = True
Dim drawDisabledIcons As Boolean = True
Dim drawHazyIcons As Boolean = True
Dim showSelectOverDisabledItems As Boolean = False
Dim CheckMarkColor As Color
Dim gradientStyle As Drawing2D.LinearGradientMode = LinearGradientMode.Vertical
Dim UseIconShadow As Boolean = True
Dim MenuSeperaterHeight As Integer = 1
Dim m_Font As New Font("Verdana", 8)
#End Region

#Region "Draw MenuItem"
Sub DrawItem(ByVal EvDrawItems As System.Windows.Forms.DrawItemEventArgs, ByVal Mi As MenuItem, Optional ByVal m_Icon As Image = Nothing)
Mi.OwnerDraw = True
Dim mm As Menu
mm = Mi.GetMainMenu
If Not Mi.Parent Is mm Then
Dim fBr As Brush
Mi.OwnerDraw = True
If GetRealText(Mi) <> "-" Then
Dim br As Brush
Dim fDisposeBrush As Boolean
Dim rcBk As Rectangle = EvDrawItems.Bounds
If showSelectOverSidebar = True Then
rcBk.X = 0
Else
rcBk.X = 23
End If
If CBool(EvDrawItems.State And DrawItemState.Selected) Then
If Mi.Enabled = False And showSelectOverDisabledItems = False Then

Else
fBr = New SolidBrush(Color.Black)
rcBk.X = rcBk.X - 1 'bug fix
br = New LinearGradientBrush(rcBk, Color.FromArgb(255, 193, 210, 238), Color.FromArgb(255, 193, 210, 238), gradientStyle)
rcBk.X = rcBk.X + 1 'bug fix
Dim br2 = New LinearGradientBrush(rcBk, Color.FromArgb(255, 252, 252, 249), Color.FromArgb(255, 252, 252, 249), LinearGradientMode.Horizontal)
EvDrawItems.Graphics.FillRectangle(br2, rcBk)
br2.Dispose()
br2 = Nothing
If BorderAroundRectangle = True Then
If showSelectOverSidebar = True Then
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
EvDrawItems.Graphics.FillRectangle(br, rcBk.X + 2, rcBk.Y + 1, rcBk.Width - 4, rcBk.Height - 3)
EvDrawItems.Graphics.DrawRectangle(New Pen(Color.Blue), rcBk.X + 1, rcBk.Y + 1, rcBk.Width - 4, rcBk.Height - 3)
Else
EvDrawItems.Graphics.FillRectangle(br, rcBk.X + 2, rcBk.Y + 1, rcBk.Width - 26, rcBk.Height - 3)
EvDrawItems.Graphics.DrawRectangle(New Pen(Color.Blue), rcBk.X + 1, rcBk.Y + 1, rcBk.Width - 25, rcBk.Height - 3)
End If
Else
If showSelectOverSidebar = True Then
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
EvDrawItems.Graphics.FillRectangle(br, rcBk.X, rcBk.Y + 1, rcBk.Width - 2, rcBk.Height - 3)
Else
EvDrawItems.Graphics.FillRectangle(br, rcBk.X, rcBk.Y + 1, rcBk.Width - 2, rcBk.Height - 3)
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
End If
End If
If m_Icon Is Nothing Then
Else
If UseIconShadow = True Then
If showSelectOverSidebar = False Then
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
End If
Dim trans As New Bitmap(m_Icon)
EvDrawItems.Graphics.DrawImage(makeShadow(trans), CInt((23 / 2) - (m_Icon.Width / 2)) + 1, EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)) + 1)
EvDrawItems.Graphics.DrawImage(m_Icon, CInt((23 / 2) - (m_Icon.Width / 2)) - 1, EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)) - 1)
'EvDrawItems.Bounds.Left + 3
Else
EvDrawItems.Graphics.DrawImage(m_Icon, CInt((23 / 2) - (m_Icon.Width / 2)), EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)))
End If
End If
fDisposeBrush = True
End If
Else
fBr = New SolidBrush(Color.Black)
br = New LinearGradientBrush(rcBk, Color.FromArgb(255, 252, 252, 249), Color.FromArgb(255, 252, 252, 249), LinearGradientMode.Horizontal)
EvDrawItems.Graphics.FillRectangle(br, rcBk)
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
If m_Icon Is Nothing Then

Else
If Mi.Enabled = True Then
If drawHazyIcons = False Then
EvDrawItems.Graphics.DrawImage(m_Icon, CInt((23 / 2) - (m_Icon.Width / 2)), EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)))
Else
EvDrawItems.Graphics.DrawImage(CreateHazyImage(m_Icon), CInt((23 / 2) - (m_Icon.Width / 2)), EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)))
End If
Else
If drawDisabledIcons = True Then
EvDrawItems.Graphics.DrawImage(CreateDisabledImage(m_Icon, Color.FromArgb(255, 180, 177, 163), Color.FromArgb(255, 239, 237, 222)), CInt((23 / 2) - (m_Icon.Width / 2)), EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)))
Else
EvDrawItems.Graphics.DrawImage(m_Icon, CInt((23 / 2) - (m_Icon.Width / 2)), EvDrawItems.Bounds.Top + CInt((EvDrawItems.Bounds.Height / 2) - (m_Icon.Height / 2)))
End If
End If
End If
End If
If fDisposeBrush Then br.Dispose()
br = Nothing
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(60, New Single() {0})
If Mi.Enabled Then
br = fBr
Else
Br = New SolidBrush(Color.FromArgb(255, 197, 194, 184))
End If
EvDrawItems.Graphics.DrawString(GetRealText(Mi), m_Font, Br, 23 + 3, EvDrawItems.Bounds.Top + 4, sf)

br.Dispose()
br = Nothing
sf.Dispose()
sf = Nothing
Else
Dim br As Brush = New LinearGradientBrush(EvDrawItems.Bounds, Color.FromArgb(255, 252, 252, 249), Color.FromArgb(255, 252, 252, 249), LinearGradientMode.Horizontal)

EvDrawItems.Graphics.FillRectangle(br, 0, EvDrawItems.Bounds.Top, EvDrawItems.Bounds.Width, EvDrawItems.Bounds.Height)
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 239, 237, 222)), 0, EvDrawItems.Bounds.Top, 23, EvDrawItems.Bounds.Height)
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(255, 197, 194, 184)), 23 + 15, EvDrawItems.Bounds.Top, EvDrawItems.Bounds.Width - 0, EvDrawItems.Bounds.Height)
br.Dispose()
br = Nothing
End If
If Mi.Checked Then
Dim nPen As System.Drawing.Pen
Dim nBrush As Brush
If Mi.Enabled = True Then
nPen = New Pen(Color.Blue)
nBrush = New SolidBrush(Color.FromArgb(225, 180, 200, 255))
Else
nPen = New Pen(Color.Gray)
nBrush = New SolidBrush(Color.FromArgb(255, 197, 194, 184))
End If
EvDrawItems.Graphics.FillRectangle(nBrush, 2, EvDrawItems.Bounds.Top + 2, 18, 18)
EvDrawItems.Graphics.DrawRectangle(nPen, 2, EvDrawItems.Bounds.Top + 2, 18, 18)
Dim Pnts() As Point
ReDim Pnts(2)
Pnts(0) = New Point(8, EvDrawItems.Bounds.Top + 10)
Pnts(1) = New Point(10, EvDrawItems.Bounds.Top + 12)
Pnts(2) = New Point(14, EvDrawItems.Bounds.Top + 8)
If Mi.Enabled Then
EvDrawItems.Graphics.DrawLines(New Pen(Color.Black), Pnts)
Else
EvDrawItems.Graphics.DrawLines(New Pen(Color.Gray), Pnts)
End If
End If
Else
Dim br As Brush
Mi.OwnerDraw = True
If CBool(EvDrawItems.State And DrawItemState.HotLight) Then
'occurs when mouse rolls over a menubar menuitem
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(30, New Single() {0})
br = New LinearGradientBrush(EvDrawItems.Bounds, Color.FromArgb(255, 193, 210, 238), Color.FromArgb(255, 193, 210, 238), LinearGradientMode.Horizontal)
EvDrawItems.Graphics.FillRectangle(br, EvDrawItems.Bounds.X + 2, EvDrawItems.Bounds.Y + 1, EvDrawItems.Bounds.Width - 7, EvDrawItems.Bounds.Height - 2)
EvDrawItems.Graphics.DrawRectangle(Pens.Black, EvDrawItems.Bounds.X + 1, EvDrawItems.Bounds.Y + 1, EvDrawItems.Bounds.Width - 7, EvDrawItems.Bounds.Height - 2)
Dim PlotX, PlotY As Integer
PlotX = (CInt(EvDrawItems.Bounds.Width / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Width / 2))
PlotY = (CInt(EvDrawItems.Bounds.Height / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Height / 2))
EvDrawItems.Graphics.DrawString(Mi.Text, m_Font, New SolidBrush(Color.Black), EvDrawItems.Bounds.Left + PlotX + 3, EvDrawItems.Bounds.Top + PlotY, sf)
ElseIf CBool(EvDrawItems.State And DrawItemState.Selected) Then
'occurs when you select and click a menubar menuitem
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(30, New Single() {0})
br = New LinearGradientBrush(EvDrawItems.Bounds, Color.FromArgb(255, 239, 237, 222), Color.FromArgb(255, 239, 237, 222), LinearGradientMode.Horizontal)
EvDrawItems.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(70, 0, 0, 0)), EvDrawItems.Bounds.X + 2, EvDrawItems.Bounds.Y + 6, EvDrawItems.Bounds.Width - 4, EvDrawItems.Bounds.Height - 6)
EvDrawItems.Graphics.FillRectangle(br, EvDrawItems.Bounds.X + 2, EvDrawItems.Bounds.Y + 2, EvDrawItems.Bounds.Width - 7, EvDrawItems.Bounds.Height)
EvDrawItems.Graphics.DrawRectangle(New Pen(Color.DimGray), EvDrawItems.Bounds.X + 1, EvDrawItems.Bounds.Y + 1, EvDrawItems.Bounds.Width - 7, EvDrawItems.Bounds.Height)
Dim PlotX, PlotY As Integer
PlotX = (CInt(EvDrawItems.Bounds.Width / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Width / 2))
PlotY = (CInt(EvDrawItems.Bounds.Height / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Height / 2))
EvDrawItems.Graphics.DrawString(Mi.Text, m_Font, New SolidBrush(Color.Black), EvDrawItems.Bounds.Left + plotx + 3, EvDrawItems.Bounds.Top + ploty, sf)
Else
'this is the default state of the menubar menuitem
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(30, New Single() {0})
EvDrawItems.Graphics.FillRectangle(SystemBrushes.Control, EvDrawItems.Bounds.X, EvDrawItems.Bounds.Y, EvDrawItems.Bounds.Width + 1, EvDrawItems.Bounds.Height)
Dim PlotX, PlotY As Integer
PlotX = (CInt(EvDrawItems.Bounds.Width / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Width / 2))
PlotY = (CInt(EvDrawItems.Bounds.Height / 2)) - (CInt(EvDrawItems.Graphics.MeasureString(Mi.Text, m_Font).Height / 2))
EvDrawItems.Graphics.DrawString(Mi.Text, m_Font, New SolidBrush(Color.Black), EvDrawItems.Bounds.Left + plotx + 3, EvDrawItems.Bounds.Top + ploty, sf)
End If
End If
End Sub
#End Region

#Region "Measure Menuitem"
Sub MeasureItem(ByVal EvMeasureItem As System.Windows.Forms.MeasureItemEventArgs, ByVal Mi As MenuItem)
Dim sf As StringFormat = New StringFormat()
Dim mm As Menu
mm = Mi.GetMainMenu
If Not Mi.Parent Is mm Then
' predefinedMenuStyle.CutomStyleMenus
Mi.OwnerDraw = True
If GetRealText(Mi) <> "-" Then
EvMeasureItem.ItemHeight = 23
Else
EvMeasureItem.ItemHeight = MenuSeperaterHeight
End If
sf.HotkeyPrefix = HotkeyPrefix.Show
If Mi.ShowShortcut = False Then
EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), m_Font, 10000, sf).Width) + 23 + 5
Else
If Mi.Shortcut <> Shortcut.None Then
EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), m_Font, 10000, sf).Width) + 23 + 35
Else
EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), m_Font, 10000, sf).Width) + 23 + 5
End If
End If
sf.Dispose()
sf = Nothing
Else
'measures the size of the menubar menuitem for the waterstyle Menus
Mi.OwnerDraw = True
EvMeasureItem.ItemHeight = 9
EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), m_Font, 10000, sf).Width) - 2
sf.Dispose()
sf = Nothing
End If
sf = Nothing
End Sub
#End Region

#Region "Other Functions and Subs used to draw Menus (PRIVATE)"
'pass this sub routine a bitmap and it will turn that bitmap into a transparent shadow
' recommended Sizes (32 X 32) or less for best Performance
Private Function makeShadow(ByVal ShadowFor As Bitmap) As Bitmap
Dim x, y As Integer
For x = 0 To ShadowFor.Width - 1
For y = 0 To ShadowFor.Width - 1
If ShadowFor.GetPixel(x, y).ToArgb <> 0 Then
ShadowFor.SetPixel(x, y, Color.FromArgb(80, 0, 0, 0))
End If
Next
Next
Return ShadowFor
End Function

' get menu item text
Function GetRealText(ByVal Mi As MenuItem) As String
Dim s As String = Mi.Text
If Mi.ShowShortcut And Mi.Shortcut <> Shortcut.None Then
Dim k As Keys = CType(Mi.Shortcut, Keys)
s = s & Convert.ToChar(9) & TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
End If
Return s
End Function

Function CreateDisabledImage(ByRef Img As Image, ByVal Clr As Color, ByVal EmbossClr As Color) As Image
'Creates a disabled ("grayed out") image.
'
'ARGUMENTS:
'[img]
' -The image on which to create the 1 pixel border
'[Clr]
' -The color to act as the "grayed" color
'[EmbossClr]
' -The color of the embossed edges. Use same as
' Clr if you want a "flat" disabled look.
'
'RETURNS: The new image as an Image object

Dim bmp As Bitmap 'Final Image
Dim x, y As Integer 'Pixel/loop counters
bmp = New Bitmap(Img)
For x = bmp.Width - 1 To 0 Step -1
For y = bmp.Height - 1 To 0 Step -1
If bmp.GetPixel(x, y).A = 255 Then 'Not "empty" pixel
If bmp.GetPixel(x, y).GetBrightness < 0.75 Then 'It's a dark pixel
If Clr.GetHue <> EmbossClr.GetHue Then 'Are we embossing?
If x + 1 < bmp.Width - 1 Then
If bmp.GetPixel(x + 1, y).A = 0 Then
bmp.SetPixel(x + 1, y, EmbossClr)
End If
End If
If y + 1 < bmp.Height - 1 Then
If bmp.GetPixel(x, y + 1).A = 0 Then
bmp.SetPixel(x, y + 1, EmbossClr)
End If
End If
If x + 1 < bmp.Width - 1 And y + 1 < bmp.Height - 1 Then
If bmp.GetPixel(x + 1, y + 1).A = 0 Then
bmp.SetPixel(x + 1, y, EmbossClr)
End If
End If
End If
bmp.SetPixel(x, y, Clr) 'Draw dark pixel
Else 'or...
bmp.SetPixel(x, y, FromArgb(0, 0, 0, 0)) 'Draw transparent pixel
End If
End If
Next
Next
CreateDisabledImage = bmp
End Function

Function CreateHazyImage(ByRef Img As Image) As Image
'Creates a "Hazy" image as would be seen on an unselected XP menuitem.
'
'ARGUMENTS:
'[img]
' -The image from which to make hazy
'RETURNS: The new image as an Image object
Dim bmp As Bitmap 'Final Image
Dim x, y As Integer 'Pixel/loop counters
bmp = New Bitmap(Img)
Dim clr As Color
For x = bmp.Width - 1 To 0 Step -1
For y = bmp.Height - 1 To 0 Step -1
clr = bmp.GetPixel(x, y)
If clr.A = 255 Then 'Not "empty" pixel
bmp.SetPixel(x, y, FromArgb(175, clr.R, clr.G, clr.B))
End If
Next
Next
CreateHazyImage = bmp
End Function
#End Region

End Module

#End Region