Results 1 to 2 of 2

Thread: menu icons

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    menu icons

    Hi I try to create in VB.NET the instruction below but I can't.

    Do you have more easier instruction than this?

    =======================================

    Add Icons to Your Menus

    Menu items' OwnerDraw property and events let you leverage the power of GDI+ in dropdown and context menus. Menu icons help users associate menu items and their equivalent commands quickly. With a little creativity, you can build your own Favorites menu or emulate the Windows Start menu, for example.

    ADVERTISEMENT


    Begin by creating a Windows application and adding a MainMenu from the Visual Studio toolbox. Add a top-level menu item such as &File to it and fill it in with &New, &Open, and so on. Change the OwnerDraw property of each menu item except File to True in the Properties window. This tells the OS you'll lay out and draw your items.

    Use a comma-delimited method to specify each menu item's text label and the icon to display. For example, change the &New menu item's text to &New,New. The text before the comma will be drawn as the menu text, and the text after the comma is the item's icon name in your resource file. When your code measures and draws your menu items, the menu labels will split at the comma and be used separately.

    If you ran your application now, you'd see the File item on the menu bar, but no items would appear in the dropdown menu, and the dropdown menu would be the wrong size. You must use the MeasureItem event to specify the width and height for each item in your menu(s). Change to the code editor in Visual Studio and select the first OwnerDraw menu item (for instance, New) in the Class Name combo box. Then select the MeasureItem event in the Method Name combo box. This creates the MeasureItem event you'll use for all the menu items. Add its name to the handles list for the other OwnerDraw menu items:

    Private Sub Menu_MeasureItem(...) Handles _
    File_New.MeasureItem, File_Open.MeasureItem, _
    File_Save.MeasureItem, _
    File_SaveAs.MeasureItem, _
    File_PageSetup.MeasureItem, _
    File_Print.MeasureItem, _
    File_Exit.MeasureItem, _
    File_Sep1.MeasureItem, File_Sep2.MeasureItem

    Examine the code inside the MeasureItem event:

    Dim ThisMenuItem As MenuItem = Sender
    Dim ThisMenuItem_Strings As String() = _
    ThisMenuItem.Text.Split(",")

    Dim TextSize As SizeF = _
    e.Graphics.MeasureString(_
    ThisMenuItem_Strings(0).Replace("&", ""), _
    SystemInformation.MenuFont)

    e.ItemWidth = TextSize.Width + 30

    If ThisMenuItem.Text = "-" Then
    e.ItemHeight = 3
    Else
    e.ItemHeight = 22
    End If

    Two parameters are passed in to the event. Sender is the OwnerDraw item that needs to be measured. You'll use Sender to get this item's text label, split it, and calculate the text's width using the MeasureString method. You use the Replace method in the MeasureString call to remove any ampersands that would cause a width miscalculation. Then add a hard-coded value of 30 because the icons in this example are always 16 pixels wide with seven pixels of padding on each side.

    Next, measure the menu-item height. Menu separators have a hyphen for their text label, so this item's height is three pixels if it's a separator; icon items' height is 22 (16 pixels tall with three pixels of padding top and bottom). Put the preceding code in your MesureItem event.

    Now you can draw the menu items on screen. First you must hook the DrawItem event to each item. Choose the first OwnerDraw item and select DrawItem from the Method Name combo box, then add all the other menu items to the handles list:

    Private Sub Menu_DrawItem(...) Handles _
    File_New.DrawItem, File_Open.DrawItem, _
    File_Save.DrawItem, File_SaveAs.DrawItem, _
    File_PageSetup.DrawItem, _
    File_Print.DrawItem, File_Exit.DrawItem, _
    File_Sep1.DrawItem, File_Sep2.DrawItem

    The DrawItem event handler works this way: You draw a selection rectangle, its icon with a drop shadow, and its text if an item is selected and enabled. If an item is disabled, the selection rectangle isn't drawn and the icon and text are grayed. For enabled but not selected items, the selection rectangle isn't drawn but the icon and text are drawn normally. Menu separators are drawn as black, horizontal lines:

    On Error Resume Next

    Dim ThisMenuItem As MenuItem = sender
    Dim ThisMenuItem_Brush As Brush
    Dim ThisMenuItem_Strings As String() = _
    ThisMenuItem.Text.Split(",")
    Dim ThisMenuItem_Icon As Icon = _
    CType(res.GetObject( _
    ThisMenuItem_Strings(1)), System.Drawing.Icon)
    Dim ThisMenuItem_Rect As RectangleF = New _
    RectangleF(30, e.Bounds.Top, _
    e.Bounds.Width - 30, e.Bounds.Height)

    If ((e.State And DrawItemState.Selected) = _
    DrawItemState.Selected) And ((e.State And _
    DrawItemState.Disabled) <> _
    DrawItemState.Disabled) Then
    ' DRAW THE "SELECTED ITEM" BOX
    ThisMenuItem_Brush = New _
    SolidBrush( _
    SystemColors.InactiveCaptionText)
    e.Graphics.FillRectangle(ThisMenuItem_Brush, _
    e.Bounds)
    ThisMenuItem_Brush.Dispose()
    e.Graphics.DrawRectangle( _
    SystemPens.Highlight, e.Bounds.X, _
    e.Bounds.Y, e.Bounds.Width - 1, _
    e.Bounds.Height - 1)

    ' DRAW THE ICON WITH DROPSHADOW
    Dim img As Image = _
    ThisMenuItem_Icon.ToBitmap()
    ControlPaint.DrawImageDisabled( _
    e.Graphics, img, e.Bounds.Left + 4, _
    e.Bounds.Top + 4, _
    SystemColors.InactiveCaptionText)
    e.Graphics.DrawIcon(ThisMenuItem_Icon, _
    e.Bounds.Left + 2, e.Bounds.Top + 2)
    Else
    ' DRAW THE BACKGROUND
    e.Graphics.FillRectangle( _
    Sysöööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööööööööööööööööööööööööööö� �ööööööööööööööööööööööööö Then
    ThisMenuItem_Brush = New _
    SolidBrush(SystemColors.GrayText)
    e.Graphics.DrawString( _
    ThisMenuItem_Strings(0), _
    SystemInformation.MenuFont, _
    ThisMenuItem_Brush, ThisMenuItem_Rect, _
    sfMenuItem)
    ThisMenuItem_Brush.Dispose()
    Else
    e.Graphics.DrawString( _
    ThisMenuItem_Strings(0), _
    SystemInformation.MenuFont, _
    SystemBrushes.ControlText, _
    ThisMenuItem_Rect, sfMenuItem)
    End If

    This event draws menus that look similar to Visual Studio's menus. Selected items appear in light blue with a dark-blue border (the border varies with Windows themes), and selected icons seem to jump up and cast a shadow. The menus you can create are limited only by your imagination.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a quick example of how i do it, assuming you have an imagelist with a few images added ...
    VB Code:
    1. Private WithEvents mnuItems As MenuItem
    2.  
    3. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4. Dim x As Integer
    5.  
    6. For x = 0 To ContextMenu1.MenuItems.Count - 1
    7.     ContextMenu1.MenuItems.Item(x).OwnerDraw = True '///make sure the menuitems are set to OwnerDrawn.
    8.     AddHandler ContextMenu1.MenuItems.Item(x).MeasureItem, AddressOf mnuItems_MeasureItem
    9.     AddHandler ContextMenu1.MenuItems.Item(x).DrawItem, AddressOf mnuItems_DrawItem
    10. Next
    11. End Sub
    12. Private Sub mnuItems_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles mnuItems.MeasureItem
    13.  
    14.     If Not sender.Text = "-" Then
    15.         With e
    16.             .ItemHeight = 20
    17.             .ItemWidth = 100
    18.         End With
    19.     Else
    20.         With e
    21.             .ItemHeight = 1
    22.             .ItemWidth = 100
    23.         End With
    24.     End If
    25. End Sub
    26.  
    27. Private Sub mnuItems_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles mnuItems.DrawItem
    28. Dim rct As New Rectangle(e.Bounds.X + 20, e.Bounds.Y, e.Bounds.Width - 22, e.Bounds.Height - 1)
    29.  
    30.     Dim col As ColorTranslator
    31.     Dim x As Integer
    32.  
    33.     If Not sender.text = "-" Then
    34.     If Not e.State = 256 Then
    35.     If sender.text = "Copy" Then
    36.         x = 3
    37.     ElseIf sender.text = "Cut" Then
    38.         x = 4
    39.  
    40.     ElseIf sender.text = "Paste" Then
    41.         x = 5
    42.     ElseIf sender.text = "Open" Then
    43.         x = 8
    44.     ElseIf sender.text = "Save" Then
    45.         x = 9
    46.     Else
    47.         x = 11
    48.     End If
    49.     With e.Graphics
    50.         .FillRectangle(New SolidBrush(Color.White), e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
    51.         .DrawRectangle(Pens.Gray, e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 2)
    52.         .DrawString(sender.text, MyBase.Font, New SolidBrush(Color.Black), 22, e.Bounds.Y + 3)
    53.         .DrawImage(ImageList1.Images.Item(x), e.Bounds.X + 1, e.Bounds.Y + 1)
    54.     End With
    55.     Else
    56.         If sender.text = "Copy" Then
    57.             x = 0
    58.     ElseIf sender.text = "Cut" Then
    59.             x = 1
    60.     ElseIf sender.text = "Paste" Then
    61.             x = 2
    62.     ElseIf sender.text = "Open" Then
    63.             x = 6
    64.     ElseIf sender.text = "Save" Then
    65.             x = 7
    66.     Else
    67.             x = 10
    68.     End If
    69.     With e.Graphics
    70.         .FillRectangle(New SolidBrush(col.FromHtml("#FFFBFF")), e.Bounds.X + 20, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
    71.         .DrawString(sender.text, MyBase.Font, New SolidBrush(Color.Black), 22, e.Bounds.Y + 3)
    72.         .FillRectangle(New SolidBrush(col.FromHtml("#DEDFEF")), e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height)
    73.         .DrawImage(ImageList1.Images.Item(x), e.Bounds.X + 1, e.Bounds.Y + 1)
    74.     End With
    75.     End If
    76.     Else
    77.     With e.Graphics
    78.         .FillRectangle(New SolidBrush(Color.DarkGray), e.Bounds.X + 23, e.Bounds.Y, e.Bounds.Width - 19, e.Bounds.Height)
    79.         .FillRectangle(New SolidBrush(col.FromHtml("#DEDFEF")), e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height)
    80.     End With
    81.     End If
    82.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width