How to chage MainMenu background color?

J

James Radke

Hello,

I have figured out how to change the background colors of menuitems on my
windows form, but I cannot figure out how to change the MainMenu background
color? Because of this, if I change my menuitem background color to white,
from where my last menu item ends - till the width of the screen - shows up
as a gray color in the menu row!

What I would like is for the rest of that row to show up in white, as well!

How can I do this?

Thanks!

Jim
 
D

DalePres

How are you changing the background of the menuItems? Are you using
ownerDraw?

Dale
 
J

James Radke

Dale,

Yes, I created a nice little class that works very well. It allows me to
replace the term MenuItem with OwnerDrawMenuItem in my form (i.e. where it
is created and instantiated), and then it handles the overides for me.

It seems to be working well for what I am trying to do.

But, it leaves half the menu line with that ugly gray color.. which I can't
seem to get around!

Jim
 
D

DalePres

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);
}
 
J

James Radke

Dale,

Thanks! I had to tweak it a little to keep my menu items from disappearing
as I moved over them, but you gave me a great example from which I could
solve my problem! It all works fine now..... And looks so much better!

Thanks again!

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top