本文整理自网络,侵删。
constWM_TRAYNOTIFY=WM_USER+1;//定义通知消息...private { Private declarations } procedure SysCommand(var SysMsg: TMessage); message WM_SYSCOMMAND; //托盘 procedure WMNID(var msg:TMessage); message WM_TRAYNOTIFY; //托盘操作...var FrmLogin: TFrmLogin; nd0 :NotifyIconData; //托盘implementation{$R *.dfm}procedure TFrmLogin.SysCommand(var SysMsg: TMessage);begin if (SysMsg.WParam = SC_MINIMIZE) or (SysMsg.WParam = SC_CLOSE) then // 当最小化与关闭 时 begin SetWindowPos(Application.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW); Hide; // 在任务栏隐藏程序 // 在托盘区显示图标 with nd0 do begin cbSize := SizeOf(TNotifyIconData); Wnd := Handle; uID := 0; uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; uCallBackMessage := WM_TRAYNOTIFY; hIcon := Application.Icon.Handle; szTip := csMainRitle; end; Shell_NotifyIcon(NIM_ADD, @nd0); // 在托盘区显示图标 end else inherited; //end;end;Procedure TFrmLogin.WMNID(var msg:TMessage); //托盘操作var mousepos: TPoint;begin GetCursorPos(mousepos); //获取鼠标位置 case msg.LParam of WM_LBUTTONUP: // 在托盘区点击左键后 begin Self.Visible := not Self.Visible; // 显示主窗体与否 Shell_NotifyIcon(NIM_DELETE, @nd0); // 显示主窗体后删除托盘区的图标 SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW); // 在任务栏显示程序 end; WM_RBUTTONUP: pm1.Popup(mousepos.X, mousepos.Y); // 弹出菜单 end;end;
相关阅读 >>
更多相关阅读请进入《Delphi》频道 >>