delphi 我需要TPaintBox的关键事件

mlmc2os5  于 2023-02-08  发布在  其他
关注(0)|答案(2)|浏览(221)

我看到我的 Delphi 版本,没有TPaintBox的Key Events(OnKeyDown,OnKeyUp,OnKeyPress)事件。我想处理类似的东西。有人有这样的事件吗?

bhmjp9jg

bhmjp9jg1#

就像TLama说的,你需要从TCustomControl继承。但是你需要一些额外的代码来发布所有的键盘事件。你可以选择简单的方法从TPanel继承,因为TPanel已经公开了一个Canvas和一些键盘事件。
但下面的一些代码显示了如何创建和注册一个新控件,该控件发布了TCustomControl的属性并引入了一个新的OnPaint事件:
如果你创建一个新的Package,添加这个单元,并安装它,你将有一个新的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也是如此。一个缺点是您需要重画父控件以清除以前绘制的内容。在演示应用程序中,这很容易。我只是使窗体无效,而不是使控件无效。:p
如果不需要,可以从控件中删除WMEraseBkGndCreateParamsSetParent
小演示:在一个窗体上放一个标签。在它上面放一个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;
8wtpewkr

8wtpewkr2#

你也可以创建一个带有paintbox的帧(与alClient对齐),并根据需要重用该帧。TFrame是一个窗口控件,因此它包含所有键盘事件。它们不会发布,但你可以在代码中分配它们。

相关问题