var
Form1: TForm1;
NotifyIcon:TNotifyIconData;//NotifyIcon是非常重要的一个变量,
//整个程序基本上是围着这个变量在转。
//TNotifyIconData是一个记录类型
implementation
{$R *.dfm}
procedure TForm1.WMSysCommand(var Message:TMessage);
begin
if Message.WParam = SC_ICON then //最小化了
begin
form1.hide;
installicon;
end
else
inherited;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@NotifyIcon); //从任务栏删除图标
end;
procedure TForm1.installicon;
begin
application.ShowMainForm:=false;
//SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
//上面的函数可以和application一样用。但是只隐藏任务栏
//NotifyIcon为全局变量,在程序的开头已经定义了
with NotifyIcon do
begin
cbSize:=SizeOf(TNotifyIconData);
Wnd:=Handle; //指向当前窗体Form1的句柄
uID:=1;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallBackMessage:=WM_NID;
hIcon:=Application.Icon.Handle;
szTip:='任务栏演示程序';
end;
//把设置好的变量NotifyIcon加入到系统中以便处理
Shell_NotifyIcon(NIM_ADD,@NotifyIcon);
end;
procedure TForm1.IconOnClick(var message:TMessage);
//处理鼠标在指示状态栏对应的图标上的单击事件
var
p:TPoint;
begin
//如果按下的是鼠标左键,并且允许显示,则显示Form2
if message.lParam=WM_LBUTTONDOWN then
Form1.Show;
//如果按下的是鼠标右键,则显示弹出菜单
if(message.lParam=WM_RBUTTONDOWN)then
begin
const msg_tray=wm_user+1105;
var
Form1: TForm1;
myicon: NotifyIconData;
implementation
{$R *.dfm}
procedure Tform1.instell;
begin
with myicon do
begin
cbSize:=SizeOf(TNotifyIconData);
Wnd:=Handle; //指向当前窗体Form1的句柄
uID:=0;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
ucallBackMessage:=msg_tray;
hIcon:=Application.Icon.Handle;
szTip:='哈哈哈哈';
end;
Shell_NotifyIcon(NIM_ADD,@myicon);
end;
procedure Tform1.wmsyscommand(var msg:Tmessage);
begin
case msg.WParam of
sc_minimize : begin //最小化
Application.Minimize;
form1.Hide;
instell;
inherited;
end;
else
inherited;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @myicon);
end;
procedure TForm1.WndProc(var Msg: TMessage);
var
pt:TPoint;
begin
with Msg do
begin
if Msg = msg_tray then
begin
case lParam of
WM_RBUTTONDOWN: begin
GetCursorPos(pt);
PopupMenu1.Popup(Pt.X, Pt.Y);
end;
WM_LBUTTONDOWN: begin
form1.Visible:=not form1.Visible;
Shell_NotifyIcon(NIM_DELETE, @myicon);
end;