|
-
Jul 2nd, 2002, 03:10 PM
#1
Thread Starter
Member
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
-
Jul 2nd, 2002, 04:05 PM
#2
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:
'make contextmenu object
Dim mnuContext As New ContextMenu()
'add a menuitem i already have in my normal menu to the context menu
mnuContext.MenuItems.Add(mnuHelpAbout)
'assign it to the contextmenu prop of a control, in your case the notifyicon control
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.
-
Jul 2nd, 2002, 04:12 PM
#3
Thread Starter
Member
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
-
Jul 2nd, 2002, 04:35 PM
#4
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.
-
Jul 2nd, 2002, 04:59 PM
#5
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:
Dim mnuContext As New ContextMenu()
mnuContext.MenuItems.Add(mnuHelpAbout[b].CloneMenu[/b])
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.
-
Jul 2nd, 2002, 05:53 PM
#6
Thread Starter
Member
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
-
Jul 2nd, 2002, 06:23 PM
#7
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.
-
Jul 3rd, 2002, 05:22 AM
#8
Thread Starter
Member
would it be possible to call the drawitem sub on another event, like the notifyicon popup event??
-
Jul 3rd, 2002, 10:47 AM
#9
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.
-
Jul 3rd, 2002, 11:30 AM
#10
Thread Starter
Member
-
Feb 12th, 2003, 09:41 AM
#11
Hyperactive Member
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 
-
Feb 12th, 2003, 01:28 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|