uses ShellApi;
原函数声明如下:
function Shell_NotifyIcon(dwMessage
WORD;lpData
NotifyIconData);Bool:stdCall
下面是我写的相关的代码,可以实现托盘,仅供参考:
注意:图标是在当前工程目录下。
program MainproTray;
uses
Windows,
Forms,
TrayU in 'TrayU.pas' {MainFrm};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TMainFrm, MainFrm);
Application.ShowMainForm:=false;
//if false,MainFrm not visible 在任务栏中不出现
Application.Run;
end.
//////////////////////
unit TrayU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ShellApi,
Menus, jpeg, ExtCtrls;
const
WM_SHOWINFO=WM_USER+1;
ICON_ID=1;
type
TMainFrm = class(TForm)
PopupMenu1: TPopupMenu;
Open1: TMenuItem;
Exit1: TMenuItem;
Image1: TImage;
procedure Open1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure InstallIcon;
procedure ShowInfoClick(var Msg:TMessage);message WM_SHOWINFO;
private
{ Private declarations }
myIcon:TIcon;
protected
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
//IconData:TNotifyIconData;
implementation
{$R *.DFM}
procedure TMainFrm.Open1Click(Sender: TObject);
begin
MainFrm.Show;
//在任务栏隐藏
ShowWindow(Application.Handle,SW_HIDE);
end;
procedure TMainFrm.Exit1Click(Sender: TObject);
begin
//Shell_NotifyIcon(NIM_DELETE,@IconData);
Application.ProcessMessages;
Application.Terminate;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
begin
//BorderIcons:=[biSystemMenu];
InstallIcon;
ShowWindow(MainFrm.Handle,SW_HIDE);
end;
procedure TMainFrm.FormClose(Sender: TObject; var Action: TCloseAction);
var
IconData:TNotifyIconData;
begin
IconData.cbSize:=sizeof(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@IconData);
myIcon.Free;
//Action:=caNone;
//MainFrm.Hide;
end;
procedure TMainFrm.ShowInfoClick(var Msg: TMessage);
var
p:TPoint;
begin
if Msg.LParam=WM_LBUTTONDOWN then
begin
MainFrm.Show;
//ShowWindow(Application.Handle,SW_HIDE);
end;
if Msg.LParam=WM_RBUTTONDOWN then
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
end;
end;
procedure TMainFrm.InstallIcon;
var
IconData:TNotifyIconData;
begin
myIcon:=TIcon.Create;
myIcon.LoadFromFile('NormalIcon.ico'); //使用自己定义的图标
with IconData do
begin
cbSize:=sizeof(IconData);
//Wnd:=Handle;ok
Wnd:=MainFrm.Handle;
//Wnd:=Application.Handle; //Application.Handle why not?
uID:=ICON_ID;
uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
uCallBackMessage:=WM_SHOWINFO;
//hIcon:=LoadIcon(hInstance,'NormalIcon');
//hIcon:=Application.Icon.Handle;
hIcon:=myIcon.Handle;
StrPCopy(szTip,'myTrayIcon');
end;
Shell_NotifyIcon(NIM_ADD,@IconData);
end;
end.