二、注册一个热键,然后用热键控制。
看一下我写的这个程序,它就可以实现老板键的功能(假定热键是CTRL+ALT+]),按一下这个组合键就隐藏,再按一下就显示出来:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, CoolTrayIcon;
type
TForm1 = class(TForm)
CoolTrayIcon1: TCoolTrayIcon;
procedure CoolTrayIcon1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
id1: Integer;
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
function ByteReserve(Value: Word):Word;
public
{ Public declarations }
end;
var
Form1: TForm1;
hide_windows: boolean;
implementation
{$R *.dfm}
procedure do_something1;
begin
if hide_windows=false then
begin
Form1.CoolTrayIcon1.HideMainForm;
Form1.CoolTrayIcon1.IconVisible :=False;
hide_windows:=true;
end
else
begin
Form1.CoolTrayIcon1.ShowMainForm;
Form1.CoolTrayIcon1.IconVisible :=True;
hide_windows:=false;
end;
end;
function TForm1.ByteReserve(Value: Word):Word;
var
buf:Byte;
i: Integer;
begin
Result := 0;
for i:=0 to 7 do
begin
buf := Byte((Value shr i) and $01);
Result := Result or (buf shl (7-i));
end;
end;
procedure free_hotkey1;
begin
UnRegisterHotKey(form1.handle,form1.id1);
globalDeleteAtom(form1.id1);
end;
procedure set_hotkey(hotkey_name:pchar;key_name:string;var key_id:integer);
var
TheKey,Shirt: Word;
TheShiftState: TShiftState;
begin
if globalFindAtom(hotkey_name) = 0 then
begin
key_id:=globalAddAtom(hotkey_name);
ShortCutToKey(texttoshortcut(key_name),TheKey,TheShiftState);
Shirt:= form1.ByteReserve(Word((texttoshortcut(key_name) and $F000) shr 8));
RegisterHotKey(form1.Handle,key_id,Shirt,Cardinal(Chr(TheKey)));
end;
end;
procedure Tform1.WMHotKey (var Msg : TWMHotKey);
begin
if msg.HotKey = form1.id1 then do_something1;
end;
procedure TForm1.CoolTrayIcon1Click(Sender: TObject);
begin
Form1.CoolTrayIcon1.ShowMainForm;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
hotkey_1_name:string;
begin
hide_windows:=false;
hotkey_1_name:='CTRL+ALT+]';
set_hotkey('MyHotkey1',hotkey_1_name,form1.id1);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
free_hotkey1;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, CoolTrayIcon;
type
TForm1 = class(TForm)
CoolTrayIcon1: TCoolTrayIcon;
procedure CoolTrayIcon1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
id1: Integer;
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
function ByteReserve(Value: Word):Word;
public
{ Public declarations }
end;
var
Form1: TForm1;
hide_windows: boolean;
implementation
{$R *.dfm}
procedure do_something1;
begin
if hide_windows=false then
begin
Form1.CoolTrayIcon1.HideMainForm;
Form1.CoolTrayIcon1.IconVisible :=False;
hide_windows:=true;
end
else
begin
Form1.CoolTrayIcon1.ShowMainForm;
Form1.CoolTrayIcon1.IconVisible :=True;
hide_windows:=false;
end;
end;
function TForm1.ByteReserve(Value: Word):Word;
var
buf:Byte;
i: Integer;
begin
Result := 0;
for i:=0 to 7 do
begin
buf := Byte((Value shr i) and $01);
Result := Result or (buf shl (7-i));
end;
end;
procedure free_hotkey1;
begin
UnRegisterHotKey(form1.handle,form1.id1);
globalDeleteAtom(form1.id1);
end;
procedure set_hotkey(hotkey_name:pchar;key_name:string;var key_id:integer);
var
TheKey,Shirt: Word;
TheShiftState: TShiftState;
begin
if globalFindAtom(hotkey_name) = 0 then
begin
key_id:=globalAddAtom(hotkey_name);
ShortCutToKey(texttoshortcut(key_name),TheKey,TheShiftState);
Shirt:= form1.ByteReserve(Word((texttoshortcut(key_name) and $F000) shr 8));
RegisterHotKey(form1.Handle,key_id,Shirt,Cardinal(Chr(TheKey)));
end;
end;
procedure Tform1.WMHotKey (var Msg : TWMHotKey);
begin
if msg.HotKey = form1.id1 then do_something1;
end;
procedure TForm1.CoolTrayIcon1Click(Sender: TObject);
begin
Form1.CoolTrayIcon1.ShowMainForm;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
hotkey_1_name:string;
begin
hide_windows:=false;
hotkey_1_name:='CTRL+ALT+]';
set_hotkey('MyHotkey1',hotkey_1_name,form1.id1);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
free_hotkey1;
end;
end.