Windows Develop Bookmark and Share   
 index > Windows Forms General > Change backcolor and forecolor when hovering menu items of MenuStrip
 

Change backcolor and forecolor when hovering menu items of MenuStrip

Hello,

That´s my question. I have no clue how to change forecolor of items when hovering them. I change color when hovering but forecolor is the same before hovering so I can´t see it. I would like to change it.

This is the snippet code I´m using to configure my MenuStrip:

this

.MainMenuStrip = menuStrip1;

//Add a menu.
MyMenuItemPrincipal menuItem = new MyMenuItemPrincipal();
menuItem.Text =
"Prueba";
menuItem.Width = 100;
menuItem.BackColor =
Color.Transparent;
menuItem.ForeColor =
Color.White;
menuItem.Font =
new Font(this.Font, FontStyle.Bold);
this.MainMenuStrip.Items.Add(menuItem);

//Add a sub menu.
MyMenuItemSecundario subItem = new MyMenuItemSecundario();
subItem.BackColor =
Color.FromArgb(243,245,192);
subItem.ForeColor =
Color.FromArgb(72, 72, 61);
subItem.Text =
"Open";
menuItem.DropDownItems.Add(subItem);

//Add a menu.
MyMenuItemPrincipal menuItem = new MyMenuItemPrincipal();
menuItem.Text =
"Prueba";
menuItem.Width = 100;
menuItem.BackColor =
Color.Transparent;
menuItem.ForeColor =
Color.White;
menuItem.Font =
new Font(this.Font, FontStyle.Bold);
this.MainMenuStrip.Items.Add(menuItem);

//Add a sub menu.
MyMenuItemSecundario subItem = new MyMenuItemSecundario();
subItem.BackColor =
Color.FromArgb(243,245,192);
subItem.ForeColor =
Color.FromArgb(72, 72, 61);
subItem.Text =
"Open";
menuItem.DropDownItems.Add(subItem);

MyMenuItemSecundario subItem = new MyMenuItemSecundario();
subItem.BackColor =
Color.FromArgb(243,245,192);
subItem.ForeColor =
Color.FromArgb(72, 72, 61);
subItem.Text = "Open";
menuItem.DropDownItems.Add(subItem);

public

class MyMenuItemPrincipal : ToolStripMenuItem
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Selected)
{
SolidBrush a = new SolidBrush(Color.FromArgb(243,245,192));
e.Graphics.FillRectangle(a, e.ClipRectangle);
Rectangle rect = e.ClipRectangle;
rect.X += 100;
rect.Width -= 100;
StringFormat sf = new StringFormat();
sf.LineAlignment =
StringAlignment.Center;
Font b = new Font(this.Font,FontStyle.Bold);
e.Graphics.DrawString(
this.Text, b, Brushes.White, rect, sf);
}
}

public class MyMenuItemSecundario : ToolStripMenuItem
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Selected)
{
SolidBrush a = new SolidBrush(Color.FromArgb(243, 245, 192));
//e.Graphics.FillRectangle(Brushes.Transparent, e.ClipRectangle);
Rectangle rect = e.ClipRectangle;
rect.X += 100;
rect.Width -= 100;
StringFormat sf = new StringFormat();
sf.LineAlignment =
StringAlignment.Center;
e.Graphics.DrawString(
this.Text, this.Font, Brushes.Red, rect, sf);
}
}
}

Could you help me?It´s really important to get this.

My other question is: in the same MenuStrip I have the a menu item and this one has a item son. When opening the main item (dad) to access to the son item, the background color of the main item is a bright blue. I would like to know how to change this too.

Thank you in advance, I hope you can help me.

Greetings,
egurre_egurre

egurre_egurre  Thursday, September 17, 2009 3:58 PM
This is most easily done by providing your own renderer. For example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
menuStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer {
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) {
if (!e.Item.Selected) base.OnRenderMenuItemBackground(e);
else {
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
e.Graphics.FillRectangle(Brushes.Beige, rc);
e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);
}
}
}
}
}

Hans Passant.
nobugz  Thursday, September 17, 2009 5:50 PM
This is most easily done by providing your own renderer. For example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
menuStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer {
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) {
if (!e.Item.Selected) base.OnRenderMenuItemBackground(e);
else {
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
e.Graphics.FillRectangle(Brushes.Beige, rc);
e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);
}
}
}
}
}

Hans Passant.
nobugz  Thursday, September 17, 2009 5:50 PM
Thank you for your reply Hans.

I have implemented your code but still doesn´t fix my problem. When hovering doesn´t change forecolor (backColor it does) of main menu and submenu. Hover works ok, but I need to change foreColor because hover color is similar to foreColor.

The other thing is when main menu item is expanded to show submenu items. If I hover a submenu item, the selected itemof main menu to showsubmenuhas abright blue color.

I need to get this somehow, I would be very grateful if you could help me.
egurre_egurre  Thursday, September 24, 2009 10:41 AM
I showed you what is needed, it is up to you to override more of the methods to get it the way you want it.

Hans Passant.
nobugz  Thursday, September 24, 2009 11:50 AM
Thanks so much Hans! Ive been looking on google for some time now and youre method is explained so easy! :D
I finnaly got my highlight color changed ;) Thanks

Jordy Emond,
Jordemon  Friday, September 25, 2009 12:52 PM
I showed you what is needed, it is up to you to override more of the methods to get it the way you want it.

Hans Passant.

I appreciate your help but I´m stuck on developing my own styles. I will be grateful if you could at least tell me how to change forecolor when hovering items. I have been developing for hours but I am not able to change it, that´s why I need a litle push because I see that I´m close to the solution.

Thank you.
egurre_egurre  Monday, September 28, 2009 9:06 AM
I changed the code a bit so that everything will changes colors now, the root of the buttons stayed windows color blue or something like that, but now everything is the color i wanted ;) also on hover.

Here is the code:

private

class MyRenderer : ToolStripProfessionalRenderer

{

protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)

{

if (!e.Item.Selected)

{

Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);

e.Graphics.FillRectangle(

Brushes.Red, rc);

e.Graphics.DrawRectangle(

Pens.Red, 1, 0, rc.Width - 2, rc.Height - 1);

}

else

{

Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);

e.Graphics.FillRectangle(

Brushes.Gray, rc);

e.Graphics.DrawRectangle(

Pens.Gray, 1, 0, rc.Width - 2, rc.Height - 1);

}

}

}





Hope it helps ;)

Greets

Jordemon  Monday, September 28, 2009 12:45 PM

You can use google to search for other answers

Custom Search

More Threads

• Filling a comboBox from a class file
• Date Data Type
• can I fake a firing of an event handler?
• problema con datagrid
• Secure,Professional,Standard and Readable Code, Please Help Me ??
• bind text boxes to app config
• Button's default property
• TAB-DataGrid
• Flickering Panel
• Alt-PrintScreen from VB?