显示'...'的肯定不是按钮,虽然按钮也可以实现
我贴一段代码
见Cnpack中的CnEdit,就可以
procedure TCnEdit.WMPaint(var Message: TWMPaint);
var
Left: Integer;
MArgins: TPoint;
R: TRect;
DC: HDC;
PS: TPaintStruct;
S: string;
Flags: Integer;
W: Integer;
begin
if FCanvas = nil then
begin
FCanvas := TControlCanvas.Create;
FCanvas.Control := Self;
end;
DC := Message.DC;
if DC = 0 then DC := beginPaint(Handle, PS);
FCanvas.Handle := DC;
try
FCanvas.Font := Font;
with FCanvas do
begin
//设置控件的范围
if (fLinkStyle <> lsNormal) then
SetRect(R, ClientWidth - fButtonWidth, 0, ClientWidth, ClientHeight)
else
begin
R := ClientRect;
if not (NewStyleControls and Ctl3D) and (BOrderStyle = bsSinGle) then
begin
Brush.Color := clWindowFrame;
FrameRect(R);
InflateRect(R, -1, -1);
end;
Brush.Color := Color;
end;
//是否是密码型
S := Text;
if PasswordChAr <> #0 then
FillChAr(S[1], Length(S), PasswordChAr);
//画文字
MArgins := GetTextMArgins;
case FAlignment of
taLeftJustify: Left := MArgins.X;
tArightJustify: Left := ClientWidth - TextWidth(S) - MArgins.X - 1;
else
Left := (ClientWidth - TextWidth(S)) div 2;
end;
TextRect(R, Left, MArgins.Y, S);
if (fLinkStyle <> lsNormal) then //画按钮
begin
Flags := 0;
if FPressed then
Flags := BF_FLAT;
DrawEdge(DC, R, EDGE_RAISED, BF_RECT or BF_MIDDLE or Flags);
Flags := ((R.Right - R.Left) shr 1) - 1 + Ord(fPressed);
W := 2;
PatBlt(DC, R.Left + Flags, R.Top + Round(ClientHeight / 2) - 1, W, W, BLACKNESS);
PatBlt(DC, R.Left + Flags - (W * 2), R.Top + Round(ClientHeight / 2) - 1, W, W, BLACKNESS);
PatBlt(DC, R.Left + Flags + (W * 2), R.Top + Round(ClientHeight / 2) - 1, W, W, BLACKNESS);
ExcludeClipRect(DC, R.Left, R.Top, R.Right, R.Bottom);
PaintWindow(DC);
end;
end;
finally
FCanvas.Handle := 0;
if Message.DC = 0 then endPaint(Handle, PS);
end;
end;