Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Completely Customized Menu Procedures for .NET
|
Completely Customized Menus! by There is nothing to Explain, the Code is so well commented that I didnt Find anything to write here. If you dont understand the .NET GDI+ libraries then that is not done here, read the SDK documentation for that.
Even then you can use this Code following Instructions and understand it later at your will !
If you want some more features, please post back your comments here and I will try to add them.
#Region "Custom Menu Code"
'**************************************************************************** '**********Code template that creates Custom Menus**************** '**************************************************************************** '********** How to Create Custom Menus Using this CodeTemplate: ' Copy this Region In your Form ' Add a Main Menu and Add menus to It.Set the OwnerDrawn Property of all Those ' Menus to Ture whom you want to Customize ' Add Handles and Event Handlers after the Subs MenuDrawItem for each Owner Draw Menu: ' Private Sub MenuDrawItem(ByVal sender As System.Object, ByVal e As ' System.Windows.Forms.DrawItemEventArgs) handles FileNew.DrawItem,FileOpen.DrawItem
' Similarly perform for Sub MenuMeasureItem:
' Private Sub MenuMeasureItem(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.MeasureItemEventArgs) ' handles FileNew.MeasureItem,FileOpen.MeasureItem
' Custom Property #1:
' You can Change the Font and Size of Your Menus by changing the Following Font Properties
Private MenuFont As Font = New Font("Arial", 10)
' Custom Property #2:
'Y ou can change the BackColor of Your Menus by changing the MenuBackColor-Default is System Default
Private MenuBackColor As Color = System.Drawing.SystemColors.Menu
'Cutom Property #3:
'You can change the MenuForeColor-Default is System Default
Private MenuForeColor As Color = System.Drawing.SystemColors.MenuText
'Cutom Property #4:
'You can Change the Starting and Ending Colors for the Gradient with which Menu is selected
Private MenuSelectBackColor1 As Color = Color.White Private menuSelectBackColor2 As Color = Color.BlueViolet
'Custom Property #5
'You can Change the Distance of Picture from Menu Margin
Private picMarginDistance As Integer = 5
Private ImgIndex As Short ndex of Image in ImageList-the Name of Imagelist must be mnuImageList
Private ShortCutText As String ext of Menu Sohrtcut to be displayed if you want to
'Sub that Handles the MeasureItem Event for the Menus
'Should You need Wider or Higher Menus, just change the properties
Private Sub MenuMeasureItem(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.MeasureItemEventArgs) _ Handles MenuItem2.MeasureItem ' *** I have chosen these Height and Width after thorough Experimentation and ' it gives a professional look *******
e.ItemHeight = 20 Return the menu hieght
e.ItemWidth = 150 Return the menu width
End Sub
Private Sub MenuDrawItem(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DrawItemEventArgs) _ ' Handles MenuItem2.DrawItem
'First convert the Sender Object into MenuItem so that we can Use its properties Directly
Dim mnuItem As MenuItem = CType(sender, MenuItem)
Dim mnuCaption As String = mnuItem.Text et the menu caption
' StringFormat class that will be passed to Various Drawing Functions to Align the Items Dim strFormat As New StringFormat
' Rectangle containing the bounds of the Menu Dim rcText As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Select Case mnuCaption Case "New" 'set the picture of New
ImgIndex = 1
'set the Shortcut of New
ShortCutText = "Ctrl+N"
Case "Open" ImgIndex = 2 ShortCutText = "Ctrl+O"
'And so on for other Menus
' NOTE: YOU CAN SPECIFY ImgIndex to -1 If you ' do not want to display an Image for a particular Menu
End Select
' Fill the MenuRectangle with BackColor
e.Graphics.FillRectangle(New SolidBrush(MenuBackColor), rcText)
' Set the String Alignment to Display Text near the Margin
strFormat.Alignment = StringAlignment.Near 'Now print the Text
rcText.X = rcText.X + mnuImageList.Images(1).Width + 10 e.Graphics.DrawString(mnuCaption, MenuFont, New SolidBrush(MenuForeColor), rcText, strFormat) rcText.X = e.Bounds.X
' set the String Alignment to Far
strFormat.Alignment = StringAlignment.Far ' Now Print the Shortcut
e.Graphics.DrawString(ShortCutText, MenuFont, New SolidBrush(MenuForeColor), rcText, strFormat)
' Print the ShortCut if you want to-Default Prints Empty ShortCut
' Check whether the Menu is Selected or In Hotlight
If e.State = (DrawItemState.NoAccelerator Or DrawItemState.Selected) _ Or e.State = (DrawItemState.NoAccelerator Or DrawItemState.HotLight) Then 'Print the Menu Highlighted Properties
'First Fill the Backgroud rectangle with MenuSelectBackColor
e.Graphics.FillRectangle(New Drawing2D.LinearGradientBrush(rcText, MenuSelectBackColor1, _ menuSelectBackColor2, Drawing2D.LinearGradientMode.BackwardDiagonal), rcText)
'Now Print the String
strFormat.Alignment = StringAlignment.Near rcText.X = rcText.X + mnuImageList.Images(1).Width + 10 e.Graphics.DrawString(mnuCaption, MenuFont, New SolidBrush(Color.Black), _ rcText, strFormat) rcText.X = e.Bounds.X
'Now Print the Shortcut
strFormat.Alignment = StringAlignment.Far e.Graphics.DrawString(ShortCutText, MenuFont, New SolidBrush(Color.Black), _ rcText, strFormat)
End If
'Now Print the Image for The Menu
If ImgIndex = -1 Then Exit Sub irst check whether there is an image assosciated
Set image for the specified menu Dim img As Image = mnuImageList.Images(ImgIndex)
e.Graphics.DrawImage(img, CSng(e.Bounds.X + picMarginDistance), _ CSng((e.Bounds.Bottom + e.Bounds.Top) / 2 - img.PhysicalDimension.Height / 2))
End Sub
#End Region
|
Responses
|
| Author: malar 05 Jul 2004 | Member Level: Bronze Points : 0 | Really nice & useful article. I have a doubt in this article. i've mentioned the menu text in the properties box like this. &Open, E&xit. If i use customized menu i'm getting the menu caption as i typed in the properties. But underline should come for O & x. Is it possible to get underline instead of short-cut key?
Thanks & Regards malar
| | Author: Dan Z 02 Aug 2004 | Member Level: Bronze Points : 0 | I can't get this to work for more than one menu item in a main menu. If I add the handler to all menu items in a main menu, it only does the desired effects for the first menu item. Any help would be greatly appreciated.
| | Author: Yagnesh patel 05 Jan 2005 | Member Level: Bronze Points : 0 | When we draw the menus with images and text together, then Alt+ is not working.
|
|