1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| unit TCustomControl_Unit;
interface
uses
Windows, Messages, SysUtils, StrUtils, Classes, Types, Controls, Graphics,
Dialogs;
type
TMyControl = class(TCustomControl)
private
fLastClick: TPoint;
fKey: Char;
procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
published
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
property OnClick;
property OnMouseDown;
property OnKeyPress;
property LastClick: TPoint read fLastClick write fLastClick;
property Key: Char read fKey write fKey;
end;
implementation
constructor TMyControl.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
if aOwner is TWinControl then
Parent := TWinControl(aOwner);
Width := 300;
Height := 200;
TabStop := True;
ControlStyle := ControlStyle + [csClickEvents, csCaptureMouse,
csAcceptsControls];
Color := clWindow;
Invalidate;
end;
destructor TMyControl.Destroy;
begin
inherited Destroy;
end;
procedure TMyControl.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if Button = mbLeft then
begin
fLastClick := Point(X, Y);
if Assigned(OnMouseDown) then
OnMouseDown(Self, Button, Shift, X, Y);
if Assigned(OnClick) then
OnClick(Self);
end;
end;
procedure TMyControl.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
inherited;
Msg.Result := Msg.Result or DLGC_WANTARROWS or DLGC_WANTCHARS;
end;
procedure TMyControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
ShowMessage('KeyDown=' + IntToStr(Key));
case Key of
VK_LEFT:
begin
//
end;
VK_RIGHT:
begin
//
end;
VK_UP:
begin
//
end;
VK_DOWN:
begin
//
end;
end;
end;
procedure TMyControl.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
fKey := Key;
if Assigned(OnKeyPress) then
OnKeyPress(Self, Key);
end;
procedure TMyControl.Paint;
begin
Canvas.Brush.Color := clRed;
Canvas.Pen.Color := clBlack;
Canvas.Pen.Width := 3;
Canvas.FillRect(Canvas.ClipRect);
Canvas.Rectangle(Canvas.ClipRect);
end;
end. |
Partager