Delphi 7下最小化到系统托盘
转自:http://blog.csdn.net/akof1314/article/details/6411179
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本。定义如下:
| 1 2 3 4 5 6 7 8 9 |
_NOTIFYICONDATAA = record cbSize: DWORD; Wnd: HWND; uID: UINT; uFlags: UINT; uCallbackMessage: UINT; hIcon: HICON; szTip: array [0..63] of AnsiChar; end; |
下面开始实现下最小化到系统托盘功能:
1.新建应用程序,然后在“菜单栏”→“Project”→“Options”→”Application“为程序设定下标题和图标,一定得设置图标,不让显示系统托盘的时候就会空白;
2.在窗体上放置一个右键菜单,添加两个菜单项,如下图所示:

3.窗体单元文件代码如下:
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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; mniN1: TMenuItem; mniwo1: TMenuItem; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure mniN1Click(Sender: TObject); private procedure WMTrayMsg(var Msg: TMessage);message WM_TRAYMSG; //声明托盘消息 procedure WMSysCommand(var Msg: TMessage);message WM_SYSCOMMAND; public { Public declarations } end; var Form1: TForm1; NotifyIcon: TNotifyIconData; //定义托盘图标结构体 implementation {$R *.dfm} {------------------------------------------------------------------------------- Description: 窗体创建时,即创建托盘 -------------------------------------------------------------------------------} 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 := '托盘测试'; end; Shell_NotifyIcon(NIM_ADD,@NotifyIcon); end; {------------------------------------------------------------------------------- Description: 窗体销毁时,卸载托盘 -------------------------------------------------------------------------------} procedure TForm1.FormDestroy(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE,@NotifyIcon); end; {------------------------------------------------------------------------------- Description: 截获窗体最小化消息,最小化到托盘 -------------------------------------------------------------------------------} 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; {------------------------------------------------------------------------------- Description: 自定义的托盘消息 -------------------------------------------------------------------------------} 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; {------------------------------------------------------------------------------- Description: 测试菜单项 -------------------------------------------------------------------------------} procedure TForm1.mniN1Click(Sender: TObject); begin ShowMessage('One'); end; end. |
4.运行结果如下:

浙公网安备 33010602011771号