c# .net 如何為MENU菜單加圖片

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.