Here's a working example. Most of this code comes from an application of
mine. I modified it to change the menuitem background color and tested it
and it solves your problem. I used yellow for the background since XP
already makes menus white.
The DrawBackground() will only use the default menu color so you have to
draw the background and then fill it with the desired color. rectCheck is
the checkbox rectangle, rectText is the Text rectangle. This left an
unpainted border that looked like one or two pixels wide that I didn't
pursue, but I'm sure it's just a matter of creating a new rectangle that is
inclusive of the border, the check rectangle, and the text rectangle. Then
you'd only have to call FillRectangle once.
Good luck,
Dale
private void itemOnDraw(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
MenuItem mi = (MenuItem)sender;
Graphics grfx = e.Graphics;
Brush brush;
// Create the Font and StringFormat
Font font = new Font(SystemInformation.MenuFont,FontStyle.Bold);
StringFormat format = new StringFormat();
format.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
Rectangle rectCheck = e.Bounds;
rectCheck.Width = SystemInformation.MenuCheckSize.Width *
rectCheck.Height /
SystemInformation.MenuCheckSize.Height;
Rectangle rectText = e.Bounds;
rectText.X += rectCheck.Width;
e.DrawBackground();
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow),rectCheck);
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow),rectText);
if ((e.State & DrawItemState.Checked) != 0)
{
ControlPaint.DrawMenuGlyph(grfx, rectCheck, MenuGlyph.Bullet);
}
if ((e.State & DrawItemState.Selected) != 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,rectCheck);
e.Graphics.FillRectangle(SystemBrushes.Highlight,rectText);
brush = SystemBrushes.HighlightText;
}
else
{
brush = new SolidBrush(Color.Red);
}
grfx.DrawString(mi.Text, font, brush, rectText, format);
}