网上到处都有
type
TMyComponent = class (TWinControl)
... 
protected 
procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
...
end;procedure TMyComponent.CMWantSpecialKey(var Message: TCMWantSpecialKey); begin
inherited;
// 我们只想处理向左方向键,其它的特殊键都给 Windows 处理
if Message.CharCode = VK_LEFT then
Message.Result := 1; 
end;这种做法,可是type下面都有个TForm1 = class(TForm),这个TMyComponent = class (TWinControl)
该写在哪里呢?写在TForm1 = class(TForm)的下面会出错。

解决方案 »

  1.   

    这样写:type
      TForm1 = class(TForm)
        Button1: TButton;
      private
        { Private declarations }
      public
        { Public declarations }
      end;type
      TMyComponent = class (TWinControl)
       protected
         procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
    end;var
      Form1: TForm1;implementation
    procedure TMyComponent.CMWantSpecialKey(var Message: TCMWantSpecialKey); begin
    inherited;
    // 我们只想处理向左方向键,其它的特殊键都给 Windows 处理
    if Message.CharCode = VK_LEFT then
    Message.Result := 1;
    end;
      

  2.   

    多谢liangqingzhi(老之) 的答复,按照您的做法,编译是通过了,可是好像拦截不到方向键。当窗口上有CheckBox,RadioButton这些可以聚焦的控件时,方向键就成了它们切换用了。
      

  3.   

    可以用Additional页的ApplicationEvents控件。
    例如拦截Memo控件的方向左键:procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    begin
      if (Msg.hwnd=Memo1.Handle)and(Msg.message=WM_KEYDOWN)and(Msg.wParam=VK_LEFT)
       then Handled:=True;
    end;
      

  4.   

    哈。多谢liangqingzhi(老之) 了。这招厉害!问题得到解决。谢谢!谢谢!!马上结贴给分