一个很简单的API问题!!!(50分)

  • 主题发起人 主题发起人 xcode
  • 开始时间 开始时间
X

xcode

Unregistered / Unconfirmed
GUEST, unregistred user!
请教,如何向WinAmp等软件一样,只在系统托盘里显示图标,而在任务栏上不回显示?
 
  某些程序运行启动后并不出现在任务条中,而是缩小为任务条右下角的一个小图标,当鼠标移到这个小图标上时会出现一些提示信息、单击该小图标会执行一些特定的操作。便如任务条右下角的小喇叭图标,单击它会弹出一个简单的音量控制条,双击会启动另一个更大的音量控制程序。<br>  在Shell32.DLL动态链接库中包括一个函数Shell_NotifyIconA()可通知Windows在任务条右下角加入一个小图标,可惜该函数的详细说明未收入Delphi的帮助文档中,下面以一个简单的实例来说明如果使用该函数。<br><br>unit Unit1;<br><br>interface<br><br>{ 记住在uses部分中包括 ShellAPI}<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes,<br>&nbsp; Graphics, Controls, Forms, Dialogs,<br>&nbsp; ShellAPI, StdCtrls;<br><br>{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}<br>const MY_MESSAGE = WM_USER + 100;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; procedure FormPaint(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure OnIconNotify(var Message: TMessage);<br>&nbsp; &nbsp; &nbsp; message MY_MESSAGE;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>{当小图标捕捉到鼠标事件时进入此过程}<br>procedure TForm1.OnIconNotify(var Message: TMessage);<br>const<br>&nbsp; Busy: Boolean = false;<br>begin<br>&nbsp; if not Busy then begin<br><br>&nbsp; &nbsp; Busy := true;<br>&nbsp; &nbsp; if Message.LParam=WM_LBUTTONDOWN then<br>&nbsp; &nbsp; &nbsp; if Application.MessageBox('Are you sure',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Exit', MB_YESNO)=IDYES then Close;<br>&nbsp; &nbsp; Busy := false;<br>&nbsp; end;<br>end;<br><br>{当主Form建立时通知Windows加入小图标}<br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; nid: TNotifyIconData;<br>begin<br>&nbsp; nid.cbSize := sizeof(nid); // nid变量的字节数<br>&nbsp; nid.Wnd := Handle; // 主窗口句柄<br>&nbsp; nid.uID := -1; // 内部标识,可设为任意数<br>&nbsp; nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指定<br>&nbsp; nid.szTip := 'This is a test application'; // 提示字符串<br>&nbsp; nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息<br><br>&nbsp; nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有效<br>&nbsp; if not Shell_NotifyIcon(NIM_ADD, @nid) then begin<br>&nbsp; &nbsp; ShowMessage('Failed!');<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>&nbsp; {将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}<br>&nbsp; SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br>end;<br><br>{程序被关闭时通知Windows去掉小图标}<br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>var<br>&nbsp; nid: TNotifyIconData;<br>begin<br>&nbsp; nid.cbSize := sizeof(nid); // nid变量的字节数<br>&nbsp; nid.uID := -1; //内部标识,与加入小图标时的数一致<br>&nbsp; nid.Wnd := Handle; //主窗口句柄<br>&nbsp; Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标<br><br>end;<br><br>{主窗口初始化完毕并显示时将激活Paint重画事件,此时将主窗口隐藏}<br>procedure TForm1.FormPaint(Sender: TObject);<br>begin<br>&nbsp; Hide;<br>end;<br><br>end.<br>&nbsp;<br><br>  上例中将程序的图标放在任务条右下角,然后隐藏自身,当用户移动鼠标至该图标上时会看到提示字符串,如果单击该图标会出现一个对话框,选择Yes退出程序并清除小图标。<br>
 
procedure WMTrayIcon(var message: TMessage); message WM_TRAYICON;<br>procedure ModifyTrayIcon(Action:DWORD);<br><br><br>procedure TFrmAbout.FormCreate(Sender: TObject);<br>var hr:THandle;<br>begin<br>&nbsp; ShowWindow(Application.Handle,SW_HIDE);//任务栏上隐藏<br>&nbsp; ModifyTrayIcon(NIM_ADD);<br>&nbsp; hr:=CreateRoundRectRgn(0,21,Width,Height,80,80); &nbsp;//<br>&nbsp; SetWindowRgn(handle,hr,true);<br>end;<br><br>procedure TFrmAbout.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; ModifyTrayIcon(NIM_DELETE);<br>end;<br><br>procedure TFrmAbout.ModifyTrayIcon(Action: DWORD); &nbsp;//&amp;shy;托盘图标<br>var NIData:TNotifyIconData;<br>begin<br>&nbsp; with NIData do<br>&nbsp; begin<br>&nbsp; &nbsp; cbSize:=sizeof(TNotifyIconData);<br>&nbsp; &nbsp; uID:=0;<br>&nbsp; &nbsp; uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br>&nbsp; &nbsp; Wnd:=Handle;<br>&nbsp; &nbsp; uCallBackMessage:=WM_TRAYICON;<br>&nbsp; &nbsp; hIcon:=Application.Icon.Handle;<br>&nbsp; &nbsp; strPCopy(szTip,Application.Title);<br>&nbsp; end;<br>&nbsp; Shell_NotifyIcon(Action,@NIData);<br>end;<br><br>procedure TFrmAbout.WMTrayIcon(var message: TMessage); &nbsp;//鼠标点击时弹出菜单<br>var MousePos:TPoint;<br>begin<br>&nbsp; if message.LPARAM=WM_RBUTTONDOWN then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetActiveWindow(Handle);<br>&nbsp; &nbsp; &nbsp; GetCursorPos(MousePos);<br>&nbsp; &nbsp; &nbsp; PopupMenu.Popup(MousePos.x,MousePos.y);<br>&nbsp; &nbsp; end;<br>end;
 
谢谢各位,可惜我已经穷困潦倒了------没有分了!等我有了 再补上!
 
多人接受答案了。
 
后退
顶部