私のバージョンのdelphiには、TPaintBoxのキーイベント(OnKeyDown、OnKeyUp、OnKeyPress)イベントがありません。そのようなものを処理したいと思います。誰かがこれらのイベントのペイントボックスを持っていましたか?
2 に答える
TLama が言ったように、TCustomControl から継承する必要があります。ただし、すべてのキーボード イベントを発行するには、追加のコードが必要になります。TPanel はすでに Canvas と多数のキーボード イベントを公開しているため、簡単な方法を選択して TPanel から継承することができます。
ただし、TCustomControl のプロパティを公開し、新しい OnPaint イベントを導入する新しいコントロールを作成して登録する方法を示すコードを次に示します。
新しいパッケージを作成し、このユニットを追加してインストールすると、フォーカスを持つことができる新しい TGTPaintBox コントロールが作成されます (ただし、表示されません)。キーボード入力も取得できます。
unit uBigPaintbox;
interface
uses Windows, Classes, Messages, Controls;
type
TGTPaintBox = class(TCustomControl)
private
FOnPaint: TNotifyEvent;
protected
// Three methods below are for transparent background. This may not work that great,
// and if you don't care about it, you can remove them.
procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
procedure CreateParams(var Params: TCreateParams); override;
procedure SetParent(AParent: TWinControl); override;
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
public
property Canvas;
published
// Introduce OnPaint event
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
// Publish keyboard and mouse events.
property OnKeyPress;
property OnKeyDown;
property OnKeyUp;
property OnClick;
property OnDblClick;
property OnMouseUp;
property OnMouseDown;
property OnMouseMove;
// And some other behavioral property that relate to keyboard input.
property TabOrder;
property TabStop;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('GolezTrol', [TGTPaintBox]);
end;
{ TGTPaintBox }
procedure TGTPaintBox.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
end;
procedure TGTPaintBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
// Focus the control when it is clicked.
if not (csDesigning in ComponentState) and CanFocus then
SetFocus;
end;
procedure TGTPaintBox.Paint;
begin
inherited;
// Call paint even if it is assigned.
if Assigned(FOnPaint) then
FOnPaint(Self);
end;
procedure TGTPaintBox.SetParent(AParent: TWinControl);
var
NewStyle: Integer;
begin
inherited;
if AParent = nil then
Exit;
// Make sure the parent is updated too behind the control.
NewStyle := GetWindowLong(AParent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN;
SetWindowLong(AParent.Handle, GWL_STYLE, NewStyle);
end;
procedure TGTPaintBox.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
begin
SetBkMode(Msg.DC, TRANSPARENT);
Msg.Result := 1;
end;
end.
PaintBox も同様であるため、コントロールを透明にするためにいくつかの機能を追加しました。1 つの欠点は、以前に描画されたコンテンツをクリアするために、親を再描画する必要があることです。デモ アプリケーションでは、これは簡単です。コントロールではなくフォームを無効にするだけです。:p
必要ない場合は、コントロールからWMEraseBkGnd
,CreateParams
とを削除できます。SetParent
小さなデモ: フォームにラベルを付けます。その上に TGTPaintBox を置き、少し大きくします。次に、タイマーとその他のコントロールを追加します。
GTPaintBox の TabStop プロパティを に設定してくださいTrue
。
次に、次のイベントを実装します。
// To repaint the lot.
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Invalidate;
end;
// Capture key input and save the last entered letter in the tag.
procedure TForm1.GTPaintBox1KeyPress(Sender: TObject; var Key: Char);
begin
if Key in ['a'..'z'] then
TGTPaintBox(Sender).Tag := Integer(Key);
end;
// Paint the control (this is called every second, when the timer invalidates the form
procedure TForm1.GTPaintBox1Paint(Sender: TObject);
var
PaintBox: TGTPaintBox;
begin
PaintBox := TGTPaintBox(Sender);
// Draw a focus rect too. If you want the control to do this, you would normally
// implement it in the control itself, and make sure it invalides as soon as it
// receives or loses focus.
if PaintBox.Focused then
PaintBox.Canvas.DrawFocusRect(PaintBox.Canvas.ClipRect);
// It just draws the character that we forced into the Tag in the KeyPress event.
PaintBox.Canvas.TextOut(Random(200), Random(200), Char(PaintBox.Tag));
end;
ペイントボックスが配置されたフレームを作成し ( に位置合わせalClient
)、必要に応じてそのフレームを再利用することもできます。TFrame
はウィンドウ コントロールなので、すべてのキーボード イベントがあります。これらは公開されていませんが、コードで割り当てることができます。