没有关键事件的Delphi PaintBox

时间:2011-12-02 09:40:33

标签: delphi events key paint paintbox

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

2 个答案:

答案 0 :(得分:9)

像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
如果您不需要,可以从控件中删除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;

答案 1 :(得分:4)

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