先说菜单 this.menuItem1.OwnerDraw = true; 加上两个事件 private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { MenuItem mi = (MenuItem) sender; // Create the hover rectangle Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height - 1); // Create the hover brush Brush b = new LinearGradientBrush(rect, Color.White, Color.CadetBlue, 90f, false); // Fill the rectangle e.Graphics.FillRectangle(b, rect); Rectangle rect1 = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width + 1, e.Bounds.Height ); e.Graphics.DrawRectangle(new Pen(Color.Blue), rect1);
// Create stringFormat object StringFormat sf = new StringFormat(); // set the Alignment to center sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; // Draw the text e.Graphics.DrawString(mi.Text, System.Windows.Forms.SystemInformation.MenuFont, new SolidBrush(Color.FromKnownColor(KnownColor.MenuText)), e.Bounds, sf); } private void menuItem1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { MenuItem mi = (MenuItem) sender; // if the item is a seperator if ( mi.Text == "-" ) { e.ItemHeight = 7; } else { // get the item's text size SizeF miSize = e.Graphics.MeasureString(mi.Text, System.Windows.Forms.SystemInformation.MenuFont); int scWidth = 0; // get the short cut width if ( mi.Shortcut != Shortcut.None ) { SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), System.Windows.Forms.SystemInformation.MenuFont); scWidth = Convert.ToInt32(scSize.Width); } // set the bounds int miHeight = Convert.ToInt32(miSize.Height) + 7; if (miHeight < 25) miHeight = 22; e.ItemHeight = miHeight; e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (24 * 2); } } }
this.menuItem1.OwnerDraw = true;
加上两个事件
private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
MenuItem mi = (MenuItem) sender;
// Create the hover rectangle
Rectangle rect = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
e.Bounds.Width,
e.Bounds.Height - 1); // Create the hover brush
Brush b = new LinearGradientBrush(rect,
Color.White,
Color.CadetBlue,
90f, false); // Fill the rectangle
e.Graphics.FillRectangle(b, rect);
Rectangle rect1 = new Rectangle(e.Bounds.X,
e.Bounds.Y,
e.Bounds.Width + 1,
e.Bounds.Height );
e.Graphics.DrawRectangle(new Pen(Color.Blue), rect1);
// Create stringFormat object
StringFormat sf = new StringFormat(); // set the Alignment to center
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center; // Draw the text
e.Graphics.DrawString(mi.Text,
System.Windows.Forms.SystemInformation.MenuFont,
new SolidBrush(Color.FromKnownColor(KnownColor.MenuText)),
e.Bounds,
sf);
} private void menuItem1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
MenuItem mi = (MenuItem) sender;
// if the item is a seperator
if ( mi.Text == "-" )
{
e.ItemHeight = 7;
}
else
{
// get the item's text size
SizeF miSize = e.Graphics.MeasureString(mi.Text, System.Windows.Forms.SystemInformation.MenuFont);
int scWidth = 0;
// get the short cut width
if ( mi.Shortcut != Shortcut.None )
{
SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), System.Windows.Forms.SystemInformation.MenuFont);
scWidth = Convert.ToInt32(scSize.Width);
}
// set the bounds
int miHeight = Convert.ToInt32(miSize.Height) + 7;
if (miHeight < 25) miHeight = 22;
e.ItemHeight = miHeight;
e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (24 * 2);
}
}
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ Panel lbl = (Panel)sender;
Rectangle rect = new Rectangle(e.ClipRectangle.X,
e.ClipRectangle.Y,
e.ClipRectangle.Width -1,
e.ClipRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Blue),rect );
}
Rectangle rect1;
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
this.rect1 = e.ClipRectangle;
}
private void panel1_MouseEnter(object sender, System.EventArgs e)
{
Graphics g = ((Panel)sender).CreateGraphics();
Rectangle rect = new Rectangle(this.rect1.X,
this.rect1.Y,
this.rect1.Width -1,
this.rect1.Height-1);
g.DrawRectangle(new Pen(Color.Blue),rect);
}private void panel1_MouseLeave(object sender, System.EventArgs e)
{
Graphics g = ((Panel)sender).CreateGraphics();
Rectangle rect = new Rectangle(this.rect1.X,
this.rect1.Y,
this.rect1.Width -1,
this.rect1.Height-1);
g.DrawRectangle(new Pen(((Panel)sender).BackColor),rect);
}
参考
http://www.codeproject.com/cs/menu/MhOffice2003Menus.asp
结合我给你的代码就能实现了