程序最小化时时托盘状态,如何实现按哪一个键就弹出主窗体。。网上资料很多,试了下,没一个能成功的。
希望各位不要复制网上的。谢谢

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, CoolTrayIcon;type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
        procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
      public    HotKeyId:  Integer;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.WMHotKey(var Msg: TWMHotKey);
    begin
      if Msg.HotKey = HotKeyId then
      begin
         ShowWindow(Application.Handle, SW_RESTORE);
         SetForegroundWindow(Application.Handle);
      end;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
       HotKeyId := GlobalAddAtom(PChar('F2'));
       RegisterHotKey(Handle,HotKeyId,0,VK_F2);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
       UnRegisterHotKey(handle,HotKeyId);
       GlobalDeleteAtom(HotKeyId);end;end.
      

  2.   


    {
    声明一个全局变量:
    }
    var
      HotKeyId: Integer; 
    {
    在窗口的create事件中,加入以下代码

     HotKeyId := GlobalAddAtom('MyHotKey') - $C000; 
    RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F8); {注册了一个热键:ALT+F8}
    {
    注: HotKeyId的合法取之范围是0x0000到0xBFFF之间, GlobalAddAtom函数得到的值 
    在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。在程序头部分的private段中加入声明  
    }
    procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY; 
    {
    然后在程序中加入如下代码: 
    }
    procedure Tfmain.HotKeyDown(var Msg: Tmessage); 
    begin 
      if (Msg.LparamLo = MOD_ALT) AND (Msg.LParamHi = VK_F8) then {假设热键为ALT+F8 }
      begin 
        ShowMessage('F8'); 
      end; 
    end; {
    在窗口的close事件中加入
    } UnRegisterHotKey(handle, HotKeyId); {注销HotKey, 释放资源。}