Delphi 实现最小化系统托盘
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, Menus; const WM_TRAYMSG = WM_USER + 101; type TForm1 = class(TForm) pm1: TPopupMenu; N1: TMenuItem; procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private NotifyIcon: TNotifyIconData; procedure WMTrayMsg(var Msg: TMessage); message WM_TRAYMSG; //声明托盘消息 procedure WMSysCommand(var Msg: TMessage); message WM_SYSCOMMAND; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormDestroy(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); end; procedure TForm1.FormCreate(Sender: TObject); begin with NotifyIcon do begin cbSize := SizeOf(TNotifyIconData); Wnd := Self.Handle; uID := 1; uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP; //图标、消息、提示信息 uCallbackMessage := WM_TRAYMSG; hIcon := Application.Icon.Handle; szTip := 'erp服务'; end; Shell_NotifyIcon(NIM_ADD, @NotifyIcon); //去掉关闭按钮 EnableMenuItem(GetSystemMenu(Handle, FALSE), SC_CLOSE, MF_BYCOMMAND or MF_GRAYED); end; { TForm1 } procedure TForm1.WMSysCommand(var Msg: TMessage); begin if Msg.WParam = SC_ICON then Self.Visible := False else DefWindowProc(Self.Handle, Msg.Msg, Msg.WParam, Msg.LParam); end; procedure TForm1.WMTrayMsg(var Msg: TMessage); var p: TPoint; begin case Msg.LParam of WM_LBUTTONDOWN: Self.Visible := True; //显示窗体 WM_RBUTTONDOWN: begin SetForegroundWindow(Self.Handle); //把窗口提前 GetCursorPos(p); pm1.Popup(p.X, p.Y); end; end; end; end.
    有些事情,没经历过不知道原理,没失败过不明白奥妙,没痛苦过不了解真谛。临渊羡鱼,不如退而结网!
                    
                
                
            
        
浙公网安备 33010602011771号