Register HotKey

Register HotKey

代码
{*******************************************************}
{ }
{ Module Name: unitHotKey.pas }
{ Creation Date 2010-12-30 }
{ }
{*******************************************************}
unit unitHotKey;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1
= class(TForm)
btnAtom: TButton;
btnGAtom: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnAtomClick(Sender: TObject);
procedure btnGAtomClick(Sender: TObject);
private
{ Private declarations }
FKeyHandle:
array [1..4] of ATOM;
public
{ Public declarations }
procedure WMHotKeyHandle(var msg: Tmessage); message WM_HOTKEY;
//procedure WMHotKeyHandle(var msg: TWMHotKey); message WM_HOTKEY;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 4 do
FKeyHandle[i] :
= GlobalAddAtom(PChar('MyTestHotKey' + IntToStr(i)));
RegisterHotKey(Handle, FKeyHandle[
1], 0, VK_F4);
RegisterHotKey(Handle, FKeyHandle[
2], MOD_ALT, VK_F4);
RegisterHotKey(Handle, FKeyHandle[
3], MOD_ALT + MOD_CONTROL, VK_F4);
RegisterHotKey(Handle, FKeyHandle[
4], MOD_WIN, VK_F4);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 4 do
begin
UnregisterHotKey(Handle,FKeyHandle[i]);
GlobalDeleteAtom(FKeyHandle[i]);
end;
end;

procedure TForm1.btnAtomClick(Sender: TObject);
var
id1: ATOM;
pc: Array [
0..255] of Char;
begin
id1 :
= AddAtom('测试文本');
GetAtomName(id1,pc,
256);
ShowMessage(pc);
DeleteAtom(id1);
end;

procedure TForm1.btnGAtomClick(Sender: TObject);
var
id1: ATOM;
pc: Array [
0..255] of Char;
begin
id1 :
= GlobalAddAtom('测试GlobalAddATom');
GlobalGetAtomName(id1,pc,
256);
ShowMessage(pc);
GlobalDeleteAtom(id1);
end;

procedure TForm1.WMHotKeyHandle(var msg: Tmessage);
begin
if msg.WParam = FKeyHandle[1] then ShowMessage('F4');
if msg.WParam = FKeyHandle[2] then ShowMessage('Alt+F4');
if msg.WParam = FKeyHandle[3] then ShowMessage('Alt+Ctrl+F4');
if msg.WParam = FKeyHandle[4] then ShowMessage('Win+F4');
end;

end.
VC:

代码

afx_msg LRESULT OnHotKey(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_HOTKEY, OnHotKey)


RegisterHotKey(GetSafeHwnd(),m_HKID1,MOD_ALT
| MOD_CONTROL,VK_F3);
RegisterHotKey(GetSafeHwnd(),m_HKID2,MOD_ALT ,VK_F4);


UnregisterHotKey(GetSafeHwnd(),m_HKID1);

UnregisterHotKey(GetSafeHwnd(),m_HKID2);

LRESULT CSignleDoc_View::OnHotKey(WPARAM wParam, LPARAM lParam)
{
SetForegroundWindow();
if (wParam==m_HKID1)
{
MessageBox(
"Hello ALT+CTRL+F3 !");
}
else if (wParam==m_HKID2)
{
MessageBox(
"Hello ALT+CTRL+F4 !");
}
return 1;
}

 

posted @ 2010-12-30 09:20  Jekhn  阅读(431)  评论(0编辑  收藏  举报