更正 private void menuItem1_DrawItem_1(object sender, System.Windows.Forms.DrawItemEventArgs e) { MenuItem mi = (MenuItem) sender; // 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, Globals.menuFont, new SolidBrush(Globals.TextColor), e.Bounds , sf); Rectangle rect1 = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, 12, e.Bounds.Height - 1); e.Graphics.DrawImage(this.imageList1.Images[1], rect1) ;
} private void menuItem1_MeasureItem_1(object sender, System.Windows.Forms.MeasureItemEventArgs e) { //MessageBox.Show(sender.ToString()); 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, Globals.menuFont); int scWidth = 0; // get the short cut width if ( mi.Shortcut != Shortcut.None ) { SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), Globals.menuFont); scWidth = Convert.ToInt32(scSize.Width); } // set the bounds int miHeight = Convert.ToInt32(miSize.Height) + 7; if (miHeight < 25) miHeight = Globals.MIN_MENU_HEIGHT; e.ItemHeight = miHeight; e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (Globals.PIC_AREA_SIZE * 2); }
添加一个类 public class Globals { public static int PIC_AREA_SIZE = 24; public static int MIN_MENU_HEIGHT = 22; public static Font menuFont = System.Windows.Forms.SystemInformation.MenuFont; public static Color CheckBoxColor = Color.FromArgb(255, 192, 111); public static Color UnCheckBoxColor = Color.FromArgb(182,189,250); public static Color DarkCheckBoxColor = Color.FromArgb(254, 128, 62); public static Color SelectionColor = Color.FromArgb(255,238,194); public static Color TextColor = Color.FromKnownColor(KnownColor.MenuText); public static Color TextDisabledColor = Color.FromKnownColor(KnownColor.GrayText); public static Color MenuBgColor = Color.White; public static Color MainColor = Color.FromKnownColor(KnownColor.Control); public static Color MenuDarkColor = Color.FromKnownColor(KnownColor.ActiveCaption); public static Color MenuDarkColor2 = Color.FromArgb(110, Color.FromKnownColor(KnownColor.ActiveCaption)); public static Color MenuLightColor = Color.FromKnownColor(KnownColor.InactiveCaption); public static Color MenuLightColor2 = Color.FromArgb(50, Color.FromKnownColor(KnownColor.InactiveCaption)); public static int height = 20; }
但是CONTEXTMENU左侧的图片不属于任何一个MENUITEM呀,我的问题是这个图片该怎么画!
{
// 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,
Globals.menuFont,
new SolidBrush(Globals.TextColor),
e.Bounds ,
sf);
Rectangle rect1 = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
12,
e.Bounds.Height - 1);
e.Graphics.DrawImage(this.imageList1.Images[1], rect1) ;
} private void menuItem1_MeasureItem_1(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, Globals.menuFont);
int scWidth = 0;
// get the short cut width
if ( mi.Shortcut != Shortcut.None )
{
SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), Globals.menuFont);
scWidth = Convert.ToInt32(scSize.Width);
}
// set the bounds
int miHeight = Convert.ToInt32(miSize.Height) + 7;
if (miHeight < 25) miHeight = Globals.MIN_MENU_HEIGHT;
e.ItemHeight = miHeight;
e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (Globals.PIC_AREA_SIZE * 2);
}
}
this.imageList1.Images[1]要求你添加一个图像列表
private void menuItem1_DrawItem_1(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
MenuItem mi = (MenuItem) sender;
// 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,
Globals.menuFont,
new SolidBrush(Globals.TextColor),
e.Bounds ,
sf);
Rectangle rect1 = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
12,
e.Bounds.Height - 1);
e.Graphics.DrawImage(this.imageList1.Images[1], rect1) ;
} private void menuItem1_MeasureItem_1(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
//MessageBox.Show(sender.ToString());
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, Globals.menuFont);
int scWidth = 0;
// get the short cut width
if ( mi.Shortcut != Shortcut.None )
{
SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), Globals.menuFont);
scWidth = Convert.ToInt32(scSize.Width);
}
// set the bounds
int miHeight = Convert.ToInt32(miSize.Height) + 7;
if (miHeight < 25) miHeight = Globals.MIN_MENU_HEIGHT;
e.ItemHeight = miHeight;
e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (Globals.PIC_AREA_SIZE * 2);
}
public class Globals
{
public static int PIC_AREA_SIZE = 24;
public static int MIN_MENU_HEIGHT = 22;
public static Font menuFont = System.Windows.Forms.SystemInformation.MenuFont;
public static Color CheckBoxColor = Color.FromArgb(255, 192, 111);
public static Color UnCheckBoxColor = Color.FromArgb(182,189,250);
public static Color DarkCheckBoxColor = Color.FromArgb(254, 128, 62);
public static Color SelectionColor = Color.FromArgb(255,238,194);
public static Color TextColor = Color.FromKnownColor(KnownColor.MenuText);
public static Color TextDisabledColor = Color.FromKnownColor(KnownColor.GrayText);
public static Color MenuBgColor = Color.White;
public static Color MainColor = Color.FromKnownColor(KnownColor.Control);
public static Color MenuDarkColor = Color.FromKnownColor(KnownColor.ActiveCaption);
public static Color MenuDarkColor2 = Color.FromArgb(110, Color.FromKnownColor(KnownColor.ActiveCaption));
public static Color MenuLightColor = Color.FromKnownColor(KnownColor.InactiveCaption);
public static Color MenuLightColor2 = Color.FromArgb(50, Color.FromKnownColor(KnownColor.InactiveCaption));
public static int height = 20; }
“我知道 CONTEXTMENU的Item也是MenuItem,我也重画了每个MENUITEM。
但是CONTEXTMENU左侧的图片不属于任何一个MENUITEM呀,我的问题是这个图片该怎么画!”
不就是画图片吗?
http://www.codeproject.com/cs/menu/MhOffice2003Menus.asp
有源码的,你自己好好参考参考
e.Bounds.Y + 1,
Globals.PIC_AREA_SIZE,
60);//假设第一个菜单的名字是"第一项",你自己调节好高度,自己节个图片
if(mi.Text == "第一项")
{
Bitmap MyImage = new Bitmap(@"d:\1.bmp");
e.Graphics.DrawImage(MyImage, rect1) ;
}
{
MenuItem mi = (MenuItem) sender;
// 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,
Globals.menuFont,
new SolidBrush(Globals.TextColor),
e.Bounds ,
sf);
Rectangle rect1 = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
Globals.PIC_AREA_SIZE,
60);
if(mi.Text == "第一项")
{
Bitmap MyImage = new Bitmap(@"d:\1.bmp");
e.Graphics.DrawImage(MyImage, rect1) ;
}
else
{
// Bitmap MyImage = new Bitmap(@"d:\1.bmp");
// e.Graphics.DrawImage(MyImage, rect1) ;
}
}
http://www.codeproject.com/cs/menu/MhOffice2003Menus.asp
设计好效果
也不知道是不是还有更好的办法?