unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    lbl1: TLabel;
    btn1: TButton;
  private
    { Private declarations }
  public
    procedure WndProc(var Message: TMessage);override;
  end;var
  Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.WndProc(var Message: TMessage);
begin
  inherited;
  if Message.Msg = WM_KEYDOWN then
  begin
    lbl1.Caption := IntToStr(Message.WParam);
  end;
end;end.如果窗体上没有按钮则是可以接受的,谁知道为什么?怎样才能在WndProc处理WM_KEYDOWN消息?

解决方案 »

  1.   

    KeyPreview=True不起作用,谁还知道点办法?
      

  2.   

    哦,按钮啊,那应该是消息发送到按钮上了,不是在Form1上。你要这样:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        lbl1: TLabel;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        procedure MsgPro(var msg: TMsg; var Handled: boolean);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.MsgPro(var msg: TMsg; var Handled: boolean);
    begin
      if msg.message = WM_KEYDOWN then
      begin
        lbl1.Caption := IntToStr(msg.wParam);
      end;
      Handled:=false;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMessage:= MsgPro;
    end;end.
      

  3.   

    但是我必须要在wndproc里面处理,因为我要子类化这个窗口,还有办法吗?
      

  4.   

    在wndproc里面处理CMCHILDKEY,试试