雨过天晴 发表于 2011-3-29 09:17:44

请问delphi如何注册多个热键?

我的代码如下。unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls, ComCtrls, Spin,ShellApi;
constNOTIFYEVENT=WM_USER+100;
type
TForm1 = class(TForm)
    HotKey0: THotKey;
    HotKey1: THotKey;
    HotKey2: THotKey;
    procedure FormCreate(Sender: TObject);
private
    { Private declarations }
      aatom:atom;
      abcd:atom;
procedure hotkey(var msg:tmessage);
message wm_hotkey;
public
    { Public declarations }
end;

var
Form1: TForm1;
    MyHwnd:hwnd;//窗口
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
myhot:string;
begin
RegisterHotKey(handle,aatom,MOD_ALT,VK_F10); //注册热键ALT-F10
//RegisterHotKey(handle,abcd,MOD_ALT,VK_F11);
end;
procedure TForm1.hotkey(var msg:tmessage);

begin
if (msg.LParamHi=VK_F10) and (msg.LParamLo=MOD_ALT) then
    begin
      MyHwnd:=GetForegroundwindow;      //这里是不是获取热键Alt+F10按下后的当前窗口的句柄呢?
          begin
            SetForegroundWindow(handle);
            messagebox(handle,'你已经按下热键Alt+F10','成功', MB_OK);
          end;
    end;
if (msg.LParamHi=VK_F11) and (msg.LParamLo=MOD_ALT) then
      SetForegroundWindow(handle);
      messagebox(handle,'你已经按下热键Alt+F11','成功', MB_OK);
    end;

end.

clarexxg 发表于 2011-3-29 09:18:19

你的原子aatom、abcd还需要注册
aatom:=globaladdatom('原子唯一识别码字符串'); //每个注册的热键的原子必须不同

关闭辅助时必须注销原子和热键
UnregisterHotKey(handle,aatom);
globalDeleteatom(aatom);

bimuyu 发表于 2011-3-29 09:18:41

不错,学习一下,利用多原子实现多热键
页: [1]
查看完整版本: 请问delphi如何注册多个热键?