如何在一个模态窗口中实现托盘程序?
我想实现这样的功能,在Form1中点击按钮,弹出模态窗口Form2,在Form2中点击按钮,整个软件隐藏到
托盘中。
我用下面的代码,结果Form1不会隐藏,该怎么办?
Form1代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
form2.showModal;
end;
Form2代码:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI;
const
CDSNAPSHOT_TRAY_ICON=1001;
WM_TRAYNOTIFY=2308;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure WMTrayNotify(var Msg: TMessage); message WM_TRAYNOTIFY;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
nid: TNOTIFYICONDATA;
begin
nid.cbSize:=sizeOf(TNOTIFYICONDATA);
nid.Wnd:=Application.Handle;
nid.uID:=CDSNAPSHOT_TRAY_ICON;
nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
nid.uCallbackMessage:=WM_TRAYNOTIFY;
nid.hicon:=Application.Icon.Handle;
nid.szTip:='PChar(Application.Title)';
ShowWindow(self.ParentWindow,SW_HIDE);
ShowWindow(Application.Handle,SW_HIDE);
ShowWindow(Handle,SW_HIDE);
Shell_NotifyIcon(NIM_ADD,@nid);
end;
procedure TForm2.WMTrayNotify(var Msg: TMessage);
var
p: TPoint;
begin
if (Msg.LParam=WM_LBUTTONDOWN) Then
begin
ShowWindow(Application.Handle,SW_SHOW);
ShowWindow(Handle,SW_SHOW);
Application.Restore;
SetForegroundWindow(GetLastActivePopup(self.Handle));
end;
end;
end.
问题点数:20、回复次数:4Top
1 楼gzmhero(hihihi)回复于 2005-03-04 13:17:20 得分 8
强制设置不行吗。
ShowWindow(Form1.Handle,SW_HIDE);
你的代码有一点错误,现在执行,WMTrayNotify不会回应,
procedure TForm2.Button1Click(Sender: TObject);
var
nid: TNOTIFYICONDATA;
begin
nid.cbSize:=sizeOf(TNOTIFYICONDATA);
nid.Wnd:=Application.Handle;////////////////////////////////////句柄是Application的,改为---------------------------------------》》
nid.Wnd:=Handle;///////当前窗口的句柄,这样函数WMTrayNotify会回应的。Top
2 楼wuyu1981(方圆)回复于 2005-03-04 13:18:47 得分 3
我想:托盘图标对象应该放到Form1中吧,然后发一个消息控制Form1窗体关闭,但是不释放。Top
3 楼g961681(技术庸人(情商太低))回复于 2005-03-04 13:26:52 得分 5
ShowWindow(self.ParentWindow,SW_HIDE);中
self.ParentWindow改为Application.MainForm.Handle试试Top
4 楼g961681(技术庸人(情商太低))回复于 2005-03-04 13:27:23 得分 4
self.ParentWindow好象是为空的!Top




