BUG: The DrawItemEventArgs object returns incorrect ForeColor and BackColor properties for owner-draw menu items (834530)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0

SYMPTOMS

When you owner-draw menu items, the color of the menu background and of the menu text is not correct. Although you defined a color scheme for the Menu item in the Display Properties dialog box, the color scheme for the Window item is used.

CAUSE

This problem occurs because the DrawItemEventArgs object does not return the correct information for the ForeColor property and the BackColor property for owner-draw menu items.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section of this article.

WORKAROUND

To work around this problem, construct a new DrawItemEventArgs object that is based on the e parameter in the DrawItem event handler:
private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
	DrawItemEventArgs dea=new DrawItemEventArgs(e.Graphics,e.Font,e.Bounds,e.Index,e.State, SystemColors.MenuText,SystemColors.Menu);
	dea.DrawBackground ();
	ControlPaint.DrawMenuGlyph(dea.Graphics, 2, 2, 14, 14, MenuGlyph.Checkmark);
	SolidBrush sb = new SolidBrush (dea.ForeColor);
	dea.Graphics.DrawString ("Open", dea.Font, sb,dea.Bounds.Left + 18, dea.Bounds.Top + 2);
	sb.Dispose ();
}
Note Although this workaround fixes the background color and the foreground color of the menu item, the checkmark is still the wrong color.

MORE INFORMATION

Steps to reproduce the behavior

Follow these steps to reproduce the behavior on Microsoft Windows XP.

Note The user interface (UI) in the Display Properties dialog box may be different on other versions of Windows.
  1. In the Display Properties dialog box, set the system colors on the Appearance tab as follows:
    • For Windows and buttons, click Windows Classic style.
    • For Color scheme, click Spruce.
  2. Click the Advanced button, and then set the colors for the items as follows:
    • Click Window in the Item list, click Yellow for Color1, and then click Red for the Font color.
    • Click Menu in the Item list, click Green for Color1, and then click Pink for the Font color.
  3. Start Visual Studio .NET, and then create a Windows application by using Visual C# .NET.
  4. Replace the code in Form1.cs with the following code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Text;
    
    namespace WindowsApplication1
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.MainMenu mainMenu1;
    		private System.Windows.Forms.MenuItem menuItem1;
    		private System.Windows.Forms.MenuItem menuItem2;
    		private System.Windows.Forms.TextBox textBox1;
    		private System.Windows.Forms.TextBox textBox2;
    		private System.Windows.Forms.MenuItem menuItem3;
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.mainMenu1 = new System.Windows.Forms.MainMenu();
    			this.menuItem1 = new System.Windows.Forms.MenuItem();
    			this.menuItem2 = new System.Windows.Forms.MenuItem();
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.textBox2 = new System.Windows.Forms.TextBox();
    			this.menuItem3 = new System.Windows.Forms.MenuItem();
    			this.SuspendLayout();
    			// 
    			// mainMenu1
    			// 
    			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    																					  this.menuItem1});
    			// 
    			// menuItem1
    			// 
    			this.menuItem1.Index = 0;
    			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    																					  this.menuItem2,
    																					  this.menuItem3});
    			this.menuItem1.Text = "File";
    			// 
    			// menuItem2
    			// 
    			this.menuItem2.Index = 0;
    			this.menuItem2.OwnerDraw = true;
    			this.menuItem2.Text = "Open";
    			this.menuItem2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem);
    			this.menuItem2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem);
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(8, 136);
    			this.textBox1.Name = "textBox1";
    			this.textBox1.Size = new System.Drawing.Size(272, 20);
    			this.textBox1.TabIndex = 0;
    			this.textBox1.Text = "";
    			// 
    			// textBox2
    			// 
    			this.textBox2.Location = new System.Drawing.Point(8, 160);
    			this.textBox2.Name = "textBox2";
    			this.textBox2.Size = new System.Drawing.Size(272, 20);
    			this.textBox2.TabIndex = 1;
    			this.textBox2.Text = "";
    			// 
    			// menuItem3
    			// 
    			this.menuItem3.Checked = true;
    			this.menuItem3.Index = 1;
    			this.menuItem3.Text = "Close";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(284, 185);
    			this.Controls.AddRange(new System.Windows.Forms.Control[] {
    																		  this.textBox2,
    																		  this.textBox1});
    			this.Menu = this.mainMenu1;
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    		{
    			textBox1.Text = "BackColor: " + ColorToString(e.BackColor);
    			textBox2.Text = "ForeColor: " + ColorToString(e.ForeColor);
    			e.DrawBackground ();
    			ControlPaint.DrawMenuGlyph(e.Graphics, 2, 2, 14, 14, MenuGlyph.Checkmark);
    			SolidBrush sb = new SolidBrush (e.ForeColor);
    			e.Graphics.DrawString ("Open", e.Font, sb, e.Bounds.Left + 18, e.Bounds.Top + 2);
    			 
    			sb.Dispose ();
    		}
    
    		private string ColorToString(Color clr)
    		{
    			StringBuilder sb = new StringBuilder(100);
    			sb.Append (clr.Name);
    			sb.Append (" A:");
    			sb.Append (clr.A);
    			sb.Append (" R:");
    			sb.Append (clr.R);
    			sb.Append (" G:");
    			sb.Append (clr.G);
    			sb.Append (" B:");
    			sb.Append (clr.B);
    			return sb.ToString();
    		}
    
    		private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    		{
    			e.ItemHeight = 20;
    			e.ItemWidth = 70;
    		}
    	}
    }
    
  5. Run the application that you created in step 3, and then click the File menu.

    Notice the foreground and background colors. Instead of the colors that you expect, the menu uses the system Window colors.

Modification Type:MajorLast Reviewed:3/2/2004
Keywords:kbcode kbBug kbGDIPlus kbWindowsForms KB834530 kbAudDeveloper