我写的托盘控制程序。
最小化后到托盘。
unit KeyCtr;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus,ShellAPI, ExtCtrls, ImgList, Buttons,TFlatSpeedButtonUnit;
type
TForm1 = class(TForm)
popMenu: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
img1: TImage;
img2: TImage;
btHide: TFlatSpeedButton;
Label1: TLabel;
Image1: TImage;
N5: TMenuItem;
lbState: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure btHideClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure FormKeyPress(Sender: TObject;
var Key: Char);
private
{ Private declarations }
IconData:TNotifyIconData;
procedure HotKeyDown(var Msg: Tmessage);
message WM_HOTKEY;
procedure WndProc(var Msg: TMessage);
override;
public
{ Public declarations }
protected
end;
var
Form1: TForm1;
PgDnid:Integer;
PgUpid:Integer;
Flag:Integer;
tmpstr:string;
closeflag:boolean;
implementation
{$R *.DFM}
procedure TForm1.HotKeyDown(var Msg: Tmessage);
begin
if msg.LParamHi=VK_PRIOR then
begin
//ShowMessage('');
KeyBd_Event(VK_HOME, MapVirtualKey(VK_PRIOR, 0), 0, 0);
end;
if msg.LParamHi=VK_NEXT then
begin
KeyBd_Event(VK_END, MapVirtualKey(VK_END, 0), 0, 0);
end;
end;
//控制托盘图标
procedure TForm1.WndProc(var Msg: TMessage);
var
P: TPoint;
begin
//获取小图标点击事件
if Msg.Msg = WM_USER + 100 then
case Msg.LParam of
WM_RBUTTONDOWN:
begin
setActiveWindow(Handle);
//设置窗口的次序,否则菜单无法消失,MS的bug
GetCursorPos(P);
popMenu.Popup(P.x, P.y);
end;
WM_LBUTTONDBLCLK:
begin
Form1.WindowState:=wsNormal;
Show;
end;
end;
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Flag:=1;
lbState.Caption:='当前状态:Home , End';
//注册热键
PgUpid:=GlobalAddAtom('hotkey_PgUp');
PgDnid:=GlobalAddAtom('hotkey_PgDn');
//RegisterHotKey(handle,id,mod_control,81);
RegisterHotKey(handle,PgUpid,0,VK_PRIOR);
RegisterHotKey(handle,PgDnid,0,VK_NEXT);
//控制托盘显示
with IconDatado
begin
cbSize := SizeOf(IconData);
Wnd := Handle;
uID := 100;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallBackMessage := WM_USER + 100;
//hIcon := Application.Icon.Handle;
IconData.hIcon:=img1.Picture.Icon.Handle;
szTip := 'MyHotKey';
Shell_NotifyIcon(NIM_ADD, @IconData);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
//退出时取消热键定义
UnRegisterHotKey(handle,PgUpid);
UnRegisterHotKey(handle,PgDnid);
//退出时消去托盘图标
Shell_NOtifyIcon(NIM_DELETE, @IconData);
end;
procedure TForm1.N4Click(Sender: TObject);
begin
closeflag:=true;
Form1.Close
end;
//恢复
procedure TForm1.N2Click(Sender: TObject);
begin
UnRegisterHotKey(handle,PgUpid);
UnRegisterHotKey(handle,PgDnid);
IconData.hIcon:=img2.Picture.Icon.Handle;
Shell_NOtifyicon(NIM_MODIFY,@IconData);
lbState.Caption:='当前状态:PgUp , PgDn';
end;
//对调
procedure TForm1.N1Click(Sender: TObject);
begin
UnRegisterHotKey(handle,PgUpid);
RegisterHotKey(handle,PgUpid,0,VK_PRIOR);
UnRegisterHotKey(handle,PgDnid);
RegisterHotKey(handle,PgDnid,0,VK_NEXT);
IconData.hIcon:=img1.Picture.Icon.Handle;
Shell_NOtifyicon(NIM_MODIFY,@IconData);
lbState.Caption:='当前状态:Home , End';
end;
procedure TForm1.btHideClick(Sender: TObject);
begin
//Form1.WindowState:=wsMinimized;
Form1.hide;
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
{if closeflag=true then
CanClose:=true
else
begin
if Application.MessageBox('关闭 ?','提示',MB_OKCANCEL+MB_ICONINFORMATION)=IDOK then
begin
CanClose:=true;
end else
begin
Form1.hide;
CanClose:=False;
end;
end;
}
end;
procedure TForm1.FormKeyPress(Sender: TObject;
var Key: Char);
begin
//VK_ESCAPE=27
if Key=#27 then
Form1.Hide;
end;
end.