講解:自己做出VS.NET風格的右鍵菜單

來源:互聯網
上載者:User
菜單|右鍵

  自己做出VS.NET風格的右鍵菜單(簡單,實用)

  此主題相關圖片如下:class MyMenuItem : System.Windows.Forms.MenuItem

{ 
public MyMenuItem() 
{ 
//這裡很重要,必須把OwerDraw設為true,這樣可以自己畫菜單,否則便是讓作業系統畫菜單了,預設的是false 
this.OwnerDraw=true; 
} 
protected override void OnDrawItem(SysDrawItemEventArgs e) 
{ 
//要重畫菜單,是沒有OnPaint方法重載的,只有重載OnDrawItem方法! 
Graphics g=e.Graphics; 
g.SmoothingMode=SmoothingMode.AntiAlias;//消除鋸齒 
Font f = new Font(FontFamily.GenericSerif, 12, FontStyle.Regular, GraphicsUnit.Pixel);//設定菜單的字型 
Pen p=new Pen(Color.Navy,1);//這是畫邊框的字型 
if(e.State==DrawItemState.NoAccelerator)//一開始按右鍵出現菜單,但是滑鼠並沒有移上去 
{ //用白色的底色 
g.FillRectangle(Brushes.WhiteSmoke,e.Bounds.X-2,e.Bounds.Y-2,121,23); 
} 
//滑鼠移上去,但是並沒有單擊 
if ((e.State &DrawItemState.Selected)==DrawItemState.Selected) 
{ 
//花邊框和底色 
g.FillRectangle(Brushes.LightSteelBlue,e.Bounds.X,e.Bounds.Y,109,20); 
g.DrawLine(p,e.Bounds.X,e.Bounds.Y,e.Bounds.X,e.Bounds.Y+19); 
g.DrawLine(p,e.Bounds.X,e.Bounds.Y+19,e.Bounds.X+109,e.Bounds.Y+19); 
g.DrawLine(p,e.Bounds.X+109,e.Bounds.Y+19,e.Bounds.X+109,e.Bounds.Y); 
g.DrawLine(p,e.Bounds.X+109,e.Bounds.Y,e.Bounds.X,e.Bounds.Y); 
} 
//顯示文字 
g.DrawString(this.Text,f,Brushes.Black,e.Bounds.X,e.Bounds.Y); 
g.Dispose(); 
} 
//這是很重要的,這給你的菜單定義了大小,高20,寬100,否則你的菜單什麼也看不到 
protected override void OnMeasureItem(MeasureItemEventArgs e) 
{ 
e.ItemHeight=20; 
e.ItemWidth=100; 
} 
} 

  說明:這裡我沒有畫按鈕按下時的樣子(懶:),主要是以後進一步改進),當然也沒有畫表徵圖,也是為了以後改進,這隻是一個初步的形態,大家看看有什麼更高的方法?!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.