某些程序运行启动后并不出现在任务条中,而是缩小为任务条右下角的一个小图标,当鼠标移到这个小图标上时会出现一些提示信息、单击该小图标会执行一些特定的操作。便如任务条右下角的小喇叭图标,单击它会弹出一个简单的音量控制条,双击会启动另一个更大的音量控制程序。在Shell32.DLL动态链接库中包括一个函数Shell_NotifyIconA()可通知Windows在任务条右下角加入一个小图标,可惜该函数的详细说明未收入Delphi的帮助文档中,下面以一个简单的实例来说明如果使用该函数。
unit Unit1;
interface
{ 记住在uses部分中包括 ShellAPI}usesWindowsMessagesSysUtilsClassesGraphicsControlsFormsDialogsShellAPIStdCtrls;
{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}const MY_MESSAGE = WM_USER + 100;
typeTForm1 = class(TForm)procedure FormCreate(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction);procedure FormPaint(Sender: TObject);privateprocedure OnIconNotify(var Message: TMessage);message MY_MESSAGE;public{ Public declarations }end;
varForm1: TForm1;
implementation
{$R *.DFM}{当小图标捕捉到鼠标事件时进入此过程}procedure TForm1.OnIconNotify(var Message: TMessage);constBusy: Boolean = false;beginif not Busy then beginBusy := true;if Message.LParam=WM_LBUTTONDOWN thenif Application.MessageBox('Are you sure'
'Exit'MB_YESNO)=IDYES then Close;Busy := false;end;end;
{当主Form建立时通知Windows加入小图标}procedure TForm1.FormCreate(Sender: TObject);varnid: TNotifyIconData;beginnid.cbSize := sizeof(nid); // nid变量的字节数nid.Wnd := Handle; // 主窗口句柄nid.uID := -1; // 内部标识,可设为任意数nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?
nid.szTip := 'This is a test application'; // 提示字符串nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
if not Shell_NotifyIcon(NIM_ADD@nid) then beginShowMessage('Failed!');Application.Terminate;end;{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}SetWindowLong(Application.HandleGWL_EXSTYLEWS_EX_TOOLWINDOW);end;
{程序被关闭时通知Windows去掉小图标}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);varnid: TNotifyIconData;beginnid.cbSize := sizeof(nid); // nid变量的字节数nid.cbSize := sizeof(nid); // nid变量的字节数nid.uID := -1; //内部标识,与加入小图标时的数一致nid.Wnd := Handle; //主窗口句柄Shell_NotifyIcon(NIM_DELETE@nid); //去掉小图标Shell_NotifyIcon(NIM_DELETE@nid); //去掉小图标end;
{主窗口初始化完毕并显示时将激活Paint重画事件,此时将主窗口隐藏}procedure TForm1.FormPaint(Sender: TObject);beginHide;end;
end.
unit Unit1;
interface
{ 记住在uses部分中包括 ShellAPI}usesWindowsMessagesSysUtilsClassesGraphicsControlsFormsDialogsShellAPIStdCtrls;
{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}const MY_MESSAGE = WM_USER + 100;
typeTForm1 = class(TForm)procedure FormCreate(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction);procedure FormPaint(Sender: TObject);privateprocedure OnIconNotify(var Message: TMessage);message MY_MESSAGE;public{ Public declarations }end;
varForm1: TForm1;
implementation
{$R *.DFM}{当小图标捕捉到鼠标事件时进入此过程}procedure TForm1.OnIconNotify(var Message: TMessage);constBusy: Boolean = false;beginif not Busy then beginBusy := true;if Message.LParam=WM_LBUTTONDOWN thenif Application.MessageBox('Are you sure'
'Exit'MB_YESNO)=IDYES then Close;Busy := false;end;end;
{当主Form建立时通知Windows加入小图标}procedure TForm1.FormCreate(Sender: TObject);varnid: TNotifyIconData;beginnid.cbSize := sizeof(nid); // nid变量的字节数nid.Wnd := Handle; // 主窗口句柄nid.uID := -1; // 内部标识,可设为任意数nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?
nid.szTip := 'This is a test application'; // 提示字符串nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
if not Shell_NotifyIcon(NIM_ADD@nid) then beginShowMessage('Failed!');Application.Terminate;end;{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}SetWindowLong(Application.HandleGWL_EXSTYLEWS_EX_TOOLWINDOW);end;
{程序被关闭时通知Windows去掉小图标}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);varnid: TNotifyIconData;beginnid.cbSize := sizeof(nid); // nid变量的字节数nid.cbSize := sizeof(nid); // nid变量的字节数nid.uID := -1; //内部标识,与加入小图标时的数一致nid.Wnd := Handle; //主窗口句柄Shell_NotifyIcon(NIM_DELETE@nid); //去掉小图标Shell_NotifyIcon(NIM_DELETE@nid); //去掉小图标end;
{主窗口初始化完毕并显示时将激活Paint重画事件,此时将主窗口隐藏}procedure TForm1.FormPaint(Sender: TObject);beginHide;end;
end.
解决方案 »
- 求最快的图像旋转90度算法
- 求 d5 下 unicode 转 utf8 的代码
- cxGrid排序后如何让FastReport同步
- 请问怎么知道一个系统是用DELPHI开发的还是用VC开发的?里面有一些EXE文件、DLL文件之类的,有界面的程序。
- 如何生成cab控件包?
- 这种效果怎么做?
- 主程序device.exe如何调用另一个程序update.exe,赐教
- 本机没有flash.ocx也能正常运行用到了Shockwaveflash控件的flash播放器吗?
- 在下准备做一个24的程序,看看大家有什么好的算法!!(四个数的排列已经得到)!
- Form里有一个Panel,运行是拉动鼠标缩小窗口,但不能小于Panel定义时的大小,How to do?
- Delphi+Access,(ADO error :缺少更新或刷新的键列信息) 是怎么回事?
- 请问各位,为什么我的DELPHI写CORBA编译时出现(File Not Found:'OrbPas.dcu')?
WM_TrayMessage=WM_User+100; protected
procedure AppMinimized(Sender: TObject);
procedure WMTrayMessage(var msg:TMessage);message WM_TrayMessage;
....
uses ShellApivar
NID:TNotifyIconData{点击了任务栏的图标}
procedure TMainForm.WMTrayMessage(var msg:TMessage);
var
p:TPoint;
begin
if msg.LParam=WM_LButtonDown then
begin
ShowWindow(Application.Handle,SW_Show);
Application.Restore;
end
else if msg.LParam=WM_RButtonDown then
begin
GetCursorPos(p);
pmTray.Popup(p.x,p.y);//pmtray是弹出菜单
end;
end;{应用程序最小化时图标显示在任务栏}
procedure TMainForm.AppMinimized(Sender:TObject);
begin
NID.cbSize:=SizeOf(TNotifyIconData);
NID.hIcon:=Application.Icon.Handle;
NID.szTip:= '彩虹2.0 Beta ';
NID.uCallbackMessage:=WM_TrayMessage;
NID.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
NID.uID:=0;
NID.Wnd:=Handle;
Shell_NotifyIcon(NIM_ADD,@NID);
ShowWindow(Application.Handle,SW_Hide);
end;procedure TMainForm.FormCreate(Sender: TObject);
begin
Application.OnMinimize:=AppMinimized;
AppMinimized(nil);
end;procedure TMainForm.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@NID);
end;