用这个函数就可以了:
Shell_NotifyIcon
给你个例子看看:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellAPI, Menus;
const
WM_TRAYICON=WM_APP+0;
type
TForm1 = class(TForm)
btnadd: TButton;
btndelete: TButton;
tryMenu: TPopupMenu;
Hello1: TMenuItem;
Exit1: TMenuItem;
procedure btnaddClick(Sender: TObject);
procedure WMTTRAYICON(var message:Tmessage);message WM_TRAYICON;
procedure FormCreate(Sender: TObject);
procedure btndeleteClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Hello1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Nidata:TNotifyIconData;
implementation{$R *.dfm}procedure TForm1.btnaddClick(Sender: TObject);begin Shell_NotifyIcon(NIM_ADD,@Nidata);
end;procedure Tform1.WMTTRAYICON(var message:Tmessage);
var
MousePos:TPoint;
begin
if message.LParam=WM_RBUTTONDOWN then
begin
setactiveWindow(form1.Handle);
Getcursorpos(MousePos);
TryMenu.Popup(MousePos.X,MousePos.Y);
end;
if message.LParam=WM_LBUTTONDBLCLK then
begin
setactiveWindow(form1.Handle);
messagebox(form1.Handle,'hello','hello',0);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with Nidata do
begin
cbsize:=sizeof(TNotifyIconData);
uID:=0;
uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
wnd:=form1.Handle;
uCallbackMessage:=WM_TRAYICON;
hIcon:=Application.Icon.Handle;
StrPCopy(szTip,'hello!');
end;
end;procedure TForm1.btndeleteClick(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@Nidata);
end;procedure TForm1.Exit1Click(Sender: TObject);
begin
application.Terminate;
end;procedure TForm1.Hello1Click(Sender: TObject);
begin
messagebox(form1.Handle,'successful!','warning',2);
end;end.
Shell_NotifyIcon
给你个例子看看:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellAPI, Menus;
const
WM_TRAYICON=WM_APP+0;
type
TForm1 = class(TForm)
btnadd: TButton;
btndelete: TButton;
tryMenu: TPopupMenu;
Hello1: TMenuItem;
Exit1: TMenuItem;
procedure btnaddClick(Sender: TObject);
procedure WMTTRAYICON(var message:Tmessage);message WM_TRAYICON;
procedure FormCreate(Sender: TObject);
procedure btndeleteClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Hello1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Nidata:TNotifyIconData;
implementation{$R *.dfm}procedure TForm1.btnaddClick(Sender: TObject);begin Shell_NotifyIcon(NIM_ADD,@Nidata);
end;procedure Tform1.WMTTRAYICON(var message:Tmessage);
var
MousePos:TPoint;
begin
if message.LParam=WM_RBUTTONDOWN then
begin
setactiveWindow(form1.Handle);
Getcursorpos(MousePos);
TryMenu.Popup(MousePos.X,MousePos.Y);
end;
if message.LParam=WM_LBUTTONDBLCLK then
begin
setactiveWindow(form1.Handle);
messagebox(form1.Handle,'hello','hello',0);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with Nidata do
begin
cbsize:=sizeof(TNotifyIconData);
uID:=0;
uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
wnd:=form1.Handle;
uCallbackMessage:=WM_TRAYICON;
hIcon:=Application.Icon.Handle;
StrPCopy(szTip,'hello!');
end;
end;procedure TForm1.btndeleteClick(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@Nidata);
end;procedure TForm1.Exit1Click(Sender: TObject);
begin
application.Terminate;
end;procedure TForm1.Hello1Click(Sender: TObject);
begin
messagebox(form1.Handle,'successful!','warning',2);
end;end.
解决方案 »
- delphi问题请教
- 关于使用Midas查询oracle的问题
- 如何得到鼠标靠近的消息
- 关于TClientSocket问题
- PsQReport做的报表有时候打开的时候会出现:“EOSError 'System Error,code:1410' 无效窗口,它属于另外一个线程”这样的错误。有谁碰到
- ***请问这样的文件流操作!代码该怎么写?***
- 一个巨菜的问题(一个刚开始看的傻瓜^^|b)
- 请问前台应用如何可以检测与后台服务器的连接。
- 关于 EmbeddedWb 的Mshtml_tlb, SHDocVw_tlb 问题!
- delphi与excel进行连接的问题,100
- 第一次编写非桌面数据库应用程序,请教DELPHI编写C/S或两层以上数据库程序的技巧
- 关于拨号的问题
unit Unit1; interface { 记住在uses部分中包括 ShellAPI}
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ShellAPI, StdCtrls;
const
MY_MESSAGE = WM_USER + 100; //自定义消息常量
type
TForm1 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.DFM}{当主Form建立时通知Windows加入小图标}
procedure TForm1.FormCreate(Sender: TObject);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid变量的字节数
nid.Wnd := Handle; // 主窗口句柄
nid.uID := -1; // 内部标识,可设为任意数
nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任指?
nid.szTip := 'This is a test application'; // 提示字符串
nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end; {程序被关闭时通知Windows去掉小图标}
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid变量的字节数
nid.uID := -1; //内部标识,与加入小图标时的数一致
nid.Wnd := Handle; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
end;
end.
procedure Minimized(Sender: TObject);
peocedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMinimize := Minimized;
end;procedure TForm1.Minimized(Sender: TObject);
begin
ShowWindow(Handle,SW_SHOWWINDOW);
end;
不要知难而退呀!我最近就在搞这个东东,通过N天的努力现在我终于搞定了。我建义你这样试试,先看一看<<Delphi5.0开发人员指南>>的Windows消息机制和扩展windows外壳那两章,然后再作吧。不要动不动就是一个第三方控件什么事也不用作了。
CoolTrayIcon,最容易用的,design time也可以preview!
用IconVisible:=true,就OK