XPMenu是一个不错的程序界面效果控件,但它也存在不少不足之处。我最近又对它作了一点修改。
原因是我在程序里有一个ToolButton,其Style=tbsButton,当Down=True时,XPMenu绘制的效果效果跟Down=False时一样,也就是说根本看不出它是按下的。当把Style改为tbsCheck后,却能显示效果,但是底色很深。
这个按钮来我是用来表示某个面板是否可以显示的,我希望它像OfficeXP的工具按钮那样,当工具条显示时,在按钮上画个边框即可,而不是以很深的底色显示。
XPMenu绘制工具栏按钮是由TXPMenu.ToolBarDrawButton函数完成,原型为如下:
procedure TXPMenu.ToolBarDrawButton(Sender: TToolBar; Button: TToolButton; State: TCustomDrawState; var DefaultDraw: Boolean);
在函数内由以下代码决定按钮是否显示边框,以及用什么颜色作底色:
if (cdsHot in State) then
begin
if (cdsChecked in State) or (Button.Down) or (cdsSelected in State) then
ACanvas.Brush.Color := FCheckedAreaSelectColor
else
ACanvas.brush.color := FBSelectColor;
HasBorder := true;
HasBkg := true;
end;
if ((cdsChecked in State) and not (cdsHot in State)) then
begin
ACanvas.Brush.Color := FCheckedAreaColor;
HasBorder := true;
HasBkg := true;
end;
if (cdsIndeterminate in State) and not (cdsHot in State) then
begin
ACanvas.Brush.Color := FBSelectColor;
HasBkg := true;
end;
它忽略掉了非cdsHot、非cdsChecked状态下按钮的Down=True的情况的处理。因此只要加上相应的判断,并让HasBorder=true即可达到我希望的效果。修改后代码如下:
if (cdsHot in State) then
begin
if (cdsChecked in State) or (Button.Down) or (cdsSelected in State) then
ACanvas.Brush.Color := FCheckedAreaSelectColor
else
ACanvas.brush.color := FBSelectColor;
HasBorder := true;
HasBkg := true;
end;
if ((cdsChecked in State) and not (cdsHot in State)) then
begin
ACanvas.Brush.Color := FCheckedAreaColor;
HasBorder := true;
HasBkg := true;
end;
{Modify: Conch 2005-3-10 在Down=true的按钮上画出边框}
if (Button.Down) and not (cdsHot in State) then
begin
HasBorder := true;
HasBkg := false;
end;
//Conch
if (cdsIndeterminate in State) and not (cdsHot in State) then
begin
ACanvas.Brush.Color := FBSelectColor;
HasBkg := true;
end;