在单击任务栏图标后,如果不点击菜单,而点击桌面的其他地方的话,菜单不会消失,请问如何解决。
以下是程序段,谢谢!!!(最后是消息处理部分,如果可以的话请给Image1点建议)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,ShellAPI,Menus, ExtCtrls;const
WM_TRAYNOTIFY=WM_USER+1; //定义通知消息type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
l1: TMenuItem;
r1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;var
Form1: TForm1;
nd:NotifyIconData;
hs:LongWord;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
hs:=form1.Image1.Picture.Icon.Handle;
nd.cbSize := sizeof(NotifyIconData);
nd.Wnd := handle;
nd.uID := 0;
nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd.uCallbackMessage := WM_TRAYNOTIFY;
nd.hIcon := hs;
StrPLCopy(nd.szTip, 'RememberURL', 63);
Shell_NotifyIcon(NIM_ADD, @nd);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nd);
end;procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt); //通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
form1.PopupMenu1.Popup(pt.X,pt.Y);
//鼠标左键被按下
end;
WM_RBUTTONDOWN:
begin
form1.PopupMenu2.Popup(pt.X,pt.Y);
//鼠标右键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
end.Nunit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI,Menus, ExtCtrls;const
WM_TRAYNOTIFY=WM_USER+1; //定义通知消息type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
l1: TMenuItem;
r1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;var
Form1: TForm1;
nd:NotifyIconData;
hs:LongWord;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
hs:=form1.Image1.Picture.Icon.Handle;
nd.cbSize := sizeof(NotifyIconData);
nd.Wnd := handle;
nd.uID := 0;
nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd.uCallbackMessage := WM_TRAYNOTIFY;
nd.hIcon := hs;
StrPLCopy(nd.szTip, 'RememberURL', 63);
Shell_NotifyIcon(NIM_ADD, @nd);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nd);
end;procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt); //通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
form1.PopupMenu1.Popup(pt.X,pt.Y);
//鼠标左键被按下
end;
WM_RBUTTONDOWN:
begin
form1.PopupMenu2.Popup(pt.X,pt.Y);
//鼠标右键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
end.
以下是程序段,谢谢!!!(最后是消息处理部分,如果可以的话请给Image1点建议)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,ShellAPI,Menus, ExtCtrls;const
WM_TRAYNOTIFY=WM_USER+1; //定义通知消息type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
l1: TMenuItem;
r1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;var
Form1: TForm1;
nd:NotifyIconData;
hs:LongWord;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
hs:=form1.Image1.Picture.Icon.Handle;
nd.cbSize := sizeof(NotifyIconData);
nd.Wnd := handle;
nd.uID := 0;
nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd.uCallbackMessage := WM_TRAYNOTIFY;
nd.hIcon := hs;
StrPLCopy(nd.szTip, 'RememberURL', 63);
Shell_NotifyIcon(NIM_ADD, @nd);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nd);
end;procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt); //通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
form1.PopupMenu1.Popup(pt.X,pt.Y);
//鼠标左键被按下
end;
WM_RBUTTONDOWN:
begin
form1.PopupMenu2.Popup(pt.X,pt.Y);
//鼠标右键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
end.Nunit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI,Menus, ExtCtrls;const
WM_TRAYNOTIFY=WM_USER+1; //定义通知消息type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
l1: TMenuItem;
r1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;var
Form1: TForm1;
nd:NotifyIconData;
hs:LongWord;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
hs:=form1.Image1.Picture.Icon.Handle;
nd.cbSize := sizeof(NotifyIconData);
nd.Wnd := handle;
nd.uID := 0;
nd.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd.uCallbackMessage := WM_TRAYNOTIFY;
nd.hIcon := hs;
StrPLCopy(nd.szTip, 'RememberURL', 63);
Shell_NotifyIcon(NIM_ADD, @nd);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nd);
end;procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt); //通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
form1.PopupMenu1.Popup(pt.X,pt.Y);
//鼠标左键被按下
end;
WM_RBUTTONDOWN:
begin
form1.PopupMenu2.Popup(pt.X,pt.Y);
//鼠标右键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
end.
我到是碰到一个问题(以下是我的一个提问):我的程序做了一个托盘图标。如果最小化时:
ShowWindow(Application.Handle,SW_HIDE);
showWindow(FrmFh.Handle,SW_HIDE);点击托盘图标时:
ShowWindow(Application.Handle,SW_NORMAL);
ShowWindow(FrmFh.Handle,SW_NORMAL);
或
ShowWindow(Application.Handle,SW_RESTOR);
ShowWindow(FrmFh.Handle,SW_NORMAL);都程序隐藏后再重新显示后,会在处理用户操作弹出其他窗口后不响应
用户操作。这是为什么?当隐藏程序后应该怎么样来重新显示程序,才能让它和隐藏前一样能响应用户操作呢?
看看高手怎么说。。