BUG: The BackColor and ForeColor Properties Do Not Show New Values After You Change the Colors (818812)



The information in this article applies to:

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

SYMPTOMS

After you change the colors of the Background and Foreground menu items in the display settings in Control Panel, the default Windows colors appear for those menu items.

Incorrect values are contained in the BackColor property and the ForeColor property of the DrawItemEventArgs class that is received by the DrawItem event handler of a MenuItem.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

To reproduce the behavior on a Windows 2000-based system, follow these steps:
  1. Open a text editor such as Notepad, and then paste the following code in a new file:
    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.Windows.Forms.MenuItem menuItem4;
    	
    		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 );
    		}
    
    		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.menuItem4 = new System.Windows.Forms.MenuItem();
    			this.SuspendLayout();
    
    			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
    			this.menuItem1.Index = 0;
    			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem2,																					  this.menuItem3,
    												this.menuItem4});
    			this.menuItem1.Text = "File";
    			 
    			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);
    			
    			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 = "";
    			
    			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 = "";
    			
    			this.menuItem3.Index = 1;
    			this.menuItem3.Text = "Close";
    			
    			this.menuItem4.Index = 2;
    			this.menuItem4.Text = "Exit";
    			 
    			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);
    		}
    
    		[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 ();
    			SolidBrush sb = new SolidBrush (e.ForeColor);
    			e.Graphics.DrawString ("Open", e.Font, sb, e.Bounds.Left + 2, 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 = 50;
    		}
    	}
    }
    
  2. Save the file as Sample.cs.
  3. Click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
  4. From a command prompt, run the following command to compile the C# code:csc sample.cs
  5. Open Control Panel, and then double-click Display.
  6. Click the Appearance tab, and then click the Advanced button.
  7. In the Item combo box, select Menu, and then set the background color to green and the foreground color to pink.
  8. Click OK to close the dialog box.
  9. From a command prompt, run the following executable (generated in step 2): sample.exe
  10. On the File menu, click DrawItemEventArgs to view the BackColor and ForeColor properties.

    You expect to see the RGB color values that you set (green for the background and pink for the foreground) for the menu. However, the colors that appear are the default Windows colors that are associated with that window item.

REFERENCES

For more information about the DrawItemEventArgs class, see the following MSDN documentation:

Modification Type:MinorLast Reviewed:4/21/2006
Keywords:kbDSWGDI2003Swept kbMenu kbControl kbBug KB818812 kbAudDeveloper