procedure TfMain.WndProc(var MyMessage: TMessage);
var
pt: TPoint;
begin
case MyMessage.Msg of
WM_CLOSE: //关闭事件
begin
application.Minimize;
end;
WM_CREATE:
begin
WM_TASKBARCREATED := RegisterWindowMessage('TaskbarCreated');
//创建托盘栏图标类
TrayIcon := TTrayIcon.Create(self);
TrayIcon.ActiveIcon.Handle := LoadIcon(HInstance, 'Tray0');
TrayIcon.DeActiveIcon.Handle := LoadIcon(HInstance, 'Tray1');
end;
WM_DESTROY: // 窗体销毁消息
begin
//释放托盘栏图标
TrayIcon.NotifyDelete(self.Handle);
//释放托盘栏图标类
TrayIcon.Free;
end;
WM_SYSCOMMAND:
case MyMessage.WParam of
SC_MINIMIZE: // 窗体最小化
application.Minimize
else
inherited;
end;
MYWM_NOTIFYICON:
case MyMessage.lParam of
WM_LBUTTONDBLCLK: //鼠标左键双击
begin
if self.Showing = true then
begin
application.Minimize;
self.Hide;
end
else
begin
self.Show;
application.Restore;
SetForegroundWindow(self.Handle);
end;
end;
WM_RBUTTONDOWN: //鼠标右键被按下
begin
GetCursorPos(pt);
SetForegroundWindow(self.Handle);
pmTrayIcon.Popup(pt.x,pt.y);
end
else
//调用父类的WndProc方法处理其它消息
inherited;
end;
else
if MyMessage.Msg = WM_TASKBARCREATED then //托盘栏防崩溃处理
begin
TrayIcon.NotifyDelete(self.Handle);
TrayIcon.NotifyAdd(self.Handle, self.Showing);
end
else
//调用父类的WndProc方法处理其它消息
inherited;
end;
end;