方法1:
需要使用到API
首先: using System.Runtime.InteropServices;
然後:
[DllImport("user32",EntryPoint="SetMenuItemBitmaps")]
private static extern int SetMenuItemBitmaps (IntPtr hMenu, int nPosition, int wFlags, IntPtr hBitmapUnchecked, IntPtr hBitmapChecked);
最後在Form_Load時:
Bitmap bitmap = new Bitmap("c:\\icon.ico");
int MF_BYPOSITION = 0x0400;
SetMenuItemBitmaps(menuFile.Handle,0,MF_BYPOSITION,bitmap.GetHbitmap(System.Drawing.Color.White),bitmap.GetHbitmap(System.Drawing.Color.White));
注:此表徵圖我為白色底,如果為紅色底的則使用 System.Drawing.Color.Red;
以此類推,就OK了。
方法2:
用.net也可以做出來的,方法如下:
/// <summary>
/// 繪製功能表項目 XP 風格
/// </summary>
/// <param name="e">繪製菜單事件</param>
/// <param name="unSelectBackColor">未選擇時背景色</param>
/// <param name="selectBackColor">選擇時背景色</param>
/// <param name="selectBackColorEnd">選擇時背景過渡色,如果沒有請用透明色</param>
/// <param name="unSelectForeColor">未選擇時前景色彩</param>
/// <param name="selectForeColor">選擇時前景色彩</param>
/// <param name="menuRect">菜單矩框大小</param>
/// <param name="imgMenu">整個菜單左邊的圖片</param>
/// <param name="iconMenu">功能表項目表徵圖</param>
/// <param name="iconTranColor">功能表項目表徵圖之透明色</param>
/// <param name="sMenuItemWord">功能表項目文字</param>
private void DrawMenuItem(DrawItemEventArgs e, Color unSelectBackColor,Color selectBackColor,Color selectBackColorEnd,Color unSelectForeColor,Color selectForeColor,Image imgMenu,Image iconMenu,Color iconTranColor,String sMenuItemWord)
{
Bitmap iconBitMapMenu = new Bitmap(iconMenu);
iconBitMapMenu.MakeTransparent(iconTranColor);
Rectangle rectMenu = e.Bounds;
Brush selectBackBrush = new System.Drawing.SolidBrush(selectBackColor);
Brush unSelectBackBrush = new System.Drawing.SolidBrush(unSelectBackColor);
Brush selectForeBrush = new System.Drawing.SolidBrush(selectForeColor);
Brush unSelectForeBrush = new System.Drawing.SolidBrush(unSelectForeColor);
if(e.State == DrawItemState.NoAccelerator)
{
e.Graphics.FillRectangle(unSelectBackBrush,rectMenu);
e.Graphics.DrawString(sMenuItemWord,this.Font,unSelectForeBrush,rectMenu.X+40,rectMenu.Y+4);
e.Graphics.DrawImageUnscaled(iconBitMapMenu,rectMenu.X+22,rectMenu.Y+2,iconMenu.Width,iconMenu.Height);
}
else
{
if(selectBackColorEnd != Color.Transparent)
selectBackBrush = new System.Drawing.Drawing2D.LinearGradientBrush(rectMenu,selectBackColor,selectBackColorEnd,0F);
e.Graphics.FillRectangle(selectBackBrush,rectMenu);
e.Graphics.DrawString(sMenuItemWord,this.Font,selectForeBrush,rectMenu.X+40,rectMenu.Y+4);
e.Graphics.FillRectangle(System.Drawing.Brushes.WhiteSmoke,rectMenu.X+20,rectMenu.Y,20,20);//表徵圖背景
e.Graphics.DrawImageUnscaled(iconBitMapMenu,rectMenu.X+2+20,rectMenu.Y+2,imgMenu.Width,imgMenu.Height);//表徵圖
}
e.Graphics.DrawImageUnscaled(imgMenu,0,0);
}
//不過,您要在調用前設Menu的OwnerDraw=true才行哦。以下是調用方法:
private void menuExit_DrawItem(object sender, DrawItemEventArgs e)
{
DrawMenuItem(e,Color.WhiteSmoke,Color.DeepSkyBlue,Color.White,Color.Black,Color.Black,picMenu.Image,imageList.Images[2],Color.White,"退出");
}
private void menuExit_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemWidth = 150;
e.ItemHeight = 20;
}