Microsoft KB Archive/834530

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

Article ID: 834530

Article Last Modified on 3/2/2004

-

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 &quot;Applies to&quot; 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 (&quot;Open&quot;, 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.



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.  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.  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.</li> Click Menu in the Item list, click Green for Color1, and then click Pink for the Font color.</li></ul> </li> Start Visual Studio .NET, and then create a Windows application by using Visual C# .NET.</li>  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 ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        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 = &quot;File&quot;; //            // menuItem2 //            this.menuItem2.Index = 0; this.menuItem2.OwnerDraw = true; this.menuItem2.Text = &quot;Open&quot;; 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 = &quot;textBox1&quot;; this.textBox1.Size = new System.Drawing.Size(272, 20); this.textBox1.TabIndex = 0; this.textBox1.Text = &quot;&quot;; //            // textBox2 //            this.textBox2.Location = new System.Drawing.Point(8, 160); this.textBox2.Name = &quot;textBox2&quot;; this.textBox2.Size = new System.Drawing.Size(272, 20); this.textBox2.TabIndex = 1; this.textBox2.Text = &quot;&quot;; //            // menuItem3 //            this.menuItem3.Checked = true; this.menuItem3.Index = 1; this.menuItem3.Text = &quot;Close&quot;; //            // 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 = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; this.ResumeLayout(false);

}       #endregion

///        /// The main entry point for the application. ///        [STAThread] static void Main {           Application.Run(new Form1); }

private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)       { textBox1.Text = &quot;BackColor: &quot; + ColorToString(e.BackColor); textBox2.Text = &quot;ForeColor: &quot; + ColorToString(e.ForeColor); e.DrawBackground ; ControlPaint.DrawMenuGlyph(e.Graphics, 2, 2, 14, 14, MenuGlyph.Checkmark); SolidBrush sb = new SolidBrush (e.ForeColor); e.Graphics.DrawString (&quot;Open&quot;, 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 (&quot; A:&quot;); sb.Append (clr.A); sb.Append (&quot; R:&quot;); sb.Append (clr.R); sb.Append (&quot; G:&quot;); sb.Append (clr.G); sb.Append (&quot; B:&quot;); sb.Append (clr.B); return sb.ToString; }

private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)       { e.ItemHeight = 20; e.ItemWidth = 70; }   } } </li> 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.</li></ol>

Additional query words: owner drawn menuitem forcolor backcolor checkmark paint

Keywords: kbbug kbwindowsforms kbgdiplus kbcode KB834530

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.