c# .net 如何为MENU菜单加图片 (转)

方法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;  
  }  

posted @ 2008-01-14 16:57  zhangsir  阅读(1918)  评论(0编辑  收藏  举报