VB icon

Icons in VB .NET Menus!!!!!

Email
Submitted on: 1/1/2015 2:59:00 AM
By: The New iSoftware Company! (from psc cd)  
Level: Advanced
User Rating: By 7 Users
Compatibility: VB.NET
Views: 351
 
     This is the first code on the .NET side of Planet Source Code's VB section to do this. I am proud to post a sample of Dr. GUI's sample code. I made a lot of changes to the original code like adding comments to it. It still needs some work. Well commented. Read the label of the screen shot, please. Don't forget to comment and vote! PS. It can be used in place of any menuitem. (Could not be uploaded. Here is the class.) ***New version bug (need help) email me if you wish to help! This is NOT C# code!

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Icons in VB .NET Menus!!!!!
// Description:This is the first code on the .NET side of Planet Source Code's VB section to do this. I am proud to post a sample of Dr. GUI's sample code. I made a lot of changes to the original code like adding comments to it. It still needs some work. Well commented. Read the label of the screen shot, please. Don't forget to comment and vote! PS. It can be used in place of any menuitem. (Could not be uploaded. Here is the class.) ***New version bug (need help) email me if you wish to help! This is NOT C# code!
// By: The New iSoftware Company! (from psc cd)
//
// Side Effects:Does not highlight. Sorta basic.
//**************************************

Public Class clsMenuIcons
 'Original Code by Dr. GUI and the People at Microsoft.
 'Modifications By The New iSoftware.
 'Please give credit.
#Region " Visual Basic .NET Icons on MenuItems Class File "
 Inherits MenuItem
 Public Icon As Icon
 Public Font As Font
 Sub New(ByVal Text As String, ByVal EventHandler As EventHandler, ByVal Shortcut As Shortcut, ByVal Icon As Icon)
MyBase.New(Text, EventHandler, Shortcut) 'Creates Base Menu Meth. and Prop.
Me.OwnerDraw = True 'Required. Allows the OnMeasureItem and OnDrawItem overrides.
Me.Icon = Icon 'Sets the icon.
Me.Font = SystemInformation.MenuFont 'Sets font to normal, can be overridden.
 End Sub
 Protected Overrides Sub OnMeasureItem(ByVal E As MeasureItemEventArgs)
MyBase.OnMeasureItem(E) 'Do this first. Fires code for this.
Dim sfStringFormat As New StringFormat()
sfStringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'The _ for &.
sfStringFormat.SetTabStops(50, New Single() {0}) 'Declares tab.
If Icon.Height > Font.Height Then 'Sets height.
 E.ItemHeight = Me.Icon.Height + 2 'Icon as base.
Else
 E.ItemHeight = Me.Font.Height + 2 'Font as base. 
End If
E.ItemWidth = CInt(E.Graphics.MeasureString(AppendShortcut(), Me.Font, 1000, sfStringFormat).Width) + Icon.Width + 5 ' Width Set
sfStringFormat.Dispose() 'Clean Up!
 End Sub
 Protected Overrides Sub OnDrawItem(ByVal E As DrawItemEventArgs)
MyBase.OnDrawItem(E) 'Do this first. Fires code for this.
Dim brBrush As Brush
Dim sfStringFormat As New StringFormat()
E.Graphics.FillRectangle(SystemBrushes.Menu, E.Bounds) 'Draws BG.
If Not (Icon Is Nothing) Then
 E.Graphics.DrawIcon(Icon, E.Bounds.Left + 1, E.Bounds.Top + 1) 'Draws Icon. any size works at org size.
End If
sfStringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'The _ for &.
sfStringFormat.SetTabStops(50, New Single() {0}) 'Declares tab.
brBrush = New SolidBrush(SystemColors.MenuText) 'Sets the brush color.
E.Graphics.DrawString(AppendShortcut(), Me.Font, brBrush, E.Bounds.Left + Icon.Width + 10, E.Bounds.Top, sfStringFormat) 'Draws text.
brBrush.Dispose() 'Clean Up!
sfStringFormat.Dispose()
 End Sub
 Private Function AppendShortcut() As String 'Basicaly determines to draw the HeldButtton+AnyKey thing. 
Dim sTemp As String
sTemp = Me.Text
If Me.ShowShortcut And Me.Shortcut <> Shortcut.None Then
 Dim kKeys As Keys = CType(Shortcut, Keys)
 sTemp = sTemp & Convert.ToChar(9) & System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(kKeys)
End If
Return sTemp
 End Function
#End Region
End Class


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.