Results 1 to 12 of 12

Thread: icons in context menu??

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    39

    Question icons in context menu??

    i have seen the code to get icons in the regular menus, but does anyone know who u can get them in the context menu?? more spcificly a context menu from a tray icon

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Search for this topic in the help files:
    Adding Context Menus to Windows Forms

    Basically the NotifyIcon control and most other controls have a contextmenu property and you can build a contextmenu object and assign it to that. It may look something like this:
    VB Code:
    1. 'make contextmenu object
    2.         Dim mnuContext As New ContextMenu()
    3.         'add a menuitem i already have in my normal menu to the context menu
    4.         mnuContext.MenuItems.Add(mnuHelpAbout)
    5.         'assign it to the contextmenu prop of a control, in your case the notifyicon control
    6.         txtFile.ContextMenu = mnuContext

    Since you assign normal menuitems to the context then just use the code you already have to add the icon to the menuitem and it'll show up in the context menu as well. I imagine they don't HAVE to come from the menu in use, you could just build them in code.
    Last edited by Edneeis; Jul 2nd, 2002 at 04:09 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    39

    Unhappy

    i think u missed the point of my question, i have a full tray con and context menu thing created at run time etc but i want to add icons beside each thing, i have seen eamples of how to do this with normal menus, i tried this and they just turn up blank, im assumin this is because of how it gets the coordinats to draw it and since i dont really use a form the coordinates are very far off.

    does any one know how to get it to work properly with a tray icon & context menu setup??

    the code i got from this forum:

    Code:
    Private Function AppendShortcut(ByVal mItem As MenuItem) As String
            Dim s As String
            s = mItem.Text
            ' Check to see if you have a shortcut If so, append it to your existing text  
            If mItem.ShowShortcut And mItem.Shortcut <> Shortcut.None Then
                Dim k As Keys = CType(mItem.Shortcut, Keys)
                s = s & Convert.ToChar(9) & _
                    System.ComponentModel.TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
            End If
            Return s
        End Function
    
    Private Sub DrawIMenu(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem2.DrawItem
            e.DrawBackground()
            Dim G As Graphics = e.Graphics
            Dim X As Integer = 25 'Offset text past icon 
            Dim Y As Integer = 1 '9 'Center text in menuitem 
            Dim brhText As New SolidBrush(SystemColors.MenuText)
            Dim brhBack As New SolidBrush(SystemColors.Menu)
            Dim sf As StringFormat
            sf = New StringFormat()
            sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
    
            'test for selected
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                brhText.Color = SystemColors.HighlightText
                brhBack.Color = SystemColors.Highlight
            End If
    
            G.FillRectangle(brhBack, e.Bounds)
            G.DrawRectangle(System.Drawing.Pens.Transparent, e.Bounds)
            G.DrawImage(icoList.Images(1), 3, 1)
            G.DrawString(AppendShortcut(sender), SystemInformation.MenuFont, brhText, e.Bounds.X + X, e.Bounds.Y + Y, sf)
            brhText.Dispose()
            brhBack.Dispose()
            sf.Dispose()
        End Sub

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No I understood what you were asking, it worked for me. I'll attach my project for an example. That is almost the same draw code I am using maybe it just needs a little tweaking.

    This project is a bit messing but it's just my testbed for learning.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    One small change, if you are using a menu item that is also in the main menu then put a clone in the contextmenu because it seems the same menuitem can't be in both places at the sametime.

    VB Code:
    1. Dim mnuContext As New ContextMenu()
    2.         mnuContext.MenuItems.Add(mnuHelpAbout[b].CloneMenu[/b])
    3.         txtFile.ContextMenu = mnuContext

    And you may have noticed that the AppendShortcut code doesn't really work, or at least it doesn't right justify the shortcut. If you find a way to fix that please let me know, thanks.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    39

    Unhappy

    i had a look at ur code and could not see much difference that would produce different results, so i added a "notify icon" to ur progect and added the following line in the load part

    under
    txtFile.ContextMenu = mnuContext

    i added
    NotifyIcon1.ContextMenu = mnuContext

    the menu on the textbox will be fine, but the one on the tray icon will be blank

    looks like its a problem with the tray icon, it must not give it the coordinates or something

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you put a debug.writeline in the DrawIMenu it never gets fired when you click the SysTray one, so I don't think it's the positioning, I think the DrawItem of the menuitem never gets fired. Wierd.

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    39

    Question

    would it be possible to call the drawitem sub on another event, like the notifyicon popup event??

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The problem is that it's nested though so you'd have to call the drawitem event of the menuitem when the contextmenu finishes drawing or something along that lines.

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    39

    Unhappy

    i tried to inherit the notify icon to add the call in to it, but it wont let u inherit it


  11. #11
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336
    Originally posted by Edneeis
    And you may have noticed that the AppendShortcut code doesn't really work, or at least it doesn't right justify the shortcut. If you find a way to fix that please let me know, thanks.
    Did you ever get this sorted??

    The AppendShortcut function works fine, however its the DrawString method in DrawIMenu that causes the problem.

    The last parameter specified in this method is the stringformat which you've set to read the appropriate shortcut keys - but you've already read these in the append shortcut.

    The string format overrides whatever string is entered as the first parameter (the AppendShortcut call). Remove the last parameter (sf) and you achieve what you intended to do...

    This causes another problem... appending 1 tab to the menutext only works if your menutext is roughly the same length as all other menutexts in the menu... you'll see what I mean.

    Please ignore if this has already been resolved elsewhere - but I couldn't find the solution.

    Anyone else help to finish this?
    Another satisfied customer

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well I didn't fix that probably BUT I moved the code to handle the Icons, Drawing and IconIndexes into an extender component. It works a lot like the ToolTip control so you can add it to the form at designtime and it will add 2 properties to each menuItem, IconIndex (the index number of the image in the imagelist, -1 for none), and ImageList (the imagelist to get the icon from, none for non-owner draw items). It will also take care of the drawing code so any drawing code we change should be done in there. The only downside is to get the designtime action you need to add the iconMenuExtender component to the toolbox which means it should be in a seperate dll from the project. This really isn't a big deal but if it was for someone else you can just copy the class to your project and set the props at runtime.

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