为什么无法改变托盘程序的状态呢? ( 积分: 50 )

  • 主题发起人 主题发起人 wxc211
  • 开始时间 开始时间
W

wxc211

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,而且退出时托盘图标不能自动消失。<br><br><br> type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;PopupMenu1: TPopupMenu;<br> &nbsp; &nbsp;N1: TMenuItem;<br> &nbsp; &nbsp;N2: TMenuItem;<br> &nbsp; &nbsp;PopupMenu2: TPopupMenu;<br> &nbsp; &nbsp;N3: TMenuItem;<br> &nbsp; &nbsp;N21: TMenuItem;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp;private<br> &nbsp; &nbsp;//托盘图标<br> &nbsp; &nbsp;FIcon:TIcon;<br> &nbsp; &nbsp;//定义TNotifyIconData类型的变量<br> &nbsp; &nbsp;NID:TNotifyIconData;<br> &nbsp;public<br> &nbsp; &nbsp;procedure TrayIconMessage(var MSG:TMessage);message WM_USER+100;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;FIcon:=TIcon.Create;<br> &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> &nbsp;if FIcon.Handle=0 then<br> &nbsp;begin<br> &nbsp; &nbsp;Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> &nbsp; &nbsp;Close;<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;with NID do<br> &nbsp;begin<br> &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp;uID:=1;<br> &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp;szTip:='图标1';<br> &nbsp;end;<br> &nbsp;Shell_NotifyIcon(NIM_ADD,@NID);<br> &nbsp;//在任务栏上隐藏程序<br> &nbsp;SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,@NID);<br> &nbsp;FIcon.Free;<br>end;<br><br>procedure TForm1.TrayIconMessage(var MSG: TMessage);<br>var<br> &nbsp;Point:TPoint;<br>begin<br> &nbsp;if MSG.LParam=WM_LBUTTONDOWN then<br> &nbsp;begin<br> &nbsp; &nbsp;Visible:=not Visible;<br> &nbsp; &nbsp;if MSG.WParam=1 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> &nbsp; &nbsp; &nbsp;if FIcon.Handle=0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with NID do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp; &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp; &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp; &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp; &nbsp; &nbsp;uID:=2;<br> &nbsp; &nbsp; &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp; &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp; &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;szTip:='图标2';<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon2');<br> &nbsp; &nbsp; &nbsp;if FIcon.Handle=0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox('无法打开图标2','找不到图标文件2',MB_OK or MB_IconError);<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with NID do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp; &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp; &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp; &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp; &nbsp; &nbsp;uID:=1;<br> &nbsp; &nbsp; &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp; &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp; &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;szTip:='图标1';<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Shell_NotifyIcon(NIM_MODIFY,@NID);<br> &nbsp;end;<br>end;
 
如题,而且退出时托盘图标不能自动消失。<br><br><br> type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;PopupMenu1: TPopupMenu;<br> &nbsp; &nbsp;N1: TMenuItem;<br> &nbsp; &nbsp;N2: TMenuItem;<br> &nbsp; &nbsp;PopupMenu2: TPopupMenu;<br> &nbsp; &nbsp;N3: TMenuItem;<br> &nbsp; &nbsp;N21: TMenuItem;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp;private<br> &nbsp; &nbsp;//托盘图标<br> &nbsp; &nbsp;FIcon:TIcon;<br> &nbsp; &nbsp;//定义TNotifyIconData类型的变量<br> &nbsp; &nbsp;NID:TNotifyIconData;<br> &nbsp;public<br> &nbsp; &nbsp;procedure TrayIconMessage(var MSG:TMessage);message WM_USER+100;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;FIcon:=TIcon.Create;<br> &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> &nbsp;if FIcon.Handle=0 then<br> &nbsp;begin<br> &nbsp; &nbsp;Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> &nbsp; &nbsp;Close;<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;with NID do<br> &nbsp;begin<br> &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp;uID:=1;<br> &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp;szTip:='图标1';<br> &nbsp;end;<br> &nbsp;Shell_NotifyIcon(NIM_ADD,@NID);<br> &nbsp;//在任务栏上隐藏程序<br> &nbsp;SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,@NID);<br> &nbsp;FIcon.Free;<br>end;<br><br>procedure TForm1.TrayIconMessage(var MSG: TMessage);<br>var<br> &nbsp;Point:TPoint;<br>begin<br> &nbsp;if MSG.LParam=WM_LBUTTONDOWN then<br> &nbsp;begin<br> &nbsp; &nbsp;Visible:=not Visible;<br> &nbsp; &nbsp;if MSG.WParam=1 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon1');<br> &nbsp; &nbsp; &nbsp;if FIcon.Handle=0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox('无法打开图标1','找不到图标文件1',MB_OK or MB_IconError);<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with NID do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp; &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp; &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp; &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp; &nbsp; &nbsp;uID:=2;<br> &nbsp; &nbsp; &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp; &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp; &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;szTip:='图标2';<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FIcon.Handle:=LoadIcon(HInstance,'Icon2');<br> &nbsp; &nbsp; &nbsp;if FIcon.Handle=0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox('无法打开图标2','找不到图标文件2',MB_OK or MB_IconError);<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;with NID do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;//cbSize为本记录的大小<br> &nbsp; &nbsp; &nbsp; &nbsp;cbSize:=SizeOf(TNotifyIconData);<br> &nbsp; &nbsp; &nbsp; &nbsp;//Wnd为本窗口句柄<br> &nbsp; &nbsp; &nbsp; &nbsp;Wnd:=Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;//uID是程序员定义的唯一标识符,如一个应用程序有多个托盘图标,则每个图标需要有唯一的uID<br> &nbsp; &nbsp; &nbsp; &nbsp;uID:=1;<br> &nbsp; &nbsp; &nbsp; &nbsp;uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;<br> &nbsp; &nbsp; &nbsp; &nbsp;uCallbackMessage:=WM_USER+100;<br> &nbsp; &nbsp; &nbsp; &nbsp;hIcon:=FIcon.Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp;szTip:='图标1';<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Shell_NotifyIcon(NIM_MODIFY,@NID);<br> &nbsp;end;<br>end;
 
