最好的解决方案是将您的控件(图像)放入处理对话框键的窗口(控件)中,然后确保该控件在适当的时间接收键盘焦点。
好吧,老实说,您似乎通过移动图形控件来绘制/动画事物。最好的方法是创建一个自定义控件(从 降序TCustomControl
)并手动绘制东西(或者,可能有图形子控件)。然后您可以轻松地使此控件响应箭头键。
该控件执行此操作:
unit WindowContainer;
interface
uses
SysUtils, Windows, Messages, Classes, Controls;
type
TWindowContainer = class(TCustomControl)
private
protected
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
public
constructor Create(AOwner: TComponent); override;
published
property OnStartDrag;
property OnStartDock;
property OnUnDock;
property OnClick;
property OnCanResize;
property OnAlignPosition;
property OnAlignInsertBefore;
property OnResize;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnMouseUp;
property OnMouseMove;
property OnMouseLeave;
property OnMouseEnter;
property OnMouseDown;
property OnMouseActivate;
property OnKeyUp;
property OnKeyPress;
property OnKeyDown;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Rejbrand 2009', [TWindowContainer]);
end;
{ TWindowContainer }
constructor TWindowContainer.Create(AOwner: TComponent);
begin
inherited;
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csSetCaption, csDoubleClicks, csReplicatable, csPannable,
csFramed];
end;
procedure TWindowContainer.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
inherited;
Message.Result := Message.Result or DLGC_WANTCHARS or DLGC_WANTARROWS
or DLGC_WANTTAB or DLGC_WANTALLKEYS;
end;
end.
示例演示 EXE
要尝试此控件,只需将形状/图像放入其中,并使用OnKeyDown
此控件的事件。此外,请确保将焦点设置为此控件,例如当用户单击它时:
procedure TForm1.WindowContainer1Click(Sender: TObject);
begin
WindowContainer1.SetFocus;
end;
现在,通过移动控件来制作动画是不好的。相反,您应该手动绘制。将此答案与此答案中的代码结合起来,您就完成了。