Microsoft KB Archive/827043

= BUG: The ContextMenu control that is associated with the NotifyIcon component is not displayed when you set the OwnerDraw property of menu items in the ContextMenu control to true =

Article ID: 827043

Article Last Modified on 4/18/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





SYMPTOMS
You set the OwnerDraw property of menu items in the ContextMenu control to true, and then you set ContextMenu and the Icon properties of the NotifyIcon component. When you run the Microsoft Windows application and then you right-click the Notify icon in the status notification area, menu items that are in the ContextMenu control that is associated with the NotifyIcon component are not displayed.



CAUSE
This bug occurs because the NotifyIcon component receives the WM_DRAWITEM message and the WM_MEASUREITEM message, but NotifyIcon does not handle the DrawItem event and the MeasureItem event of the menu items. Therefore, menu items are not displayed in ContextMenu when you right-click the Notify icon.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. Create a new Windows application by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

By default, Form1 is created. Add a ContextMenu control to Form1.  Add the following code to the Form1_Load event handler:

Visual Basic .NET Code 'Add menu items to ContextMenu1. Me.ContextMenu1.MenuItems.Add(New MenuItem(&quot;Paste&quot;)) Me.ContextMenu1.MenuItems.Add(New MenuItem(&quot;Copy&quot;)) Dim item As MenuItem For Each item In ContextMenu1.MenuItems 'When the OwnerDraw property is set to True, the menu item is drawn by    'using the code that is specified in the event handlers of the DrawItem event and the MeasureItem event. item.OwnerDraw = True 'Associate the DrawItem event and the MeasureItem event with event handlers. AddHandler item.DrawItem, New DrawItemEventHandler(AddressOf DrawCurrentItem) AddHandler item.MeasureItem, New MeasureItemEventHandler(AddressOf MeasureCurrentItem) Next Visual C# .NET Code //Add menu items to contextMenu1. this.contextMenu1.MenuItems.Add(new MenuItem(&quot;Copy&quot;)); this.contextMenu1.MenuItems.Add(new MenuItem(&quot;Paste&quot;)); foreach(System.Windows.Forms.MenuItem Item in this.contextMenu1.MenuItems) {  //When the OwnerDraw property is set to True, the menu item is drawn by    //using the code that is specified in the event handlers of the DrawItem event and the MeasureItem event. Item.OwnerDraw = true; //Associate the DrawItem event and the MeasureItem event with event handlers. Item.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.DrawCurrentItem); Item.MeasureItem += new MeasureItemEventHandler( this.MeasureCurrentItem ); }   Add the following code after the Form1_Load event handler.

Visual Basic .NET Code Private Sub DrawCurrentItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 'Set the font for the menu item. Dim ItemFont As Font = New Font(FontFamily.GenericSansSerif, 12, FontStyle.Underline, GraphicsUnit.Pixel) 'Draw the text of the menu item with the specified font. e.Graphics.DrawString(Me.ContextMenu1.MenuItems(e.Index).Text, ItemFont, System.Drawing.Brushes.Blue, e.Bounds.X, e.Bounds.Y) End Sub Private Sub MeasureCurrentItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) 'Set the height of the menu item. e.ItemHeight = 30 'Set the width of the menu item. e.ItemWidth = 30 End Sub Visual C# .NET Code private void MeasureCurrentItem(object sender, MeasureItemEventArgs e ) {    //Set the height of the menu item. e.ItemHeight =15; //Set the width of the menu item. e.ItemWidth =30; } private void DrawCurrentItem(object sender,System.Windows.Forms.DrawItemEventArgs e) { //Set the font for the menu item. Font ItemFont = new Font(FontFamily.GenericSerif,12,FontStyle.Underline, GraphicsUnit.Pixel); //Draw the text of the menu item with the specified font. e.Graphics.DrawString(this.contextMenu1.MenuItems[e.Index].Text,ItemFont,System.Drawing.Brushes.Black,e.Bounds.X,e.Bounds.Y); }  Add the NotifyIcon component to Form1.

By default, NotifyIcon1 is created.</li> Right-click NotifyIcon1, and then click Properties.</li> In the Properties dialog box, set the ContextMenu property to ContextMenu1.</li> Click the Icon property, and then click the ellipsis (...) button.</li> In the Open dialog box, locate the Flags folder (click Program Files, click Microsoft Visual Studio .NET, click Common7, click Graphics, and then click icons).</li> Click the Flags folder, and then click Open.</li> Click one of the icons, and then click Open.</li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.

The application icon that you specified for the Icon property of NotifyIcon1 appears in the status notification area.</li> Right-click the application icon in the notification area.

The menu does not contain any items.</li></ol>

<div class="references_section">