帮你做了个例子
unit MyControl;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Dialogs, Graphics;
type
TMyControl = class(TCustomControl)
private
{ Private declarations }
FCaption: array[1..20] of Char;
procedure DrawFocus;
protected
{ Protected declarations }
procedure Paint; override;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMSetFocus); message WM_KILLFOCUS;
procedure WMKeyDown(var Msg: TWMKeyDown); message WM_KEYDOWN;
procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('GYB', [TMyControl]);
end;
{ TMyControl }
procedure TMyControl.DrawFocus;
var
R: TRect;
begin
R := ClientRect;
InflateRect(R, -2, -2);
DrawFocusRect(Canvas.Handle, R);
end;
procedure TMyControl.Paint;
var
R: TRect;
begin
inherited;
R := ClientRect;
DrawFrameControl(Canvas.Handle, R,
DFC_BUTTON, DFCS_BUTTONPUSH);
InflateRect(R, -2, -2);
Canvas.Brush.Color := clSkyBlue;
Canvas.FillRect(R);
SetBkMode(Canvas.Handle, TRANSPARENT);
DrawText(Canvas.Handle, @FCaption[1], -1, R,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
if GetFocus = Handle then
DrawFocus;
end;
procedure TMyControl.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
Msg.Result := DLGC_WANTARROWS or DLGC_WANTTAB;
end;
procedure TMyControl.WMKeyDown(var Msg: TWMKeyDown);
var
R: TRect;
W, H: Integer;
begin
GetKeyNameText(Msg.KeyData, @FCaption[1], 20);
W := Canvas.TextWidth(FCaption);
H := Canvas.TextHeight(FCaption);
R.Left := (Width - W) div 2;
R.Top := (Height - H) div 2;
R.Right := R.Left + W;
R.Bottom := R.Top + H;
InvalidateRect(Handle, @R, True);
end;
procedure TMyControl.WMKillFocus(var Message: TWMSetFocus);
begin
DrawFocus;
end;
procedure TMyControl.WMLButtonDown(var Message: TMessage);
begin
Inherited;
if GetFocus <> Handle then
SetFocus;
end;
procedure TMyControl.WMSetFocus(var Message: TWMSetFocus);
begin
DrawFocus;
end;
end.