【问题标题】:Add keyboard events to TPaintBox component将键盘事件添加到 TPaintBox 组件
【发布时间】:2015-05-24 10:07:57
【问题描述】:

我在我的应用程序中使用TPaintBox。已经设置了几个鼠标事件处理程序:鼠标按下、鼠标抬起等。但是,我还想响应键盘输入:如果用户按下任何功能键,我想执行一个单独的过程(事件处理程序)而不是Mouse* 事件处理函数。但我还需要新程序中的鼠标位置。

我该如何编码,因为TPaintBox 不支持任何按键事件?

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  here some code
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  //  more code here
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  // here other code
end;

【问题讨论】:

    标签: delphi


    【解决方案1】:

    TPaintBox 不是来自TWinControl,而是来自TGraphicControl,这意味着它无法接收输入焦点,因此它没有对键盘事件作出反应的功能。

    可能的解决方案:

    • 实现 PaintBox 所在父窗体的OnKeyPress 事件并启用窗体的KeyPreview 属性。
    • 添加一个将特定按键设置为ShortCut 属性的操作,并实现其OnExecute 事件处理程序。 (另请参阅:When does a ShortCut fire?)。
    • 为 MainForm 或应用程序实现 OnShortCut 事件处理程序。
    • 将 PaintBox 放置并对齐到 TWinControl,并实现该容器的 OnKeyPress 事件。

    对于鼠标和键盘输入的组合,检查鼠标事件的Shift参数或使用Win32的GetKeyState()GetKeyboardState()函数。

    【讨论】:

    • 我会投票给最后一个选项,更具体地说:将颜料盒放在框架上。这样您就可以在 IDE 中使用设计器,并且仍然可以在项目中的其他表单上重用框架。
    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多