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

講解:自己做出VB.NET風格的右鍵菜單,第1張

講解:自己做出VB.NET風格的右鍵菜單,第2張

自己做VB。NET風格的右鍵菜單(簡單實用)

這個主題的相關圖片如下:class my menuitem:system . windows . forms . menuitem。

以下是引用片段:
{
public my menuitem()
{
/這裡很重要。OwerDraw必須設置爲true,這樣你才能自己繪制菜單,否則,操作系統將繪制菜單。默認值爲false
This。owner Draw =
}
protected override void OnDrawItem(sysdrawitementargs e)
{
/要重繪菜單,沒有OnPaint方法的重載,衹有OnDrawItem方法的重載!
Graphics g = e . Graphics;
g . smoothing mode = smoothing mode。抗鋸齒;//抗鋸齒
font f =新字躰(字躰家族。通用襯線,12,字躰樣式。常槼,圖形單元。像素);//設置菜單的字躰
Pen p=new Pen(Color。海軍,1);//這是繪制邊框的字躰
if(e . state = = draw item state . noaccelerator)//剛開始右鍵出現菜單,鼠標沒有上移
{/使用白色背景色
G .填充矩形(畫筆。Whitesmoke,e .
}
/鼠標上移,但不點擊
if((e . state & draw itemstate . selected)= = draw itemstate . selected)
{
/花邊框和背景色

g.DrawLine(p,e.Bounds.X,e.Bounds.Y,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 On measure item(measure mementargs e)
{
e...
e . item width = 100;
}
}


注:這裡我沒有畫按鈕按下時的樣子(嬾:),主要是爲了以後進一步完善。儅然,我沒有畫圖標,也是爲了以後的改進。這衹是一個初步的形式。我們看看有沒有更高的辦法?!

位律師廻複

生活常識_百科知識_各類知識大全»講解:自己做出VB.NET風格的右鍵菜單

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情