0

我看到我的 delphi 版本,没有用于 TPaintBox 的 Key Events(OnKeyDown, OnKeyUp, OnKeyPress) 事件。我想处理类似的事情。有人有这些事件的油漆盒吗?

4

2 回答 2

10

就像 TLama 所说,您需要从 TCustomControl 继承。但是您需要一些额外的代码来发布所有键盘事件。您可以选择简单的方法并从 TPanel 继承,因为 TPanel 已经公开了 Canvas 和许多键盘事件。

但是这里有一些代码来展示如何创建和注册一个新控件,该控件发布了 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 也是如此。一个缺点是您需要重新绘制父级以清除先前绘制的内容。在演示应用程序中,这很容易。我只是使表单而不是控件无效。: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;
于 2011-12-02T10:53:00.513 回答
4

您还可以创建一个带有油漆盒的框架(与 对齐alClient)并根据需要重新使用该框架。TFrame是一个窗口控件,因此它具有所有键盘事件。它们未发布,但您可以在代码中分配它们。

于 2011-12-02T11:10:37.713 回答