分享
 
 
 

自绘XP风格菜单

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

这是以前写的代码,自绘XP风格的菜单,硬盘坏了后以为没了,最后写的一个软件要自定义风格,“翻箱倒柜”的终于在我可爱的古董机^_^上找到了一个应用的例子。还是把它放到Blog上来,即可共享又可作为备用 :)

把主菜单的OnMeasureItem指向MeasureMainItem、OnAdvancedDrawItem指向DrawMainItem,子菜单的OnMeasureItem指向MeasureSubItem、OnAdvancedDrawItem指向DrawSubItem,调整颜色或使用默认颜色即可达到效果

//调整主菜单项尺寸

procedure MeasureMainItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);

begin

Width := Width + 6;

Height := Height + 2;

end;

//调整子菜单项尺寸

procedure MeasureSubItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);

begin

Width := Width + 20;

Height := Height + 2;

end;

//绘制主菜单内容

procedure DrawMainItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);

const

MainMenuBackColor : TColor = $DEEDEF;

MainMenuBorderColor : TColor = $DEEDEF;

MainMenuSelectedBackColor : TColor = $DFA988;

MainMenuSelectedBorderColor: TColor = $C08000;

MainMenuHotLightBackColor : TColor = $DEEDEF;

MainMenuHotLightBorderColor: TColor = $800080;

MainMenuGrayedBackColor : TColor = $DEEDEF;

var

BrushColor, PenColor: TColor;

TextRect: TRECT;

begin

if odGrayed in State then

begin

BrushColor := MainMenuGrayedBackColor;

PenColor := MainMenuGrayedBackColor;

end

else

if odHotLight in State then

begin //鼠标划过

BrushColor := MainMenuHotLightBackColor;

PenColor := MainMenuHotLightBorderColor;

end

else

if odSelected in State then

begin

BrushColor := MainMenuSelectedBackColor;

PenColor := MainMenuSelectedBorderColor;

end

else

begin

BrushColor := MainMenuBackColor;

PenColor := MainMenuBackColor;

if TMenuItem(Sender).Caption = '帮助(&H)' then ARect.Right := ARect.Right + 1600;

end;

ACanvas.Brush.Color := BrushColor;

ACanvas.Pen.Color := PenColor;

//ACanvas.FillRect(ARect);

ACAnvas.Rectangle(ARect);

ACanvas.Brush.Style := bsClear;

//绘出文字

if odGrayed in State then

ACanvas.Font.Color := clBtnShadow

else

ACanvas.Font.Color := clBlack;

SetRect(TextRect, ARect.left+10, ARect.top+3, ARect.right, ARect.bottom);

DrawText(ACanvas.Handle, PChar(TMenuItem(Sender).Caption), Length(TMenuItem(Sender).Caption),

TextRect, DT_LEFT);

end;

//绘制子菜单内容

procedure DrawSubItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);

const

SubMenuBackColor : TColor = $F7F8F9;

SubMenuBorderColor : TColor = $DEEDEF;

SubMenuSelectedBackColor : TColor = $EED2C1;

SubMenuSelectedBorderColor: TColor = $C08000;

SubMenuLineColor : TColor = $C8D0D4;

//SubMenuHotLightBorderColor: TColor = $C08000;

SubMenuGrayedBackColor : TColor = $F7F8F9;//$DEEDEF;

var

BrushColor, PenColor: TColor;

TextRect: TRECT;

str: String;

ImageList: TCustomImageList;

begin

if (odGrayed in State) and not(TMenuItem(Sender).IsLine) then

begin

BrushColor := SubMenuGrayedBackColor;

{if odSelected in State then

PenColor := SubMenuSelectedBorderColor

else}

PenColor := SubMenuGrayedBackColor;

end

else

if odSelected in State then

begin

BrushColor := SubMenuSelectedBackColor;

PenColor := SubMenuSelectedBorderColor;

end

else

begin

BrushColor := SubMenuBackColor;

PenColor := SubMenuBackColor;

end;

ACanvas.Brush.Color := BrushColor;

ACanvas.Pen.Color := PenColor;

ACAnvas.Rectangle(ARect);

if not(odSelected in State) or (odGrayed in State) then

begin

ACanvas.Brush.Color := SubMenuBorderColor;

ACanvas.FillRect(Rect(ARect.Left, ARect.Top, ARect.Left+20, ARect.Bottom));

end;

//绘文字和快捷键

if TMenuItem(Sender).IsLine then

begin

ACanvas.Brush.Color := SubMenuLineColor;

ACanvas.Pen.Color := SubMenuLineColor;

ACanvas.FillRect(Rect(ARect.Left+23, ARect.Top+(ARect.Bottom-ARect.Top) div 2-1,

ARect.Right-2, ARect.Top+(ARect.Bottom-ARect.Top) div 2));

end

else

begin

ACanvas.Brush.Style := bsClear;

if odGrayed in State then

ACanvas.Font.Color := clBtnShadow

else

ACanvas.Font.Color := clBlack;

str := TMenuItem(Sender).Caption;

SetRect(TextRect, ARect.Left+24, ARect.Top+3, ARect.Right, ARect.Bottom);

DrawText(ACanvas.Handle, PChar(str), Length(str), TextRect, DT_LEFT);

str := ShortCutToText(TMenuItem(Sender).ShortCut);

SetRect(TextRect, ARect.Left+24, ARect.Top+3, ARect.Right-10, ARect.Bottom);

DrawText(ACanvas.Handle, PChar(str), Length(str), TextRect, DT_RIGHT);

//

if TMenuItem(Sender).Checked then

begin

ACanvas.Font.Charset := DEFAULT_CHARSET;

ACanvas.Font.Name := 'Webdings';

if TMenuItem(Sender).RadioItem then

ACanvas.TextOut(ARect.Left+4, ARect.Top, '=')

else

begin

ACanvas.Font.Height := -16;

ACanvas.TextOut(ARect.Left+2, ARect.Top, 'a');

end;

end;

end;

//绘制图片

ImageList := TMenuItem(Sender).GetImageList;

if ImageList<>nil then

if (odSelected in State) and not(odGrayed in State) then

ImageList.Draw(ACanvas, ARect.left+2, ARect.Top+2, TMenuItem(Sender).ImageIndex)

else

ImageList.Draw(ACanvas, ARect.left+3, ARect.Top+3,

TMenuItem(Sender).ImageIndex, TMenuItem(Sender).Enabled);

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有