国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

用c#繪制Office2003樣式的菜單

2024-07-21 02:18:29
字體:
來源:轉載
供稿:網友
我的方法是重寫menuitem類,在寫的過程中發現用gdi+實際測量出來的文本的大小不是很準確,還有文本也不能很好對齊,固在代碼里可以時??吹胶芏喽嗉由先サ臄底?我想原理就不用我講了吧,下面的代碼注釋的很
清楚了:
using system;
using system.drawing;
using system.drawing.drawing2d;
using system.windows.forms;
namespace officemenu
{
/// <summary>
/// class1 的摘要說明。
/// </summary>
public class officemenuitem:menuitem
{
public officemenuitem(bool isapplyofficemenu,string itemtext)
{
if(isapplyofficemenu)
{
this.text=itemtext;
this.ownerdraw=true;
font=new font("宋體",9f,graphicsunit.point);
}
else
{
this.text=itemtext;
this.ownerdraw=false;
}
}

private font font;
private stringformat strformat=new stringformat();
private string itemtext="";
private string iconpath="";

//定義繪制的時候的各種筆刷
private lineargradientbrush topmenuhostlightbrush;
private solidbrush topmenudefaultbrush;
private solidbrush fontdefaultbrush;
private lineargradientbrush topmenuselectedbrush;
private solidbrush menudefaultbrush;
private solidbrush menudefaultselectedbrush;
private solidbrush menucheckbrush;
private lineargradientbrush menusidebarbrush;
private solidbrush menudefaultgrayedbrush;
//定義繪制的時候所需的各種線條
private pen topmenupen;
private pen menudefaultpen;
//定義需要的矩形結構
private rectangle sidebarrect;
private rectangle checkedrect;
private rectangle topmenurect;
private rectangle shortcutrect;



//定義圖標
public string iconpath
{
get
{
return iconpath;
}
set
{
iconpath=value;
}
}

//判斷菜單是否是頂級菜單
private bool checkistop()
{
if(typeof(mainmenu)==this.parent.gettype())
{
return true;
}
else
{
return false;
}
}
//獲取菜單的快捷鍵
private string getshortcut()
{
string shortcut="";
try
{
if(this.showshortcut==true && (!this.isparent) && (!(this.shortcut==shortcut.none)))
{
shortcut=this.shortcut.tostring();
shortcut=shortcut.replace("ctrl","ctrl+");
shortcut=shortcut.replace("alt","alt+");
shortcut=shortcut.replace("shift","shift+");
}
}
catch
{
return "";
}
return shortcut;
}
//重寫測量函數
protected override void onmeasureitem(measureitemeventargs e)
{
base.onmeasureitem (e);

try
{
itemtext=this.text;
sizef sizemenutext;
if(this.itemtext=="-") //設置分割線的高度
{
e.itemheight=5;
return;
}

string getedshortcut=this.getshortcut();
if(!this.checkistop()) //如果不是頂級菜單
{
sizemenutext=e.graphics.measurestring(itemtext+"".padright(4,' ')+getedshortcut,this.font);
e.itemwidth=convert.toint32(sizemenutext.width)+26;
}
else
{
sizemenutext=e.graphics.measurestring(itemtext,this.font);
e.itemwidth=convert.toint32(sizemenutext.width)-4;
}
if(this.visible)
{
e.itemheight=convert.toint32(sizemenutext.height)+6;
}
else
{
e.itemheight=0;
}

}
catch
{
e.itemheight=20;
e.itemwidth=80;
}
}
//重寫繪圖函數
protected override void ondrawitem(drawitemeventargs e)
{
if(this.visible)
{
base.ondrawitem (e);
rectangle menurect=e.bounds;
itemtext=this.text;
graphics g=e.graphics;

//繪制邊框時需要的特殊矩形
sidebarrect=new rectangle(menurect.x,menurect.y,25,menurect.height);
//繪制選擇框時需要的矩形
checkedrect=new rectangle(menurect.x+1,menurect.y+1,24,menurect.height-2);
//繪制頂菜單時需要的矩形
topmenurect=new rectangle(menurect.x+1,menurect.y+3,menurect.width,menurect.height);
//繪制shortcut用到的矩形
shortcutrect=new rectangle(menurect.x,menurect.y+4,menurect.width-10,menurect.height);

//定義繪制的時候的各種筆刷
topmenuhostlightbrush=new lineargradientbrush(menurect,color.white,color.fromargb(220,134,56),90f); //頂級菜單發生hotlight 時顯示的顏色
topmenudefaultbrush=new solidbrush(systemcolors.control); //頂級菜單默認的顏色
fontdefaultbrush=new solidbrush(color.black); //字體默認的顏色
topmenuselectedbrush=new lineargradientbrush(menurect,color.white,color.fromargb(95,109,126),90f); //頂級菜單選中時的顏色
menudefaultbrush=new solidbrush(color.white); //默認菜單的顏色
menudefaultselectedbrush=new solidbrush(color.fromargb(247,218,157)); //菜單選中時的顏色
menucheckbrush=new solidbrush(color.fromargb(225,143,70)); //當菜單checked屬性為真的時顯示的顏色
menusidebarbrush=new lineargradientbrush(sidebarrect,color.white,color.fromargb(149,160,174),0f);//繪制側欄
menudefaultgrayedbrush=new solidbrush(color.fromargb(105,105,105));//菜單不可用時
//定義繪制的時候所需的各種線條
topmenupen=new pen(brushes.black,1f); //頂級菜單的邊框
menudefaultpen=new pen(new solidbrush(color.fromargb(60,60,115)),1f); //正常選擇時的邊框

//開始繪制****************************************************
if(this.checkistop() && this.itemtext!="-") //如果是頂級菜單
{
bool isdraw=false;//定義一個變量保證正常顯示的只繪制一次 避免浪費資源
strformat.alignment=stringalignment.center;
if(isdraw==false) //繪制正常顯示時的圖像
{
g.fillrectangle(topmenudefaultbrush,menurect);
g.drawstring(itemtext,this.font,fontdefaultbrush,topmenurect,strformat);
}

//繪制hotlight時的圖像
if((e.state & drawitemstate.hotlight)==drawitemstate.hotlight) //突出顯示時
{
g.fillrectangle(topmenuhostlightbrush,menurect);
g.drawrectangle(topmenupen,menurect.x,menurect.y,menurect.width-1,menurect.height-1);
g.drawstring(itemtext,this.font,fontdefaultbrush,topmenurect,strformat);
isdraw=true;
}
//繪制菜單選中時的圖像
if((e.state & drawitemstate.selected)==drawitemstate.selected)
{
g.fillrectangle(topmenuselectedbrush,menurect);
g.drawrectangle(topmenupen,menurect.x,menurect.y,menurect.width-1,menurect.height-1);
g.drawstring(itemtext,this.font,fontdefaultbrush,topmenurect,strformat);
isdraw=true;
}
}
else //非頂級菜單時
{
if(this.itemtext!="-")
{
bool isdraw=false;
if(isdraw==false) //正常顯示時
{
g.fillrectangle(menudefaultbrush,menurect);
g.fillrectangle(menusidebarbrush,menurect.x,menurect.y,25,menurect.height);
g.drawstring(itemtext,this.font,fontdefaultbrush,menurect.x+26,menurect.y+4);
//繪制shortcut
string shortcut=this.getshortcut();
strformat.alignment=stringalignment.far;
g.drawstring(shortcut,this.font,fontdefaultbrush,shortcutrect,strformat);
//繪制圖標
this.drawicon(g);
}

if((e.state & drawitemstate.selected)==drawitemstate.selected) //選中時
{
this.drawitem_selected(g,menurect,itemtext);
isdraw=true;
}

if((e.state & drawitemstate.grayed)==drawitemstate.grayed) //不可用時
{
g.drawstring(itemtext,this.font,menudefaultgrayedbrush,menurect.x+26,menurect.y+4);
isdraw=true;
}

if(!this.isparent)
{
if((e.state & drawitemstate.checked)==drawitemstate.checked) //選擇時
{
g.fillrectangle(menucheckbrush,checkedrect);
g.drawrectangle(new pen(brushes.black,1f),menurect.x+1,menurect.y+1,23,menurect.height-3);
g.drawstring("√",this.font,fontdefaultbrush,menurect.x+3,menurect.y+4);
isdraw=true;
}

if(this.checked==true && ((e.state & drawitemstate.selected)==drawitemstate.selected)) //選中且checked屬性為真時
{
g.fillrectangle(new solidbrush(color.fromargb(255,91,91)),checkedrect);
g.drawrectangle(new pen(brushes.black,1f),menurect.x+1,menurect.y+1,23,menurect.height-3);
g.drawstring("√",this.font,fontdefaultbrush,menurect.x+3,menurect.y+4);
isdraw=true;
}
}
}
else //畫分割線
{
solidbrush partitionlinebrush=new solidbrush(color.fromargb(128,128,128));
pen partitionpen=new pen(partitionlinebrush,1f);
g.fillrectangle(menudefaultbrush,menurect);
g.fillrectangle(menusidebarbrush,menurect.x,menurect.y,25,menurect.height);
g.drawline(partitionpen,new point(menurect.x+27,menurect.y+2),new point(menurect.x+27+(menurect.width-26),menurect.y+2));
}


}
}
}

//畫圖標
private void drawicon(graphics g)
{
if(this.checkistop()==false && this.iconpath!="" && this.isparent==false && this.checked==false)
{
try
{
bitmap iconimage=new bitmap(this.iconpath);
int imagewidth=iconimage.width;
int imageheight=iconimage.height;
if(imagewidth<=checkedrect.width && imageheight<=checkedrect.height)
{
int imagerecwidthmistake=checkedrect.width-imagewidth;
float mistake_2=(float)imagerecwidthmistake/2f;
g.drawimage(iconimage,checkedrect.x+(int)mistake_2,checkedrect.y,imagewidth,imageheight);
}
else
{
g.drawimage(iconimage,checkedrect);
}
}
catch
{
throw new iconnotfindexception("不能創建圖標,可能是路徑不能也可能是不支持此格式");
}
}
}
//由于再繪制的過程中選中的動作時需要執行兩次一樣的代碼 固在這里用了一個函數
private void drawitem_selected(graphics g,rectangle rec,string itemtext)
{
solidbrush tmpbrush;
if(this.enabled)
{
tmpbrush=fontdefaultbrush;
}
else
{
tmpbrush=menudefaultgrayedbrush;
}

//正常顯示
g.fillrectangle(menudefaultselectedbrush,rec);
g.drawrectangle(menudefaultpen,rec.x,rec.y,rec.width-1,rec.height-1);
g.drawstring(itemtext,this.font,tmpbrush,rec.x+26,rec.y+4);

//顯示shortcut
string shortcut=this.getshortcut();
strformat.alignment=stringalignment.far;
g.drawstring(shortcut,this.font,tmpbrush,shortcutrect,strformat);
//畫圖標
this.drawicon(g);
}
}


//自定義異常
public class iconnotfindexception:applicationexception
{
public iconnotfindexception(string message)
:base(message)
{
}
}
}



商業源碼熱門下載www.html.org.cn

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德昌县| 玉山县| 赤壁市| 南溪县| 台中市| 临桂县| 瑞昌市| 桃源县| 新巴尔虎右旗| 临洮县| 溧阳市| 水城县| 旺苍县| 富平县| 镇雄县| 富宁县| 肥东县| 获嘉县| 平安县| 武宣县| 南靖县| 九龙城区| 衡水市| 中西区| 全州县| 平塘县| 潼南县| 韶山市| 海原县| 宣城市| 江永县| 石屏县| 贵港市| 商丘市| 自贡市| 巢湖市| 马尔康县| 遂川县| 博乐市| 长泰县| 阳谷县|