unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, AxCtrls, OleCtrls, VCF1, Chartfx3;type
  TForm1 = class(TForm)
    Chartfx1: TChartfx;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure WMSysCommand(var Msg: TWMSysCommand);message WM_SYSCOMMAND;
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
  if (Msg.CmdType=SC_MAXIMIZE) then
  begin
    SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
    self.WindowState:=wsMaximized;
  end
  else
     DefaultHandler(Msg);
  Application.ProcessMessages;
end;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_ESCAPE then
  begin
     showmessage('Ok');
     SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) or WS_CAPTION);
     WindowState:=wsNormal;
  end;
end;end.
我已将表单的KeyPreView设置为了True,并且chartfx1的align属性值为alClient,
如果我将Chartfx1控件换成Memo控件或其他含有onkeypress的事件的控件,当表单
全屏时,按ESC键,能将表单恢复.但如果换成如chartfx1和其他不含
有onKeypress,onKeydown等事件的控件时,当按ESC键就没有用.请问各位大侠,有没有
其他的方法当表单上含有chartfx1时,并且chartfx1的align属性值为alClient,
表单全屏时,按ESC键能恢复窗口大小?谢谢!

解决方案 »

  1.   

    TFORM的KEYPRVIEW属性设成TRUE后,
    窗体里面的控件接受按键消息,然后层层往上先传到TFORM, 这样才实现的
    KEY PROVIEW。但是很多控件没有实现或不能实现,比如说嵌入的是ACTIVEX控件。要处理也容易,
    在TFORM的WindowProc方式可以拦到消息,自己处理的。
      

  2.   

    用APPLICATION EVENTS组件也是一样的。procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    var
      vkey:integer;
    begin  if Msg.message= WM_KEYDOWN then
      begin
        vkey:=Msg.wParam;
        if vkey=VK_ESCAPE then
        begin
          Form1.WindowState:=wsMinimized;      Handled:=true
        end
      end;end;
      

  3.   

    哈大侠,将你给的程序段放入我的程序,还是没有用.
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleCtrls, Chartfx3;type
      TForm1 = class(TForm)
        Chartfx1: TChartfx;
      private
        procedure WMSysCommand(var Msg: TWMSysCommand);message WM_SYSCOMMAND;
        procedure ApplicationEvents1Message(var msg: tagMsg;
          var Handled: Boolean);
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    var
      vkey:integer;
    begin  if Msg.message= WM_KEYDOWN then
      begin
        vkey:=Msg.wParam;
        if vkey=VK_ESCAPE then
        begin
          showmessage('ok');
          SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) or WS_CAPTION);
          Form1.WindowState:=wsNormal;      Handled:=true
        end
      end;end;procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
    begin
      if (Msg.CmdType=SC_MAXIMIZE) then
      begin
        SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
    //    ShellTreeView1.Visible:=False;
     //   ToolBar1.Visible:=False;
    //    Splitter1.Visible:=False;
        self.WindowState:=wsMaximized;
      end
      else
         DefaultHandler(Msg);
    end;end.
      

  4.   

    我用ONmessage属性做成了.可以捕捉到esc键,谢谢!
      

  5.   

    我真服你了。
    你仔细看我的话啊,叫你放一个APPLICATION EVENTS组件进去。
    ApplicationEvents1Message是它的ONMESSAGE事件处理程序啊。
      

  6.   

    实在不行,用全局HotKey算了。
      

  7.   

    //看来只能给你个进程内热键的单元了:
    {
     This unit provides two functions for an application
     to add and remove in-process hotkeys. Written by Stanely. 2002-11-14. [email protected]
     
     Calls and Return Values:
       When using it,just call the function "AddHotKey" at any time to add a hotkey.
       If succeeds,it returns a TPHotKeyRec object reference,otherwise it retruns nil;
       Call the function "RemoveHotkey" to delete a registered hotkey whenever you do not need it any longer.
       If succeeds the return value is true,else it is false. Parameters:
       1.VKey: virtual key code.
       2.Modifiers: set of ShiftState.
       3.NotifyProc: Specify a NotifyProc to be called back when the registered hotkey were pressed. 
       4.WinControl: Specify a Wincontrol to accept the keypress event.
         If it is nil then any wincontrol on the form can accept then keypress event.
       5.EatMessage: Specify if the message can be passed down. 
     Note:
       1.This unit only provides in-process Hotkey Registeration.
         So,do not try to export any function of this unit.
         For instance,you cannot export the two functions in DLL files,that's useless.
       2.When you are trying to add a hotkey whose keycode is 'a' ,pass ord('A') to the VKey paramenter.
         Any Keycode must be passed as its capitalization.
       3.This unit can check the registered hotkeys in all threads of the process.
    }unit ProcessHotKey_San;interface
    uses
    windows,sysutils,classes,controls,SyncObjs,debug_san;type
    TPHotKeyRec=class
    public
      VKeyCode:integer;
      Modifiers:TShiftState;
      NotifyProc:TNotifyEvent;
      EatMessage:boolean;
      WinControl:TWincontrol;
    end;
    //////////////////////////////////
    function AddHotKey(VKey:integer;Modifiers:TShiftState;NotifyProc:TNotifyEvent;
    WinControl:TWinControl=nil;EatMessage:boolean=true):TPHotKeyREC;
    function RemoveHotKey(VKey:integer;Modifiers:TShiftState):boolean;
    implementation
    var
    flist:tlist=nil;
    hhk:hhook=0;
    cs:tcriticalsection=nil;////////////
    procedure initproc;
    begin
      flist:=tlist.Create;
      cs:=tcriticalsection.create;
    end;
    procedure finalproc;
    var
    i:integer;
    begin  unhookwindowshookex(hhk);
      cs.Free;
      for i:=0 to flist.Count-1 do
      begin
        tphotkeyrec(flist.Items[i]).Free;
      end;
      flist.Free;
    end;
    /////////////////function KeyDataToShiftState(KeyData: Longint): TShiftState;
    const
      AltMask = $20000000;
    begin
      Result := [];
      if GetKeyState(VK_SHIFT) < 0 then Include(Result, ssShift);
      if GetKeyState(VK_CONTROL) < 0 then Include(Result, ssCtrl);
      if KeyData and AltMask <> 0 then Include(Result, ssAlt);
    end;
    ////////////////
    function finditem(vkey:integer;Modifiers:TShiftState):integer;
    var
    i:integer;
    begin
      result:=-1;
      try
      for i:=0 to flist.Count-1 do
      begin
        if (tphotkeyrec(flist.Items[i]).VKeyCode=vkey)and
        (tphotkeyrec(flist.Items[i]).Modifiers=modifiers)then
        begin
          result:=i;
          break;
        end;
      end;
      except
      end;
    end;
    //////////////
    function HookProc(ncode:smallint;wparam:integer;lparam:integer):integer;stdcall;
    var
    rec:tphotkeyrec;
    i:integer;
    begin  
      result:=0;
      rec:=nil;
     // msg2(wparam);
      cs.Enter;
      try
      i:=finditem(wparam,KeyDataToShiftState(lparam));
     // msg2(i);  if i>=0 then
      begin
      rec:=flist.items[i];
      ////////////////////
      if rec.EatMessage then
      begin
        result:=-1;    
      end;
      if assigned(rec.NotifyProc) then
      begin   if (rec.WinControl=nil) then
       begin
         if getkeystate(wparam)<0 then
           rec.NotifyProc(rec);
       end
       else
       begin
         if (getkeystate(wparam)<0)and(rec.WinControl.Focused) then
           rec.NotifyProc(rec);
       end;
      end;
      end;
     finally
      cs.Leave;
      if ncode<0 then
      begin
        if (rec<>nil) then
        begin
         if not rec.EatMessage then
          result:=callnexthookex(hhk,ncode,wparam,lparam);
        end
        else
          result:=callnexthookex(hhk,ncode,wparam,lparam);
      end;   
     end;
    // msg2(result);
    end;
    //////////////
    function RemoveHotKey(VKey:integer;Modifiers:TShiftState):boolean;
    var
    i:integer;
    begin
      result:=false;
      cs.Enter;
      try
      i:=finditem(vkey,modifiers);
      if i>=0 then
      begin
        tphotkeyRec(flist.Items[i]).Free;
        flist.Delete(i);
      end;
      finally;
      if flist.Count<=0 then
        unhookwindowshookex(hhk);
      cs.Leave;  
      end;end;
    function AddHotKey(VKey:integer;Modifiers:TShiftState;NotifyProc:TNotifyEvent;WinControl:TWinControl=nil;
             EatMessage:boolean=true):TPHotKeyREC;
    var
    i:integer;
    begin
      result:=nil;
      cs.Enter;
      try
      i:=finditem(vkey,modifiers);  if flist.Count<=0 then
        hhk:=setwindowshookex(WH_KEYBOARD,@hookproc,hinstance,0);
      if (hhk<>0) then
      begin
        if (i>=0)then
        begin
          result:=flist.items[i];
        end
        else
        begin
       // msg2(hhk);
        result:=tphotkeyrec.Create;
        result.VKeyCode:=vkey;
        result.Modifiers:=modifiers;
        result.NotifyProc:=notifyproc;
        result.EatMessage:=eatmessage;
        result.WinControl:=wincontrol;
        flist.Add(result);
        end;
      end;
      finally
      cs.Leave;
      end;
    end;///////////////////////////
    initialization
      initproc;
    finalization
      finalproc;
    end.
      

  8.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure test(sender:tobject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    uses
    processhotkey_san;
    {$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
    addhotkey(vk_escape,[],test);
    end;procedure TForm1.test(sender: tobject);
    begin
     beep;
    end;end.
    ///////////////////
    //按esc就会有效果了。
    //祝你愉快!
      

  9.   

    十分感谢各位大侠,哈大侠,十分感谢您再次提醒,是的,ApplicationEvents的onmessage事件可以.谢谢!