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 translations Article translations
Article ID: 827043 - View products that this article applies to.
Expand all | Collapse all

On This Page

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows application by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

    By default, Form1 is created.
  3. Add a ContextMenu control to Form1.
  4. 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("Paste"))
    Me.ContextMenu1.MenuItems.Add(New MenuItem("Copy"))
    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("Copy"));
    this.contextMenu1.MenuItems.Add(new MenuItem("Paste"));
    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 );
    }
  5. 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);
    }
  6. Add the NotifyIcon component to Form1.

    By default, NotifyIcon1 is created.
  7. Right-click NotifyIcon1, and then click Properties.
  8. In the Properties dialog box, set the ContextMenu property to ContextMenu1.
  9. Click the Icon property, and then click the ellipsis (...) button.
  10. 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).
  11. Click the Flags folder, and then click Open.
  12. Click one of the icons, and then click Open.
  13. On the Build menu, click Build Solution.
  14. On the Debug menu, click Start.

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

    The menu does not contain any items.

REFERENCES

For more information, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/aa984200(VS.71).aspx

Properties

Article ID: 827043 - Last Review: April 18, 2007 - Revision: 1.5
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
Keywords: 
kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbicon kbforms kbwindowsforms kbctrl kbcontmenu kbcontrol kbbug KB827043

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com