我使用Windows API和Parts and States获得了一些乐趣,并了解如何将控件绘制到画布上。
经过大量的试验和错误,我设法提出了这个程序,它会在画布上画一个按钮:
type
TButtonState = (bsDefault, bsDisabled, bsHot, bsNormal, bsPressed);
procedure DrawButton(ACanvas: TCanvas; X, Y, AWidth, AHeight: Integer;
AFont: TFont; Caption: string; ButtonState: TButtonState);
var
Size: TSize;
R: TRect;
H: HTHEME;
begin
Size.cx := AWidth;
Size.cy := AHeight;
R := Rect(X, Y, X + AWidth, Y + AHeight);
if Winapi.uxTheme.UseThemes then
begin
H := OpenThemeData(0, 'BUTTON');
if H <> 0 then
try
ACanvas.Brush.Style := bsClear;
if AFont <> nil then
begin
ACanvas.Font.Assign(AFont);
end;
case ButtonState of
bsDefault:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DEFAULTED, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DEFAULTED, R, nil);
end;
bsDisabled:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DISABLED, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DISABLED, R, nil);
//ACanvas.Font.Color := $00838383; //todo get actual disabled font color
end;
bsHot:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_HOT, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_HOT, R, nil);
end;
bsNormal:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_NORMAL, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_NORMAL, R, nil);
end;
bsPressed:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_PRESSED, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_PRESSED, R, nil);
end;
end;
// draw the button caption
DrawText(ACanvas.Handle, PChar(Caption), Length(Caption), R, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
finally
CloseThemeData(H);
end
end
else
begin
// draw button in classic theme?
end;
end;
如果我这样称呼这个程序:
procedure TForm1.FormPaint(Sender: TObject);
begin
DrawButton(Image1.Canvas, 10, 10, 75, 25, Form1.Font, 'Normal', bsNormal);
DrawButton(Image1.Canvas, 10, 40, 75, 25, Form1.Font, 'Default', bsDefault);
DrawButton(Image1.Canvas, 10, 70, 75, 25, Form1.Font, 'Disabled', bsDisabled);
DrawButton(Image1.Canvas, 10, 100, 75, 25, Form1.Font, 'Hot', bsHot);
DrawButton(Image1.Canvas, 10, 130, 75, 25, Form1.Font, 'Pressed', bsPressed);
end;
结果是我的预期,就像任何Windows按钮控件一样:
当我正在玩这个程序的时候,我很快意识到按钮没有标题,我看不到添加标题的方法,而不是自己在按钮上面绘制标题。正如你所看到的,&#34;残疾人&#34;按钮仍显示默认字体颜色,禁用控件通常具有不同的颜色,以帮助显示控件已禁用。标准Windows主题下的禁用字体颜色为$00838383
(使用屏幕颜色选择器找到),但硬编码值绝不是一个好主意,因为这些值通常对每个主题都是唯一的。
我的问题有几个部分,当使用Winows API绘制按钮时,我们是否必须自己手动绘制标题?如果是这样,我如何确保绘制正确的字体名称,样式和大小等以确保按钮与系统范围绘制的按钮相同?
如果未启用主题,我应该如何以经典Windows样式绘制按钮?
答案 0 :(得分:6)
你应该自己画文字。您也可以使用主题api来绘制文本。例如:
...
bsDisabled:
begin
GetThemePartSize(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DISABLED, nil, TS_DRAW, Size);
DrawThemeBackground(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DISABLED, R, nil);
DrawThemeText(H, ACanvas.Handle, BP_PUSHBUTTON, PBS_DISABLED, PChar(Caption),
Length(Caption), DT_CENTER or DT_VCENTER or DT_SINGLELINE, 0, R);
//ACanvas.Font.Color := $00838383; //todo get actual disabled font color
end;
...
由于您将使用适当的状态调用api,请在case分支中包含该调用并删除对DrawText的调用。
您可以将DrawFrameControl用于经典风格。例如:
DrawFrameControl(ACanvas.Handle, R, DFC_BUTTON, DFCS_BUTTONPUSH);