请问NotifyIcon的用法(50分)

  • 主题发起人 主题发起人 tryin
  • 开始时间 开始时间
T

tryin

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把程序做进任务栏,想用NotifyIcon(<br><br>&nbsp; &nbsp; DWORD dwMessage, // message identifier<br>&nbsp; &nbsp; PNOTIFYICONDATA pnid // pointer to structure<br>&nbsp; &nbsp;);,<br>请赐教,说的详细些,最好是能编译通过的源码,只要这个功<br>能就行了。多谢了!
 
procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Icon1 := TIcon.Create;<br>&nbsp; buffer_len := 2000;<br>&nbsp; SetLength(buf_list, buffer_len);<br>&nbsp; CapState := 0;<br>end;<br><br>procedure TForm1.InstallIcon;<br>var<br>&nbsp; IconData: NOTIFYICONDATA;<br>begin<br>&nbsp; Icon1.LoadFromFile(ChangeFileExt(Application.ExeName, '.ico'));<br>&nbsp; IconData.cbSize := SizeOf(IconData);<br>&nbsp; IconData.Wnd := Handle;<br>&nbsp; IconData.uID := ICON_ID;<br>&nbsp; IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br>&nbsp; IconData.uCallbackMessage := MI_ICONEVENT;<br>&nbsp; IconData.hIcon := Icon1.Handle;<br>&nbsp; strCopy(IconData.szTip, PChar('Test......'));<br>&nbsp; Shell_NotifyIcon(NIM_ADD, @IconData);<br>end;<br><br>procedure TForm1.UnInstallIcon;<br>var<br>&nbsp; IconData: NOTIFYICONDATA;<br>begin<br>&nbsp; IconData.cbSize := SizeOf(IconData);<br>&nbsp; IconData.Wnd := Handle;<br>&nbsp; IconData.uID := ICON_ID;<br>&nbsp; Shell_NotifyIcon(NIM_DELETE, @IconData);<br>end;<br><br>procedure TForm1.WMIconOnClick(var Message: TMessage);<br>var<br>&nbsp; pt: POINT;<br>begin<br>&nbsp; case Message.LParam of<br>&nbsp; &nbsp; WM_LBUTTONDOWN : ;<br>&nbsp; &nbsp; &nbsp; //Form1.Visible := true;<br>&nbsp; &nbsp; WM_RBUTTONDOWN :<br>&nbsp; &nbsp; &nbsp; PopupMenu1.Popup(pt.X, pt.Y);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormPaint(Sender: TObject);<br>begin<br>&nbsp; Form1.Visible := false;<br>&nbsp; InstallIcon;<br>end;<br><br>procedure TForm1.mnuCloseClick(Sender: TObject);<br>begin<br>&nbsp; Close; &nbsp;<br>&nbsp; UnInstallIcon;<br>end;<br>
 
建议搜索一下旧资料<br>notifyicon的帖子多得数不过来
 
unit TrayIcon;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Menus,shellapi;<br><br>type<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; PNotifyIconData = ^TNotifyIconDataA;<br>&nbsp; &nbsp; TNotifyIconDataA = record<br>&nbsp; &nbsp; cbSize : DWORD;<br>&nbsp; &nbsp; Wnd : HWND;<br>&nbsp; &nbsp; uID : UINT;<br>&nbsp; &nbsp; uFlags : UINT;<br>&nbsp; &nbsp; uCallbackMessage : UINT;<br>&nbsp; &nbsp; hIcon : HICON;<br>&nbsp; &nbsp; szTip : array [0..63] of AnsiChar;<br>&nbsp; end;<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; open1: TMenuItem;<br>&nbsp; &nbsp; close1: TMenuItem;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; about1: TMenuItem;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure open1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure close1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; //-------------------------------------------------------------<br>&nbsp; &nbsp; IconData: TNotifyIconData;<br>&nbsp; &nbsp; procedure ShowIcon;<br>&nbsp; &nbsp; procedure IconOnClick(var message:TMessage); message WM_USER+1;<br>&nbsp; &nbsp; Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;<br>&nbsp; &nbsp; //-------------------------------------------------------------<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>procedure TForm1.IconOnClick( var message: Tmessage);<br>var p : TPoint;<br>begin<br>&nbsp;if (message.lParam = WM_LBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;ShowWindow(Handle, SW_SHOW );<br>&nbsp;end;<br>&nbsp;<br>&nbsp;if (message.lParam = WM_RBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;GetCursorPos(p);<br>&nbsp; &nbsp;SetForegroundWindow (Application.Handle); //&lt;-------------------avoid a little bug.<br>&nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp;popupmenu1.Popup( p.x ,p.y );<br>&nbsp;end;<br>end;<br><br>Procedure TForm1.WMSysCommand(Var Message : TMessage) ;<br>begin<br><br>&nbsp; if (Message.WParam = SC_MINIMIZE) then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowIcon;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;Inherited;<br><br>end;<br><br>procedure TForm1.ShowIcon;<br>begin<br>&nbsp; &nbsp; &nbsp; IconData.cbSize := SizeOf( IconData );<br>&nbsp; &nbsp; &nbsp; IconData.Wnd := Handle;<br>&nbsp; &nbsp; &nbsp; IconData.uID := 1;<br>&nbsp; &nbsp; &nbsp; IconData.uFlags := NIF_ICON &nbsp;or NIF_MESSAGE or NIF_TIP;<br>&nbsp; &nbsp; &nbsp; IconData.uCallBackMessage := WM_USER+1;<br>&nbsp; &nbsp; &nbsp; IconData.hIcon := application.Icon.Handle;<br>&nbsp; &nbsp; &nbsp; IconData.szTip := 'LANChat';<br>&nbsp; &nbsp; &nbsp; Shell_NotifyIcon( NIM_ADD, @IconData );<br>&nbsp; &nbsp; &nbsp; ShowWindow(Handle, SW_HIDE);<br>&nbsp; &nbsp; &nbsp; hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp;Shell_NotifyIcon( NIM_DELETE, @IconData );<br>end;<br><br>procedure TForm1.open1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.Show;<br>end;<br><br>procedure TForm1.close1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.close;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br>&nbsp; <br>&nbsp; showwindow(application.handle,sw_hide);<br>&nbsp; <br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; showicon;<br>end;<br><br>end.<br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部