TCustomControl的子类如何处理方向键?处理WM——KEY消息怎么也找不到,看了一下帮助,说是Tab,方向键等都不产生键盘事件,晕啊。谁能帮帮忙?
当这个组件被聚焦的时候,按下左键画个元,右键画一个方!55555555555555~~~~~~~救火~~~~~~~~~~

解决方案 »

  1.   

    这个类我没用过,用Derect input 甚至可以有windows系统菜单键的消息。
      

  2.   

    "WM——KEY消息怎么也找不到"这句话说明它没有再次重载基类的此消息的处理函数呀,你可以顺着它的基类往上找。
      

  3.   

    procedure TRzLookupForm.EdtSearchKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      if ( Key = vk_Down ) or ( Key = vk_Up ) then
      begin
        if Key = vk_Down then
        begin
          if LstSelections.Row < LstSelections.RowCount - 1 then
            LstSelections.Row := LstSelections.Row + 1;
        end
        else
        begin
          if LstSelections.row > 0 then
            LstSelections.Row := LstSelections.row - 1;
        end;
        Key := 0;
      end;
    end;
      

  4.   

    楼上的代码在组件中没用,我把能想到的键盘事件都 override过了。不行~
      

  5.   

    procedure TForm2.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    begin
      if (Msg.message = WM_KEYDOWN) then
        ShowMessage(Format('%d', [Msg.wParam]));
    end;
    这个有用么?
      

  6.   

    没做过TCutomControl的子类,可《Windows程序设计》的210页的代码的 WM_KEYDOWN 处理了 VK_UP, VK_DOWN, VK_RIGHT和VK_RIGHT啊
      

  7.   

    子类化这个控件,再拦截WM_KEYDOWN消息不就OK了么

    SetWindowLong
      

  8.   

    帮你写了个,试试{ TMyControl }type
      TShapeType = (stNone, stEllipse, stRectangle);  TMyControl = class(TCustomControl)
      private
        FIsFocus: Boolean;
        FShapeType: TShapeType;
        FFillColor: TColor;
        procedure WMLButtonDown(var Message: TWMMButtonDown); message WM_LBUTTONDOWN;
        procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
        procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
        procedure WMKillFocus(var Message: TWMSetFocus); message WM_KILLFOCUS;
        procedure WMGetDlgCode(var Message: TMessage); message WM_GETDLGCODE;
      protected
        procedure Paint; override;
      public
        constructor Create(AOwner: TComponent); override;
      end;constructor TMyControl.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FFillColor := clWhite;
      TabStop := True;
    end;procedure TMyControl.Paint;
    var
      R: TRect;
    begin
      with Canvas do
      begin
        DrawFrameControl(Handle, ClientRect, DFC_BUTTON, DFCS_BUTTONPUSH);
        R := ClientRect;
        if FIsFocus then
        begin
          InflateRect(R, -2, -2);
          DrawFocusRect(R);
        end;    if FShapeType <> stNone then
        begin
          Canvas.Brush.Color := FFillColor;
          InflateRect(R, -2, -2);
          case FShapeType of
            stEllipse: Ellipse(R);
            stRectangle: Rectangle(R);
          end;
        end;
      end;
    end;procedure TMyControl.WMGetDlgCode(var Message: TMessage);
    begin
      Message.Result := DLGC_WANTARROWS or DLGC_WANTTAB;
    end;procedure TMyControl.WMKeyDown(var Message: TWMKeyDown);
    begin
      case Message.CharCode of
        VK_LEFT, VK_UP: FShapeType := stEllipse;
        VK_RIGHT, VK_DOWN: FShapeType := stRectangle;
        VK_TAB: FFillColor := RGB(Random(255), Random(255), Random(255));
      else
        Exit;
      end;
      Invalidate;
    end;procedure TMyControl.WMKillFocus(var Message: TWMSetFocus);
    begin
      FShapeType := stNone;
      FIsFocus := False;
      Invalidate;
    end;procedure TMyControl.WMLButtonDown(var Message: TWMLButtonDown);
    begin
      SetFocus;
    end;procedure TMyControl.WMSetFocus(var Message: TWMSetFocus);
    begin
      FIsFocus := True;
      FShapeType := stRectangle;
      Invalidate;
    end;
      

  9.   

    主要的看
    procedure TWinControl.CNKeyDown(var Message: TWMKeyDown);
    var
      Mask: Integer;
    begin
      with Message do
      begin
        Result := 1;
        UpdateUIState(Message.CharCode);
        if IsMenuKey(Message) then Exit;
        if not (csDesigning in ComponentState) then
        begin
          if Perform(CM_CHILDKEY, CharCode, Integer(Self)) <> 0 then Exit;
          Mask := 0;
          case CharCode of
            VK_TAB:
              Mask := DLGC_WANTTAB;
            VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:
              Mask := DLGC_WANTARROWS;
            VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:
              Mask := DLGC_WANTALLKEYS;
          end;
          if (Mask <> 0) and
            (Perform(CM_WANTSPECIALKEY, CharCode, 0) = 0) and
            (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and
            (GetParentForm(Self).Perform(CM_DIALOGKEY,
            CharCode, KeyData) <> 0) then Exit;
        end;
        Result := 0;
      end;
    end;
    这个就明白了
      

  10.   

    关键是:procedure WMGetDlgCode(var Message: TMessage); message WM_GETDLGCODE;
      

  11.   

    看来已经解决了哈,wx1452() 厉害,我刚才忘记SetFoucus了哈
      

  12.   

    恩!感谢myy()。那几个消息看起来真不错。我先看看。再次感谢!
      

  13.   

    procedure TMyControl.WMGetDlgCode(var Message: TMessage);
    begin
      Message.Result := DLGC_WANTARROWS or DLGC_WANTTAB;
    end;
    呵呵。爽死 !!!!!!!谢谢myy()。