const<br> &nbsp;mymsg = wm_user + 1;<br> &nbsp;iid = 100;<br> &nbsp;CapStr = 'Data Tool';<br><br> &nbsp;private<br> &nbsp; &nbsp;procedure mymessage(var message: TMessage); message mymsg;<br><br>var<br> &nbsp;NTID: TNotifyIconDataA;<br><br>procedure TMainForm.mymessage(var message: TMessage);<br>var<br> &nbsp;mypt: TPoint;<br>begin<br> &nbsp;if message.lparam = wm_rbuttonup then<br> &nbsp;begin<br> &nbsp; &nbsp;getcursorpos(mypt);<br> &nbsp; &nbsp;popupmenu1.popup(mypt.X,mypt.Y);<br> &nbsp;end;<br> &nbsp;if message.LParam = wm_LBUTTONDBLCLK then &nbsp;//双击左键<br> &nbsp;//if message.LParam=WM_LBUTTONDOWN then &nbsp;//单击左键<br> &nbsp;begin<br> &nbsp; &nbsp;NTID.cbSize := SizeOf(TNotifyIconDataA);<br> &nbsp; &nbsp;NTID.Wnd := handle;<br> &nbsp; &nbsp;NTID.uID := iid;<br> &nbsp; &nbsp;NTID.uFlags := nif_icon + nif_tip + nif_message;<br> &nbsp; &nbsp;NTID.uCallbackMessage := mymsg;<br> &nbsp; &nbsp;NTID.hIcon := Application.Icon.Handle;<br> &nbsp; &nbsp;NTID.szTip := CapStr;<br> &nbsp; &nbsp;Shell_NotifyIconA(NIM_DELETE,@NTID);<br> &nbsp; &nbsp;Application.MainForm.Show;<br> &nbsp;end;<br> &nbsp;message.Result := 0;<br>end;<br><br>procedure TMainForm.HideWindows;<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp;NTID.cbSize := SizeOf(TNotifyIconDataA);<br> &nbsp;NTID.Wnd := handle;<br> &nbsp;NTID.uID := iid;<br> &nbsp;NTID.uFlags := nif_icon + nif_tip + nif_message;<br> &nbsp;NTID.uCallbackMessage := mymsg;<br> &nbsp;NTID.hIcon := Application.Icon.Handle;<br> &nbsp;NTID.szTip := CapStr;<br> &nbsp;Shell_NotifyIconA(NIM_ADD, @NTID);<br> &nbsp;Application.MainForm.Hide;<br>end;<br><br>procedure TMainForm.ShowWindows;<br>begin<br> &nbsp;NTID.cbSize := SizeOf(TNotifyIconDataA);<br> &nbsp;NTID.Wnd := handle;<br> &nbsp;NTID.uID := iid;<br> &nbsp;NTID.uFlags := nif_icon+nif_tip+nif_message;<br> &nbsp;NTID.uCallbackMessage := mymsg;<br> &nbsp;NTID.hIcon := Application.Icon.Handle;<br> &nbsp;NTID.szTip := CapStr;<br> &nbsp;Shell_NotifyIconA(NIM_DELETE, @NTID);<br> &nbsp;Application.MainForm.Show;<br>end;<br><br>退出时加 Shell_NotifyIconA(NIM_DELETE, @NTID); 去托盘图标
 
接受答案了.
 
后退
顶部