如何将程序图标添加到系统托盘中?(50分)

  • 主题发起人 主题发起人 shijiesun3
  • 开始时间 开始时间
S

shijiesun3

Unregistered / Unconfirmed
GUEST, unregistred user!
找到了一段代码
//工程文件代码
Application.Initialize;
Application.CreateForm(TForm1,Form1);
Application.CreateForm(TForm2,Form2);
Application.ShowMainForm:=False;
Application.OnMinimize:=Form1.FormMin;
Application.Run;

//主窗体程序代码
procedure TForm1.IconClick(var Msg:TMessage);
var Point:TPoint;
begin
if msg.LParam=WM_LBUTTONDOWN then
begin
if Form2.Visible=False then
begin
Form2.WindowState:=wsNormal;
Form2.Show;
end
Else
Form2.Hide;
end
else if msg.LParam=WM_RBUTTONDOWN then
begin
GetCursorPos(Point);
PopupMenu1.Popup(point.X,point.Y);
end;
end;
//在主窗体创建时安装托盘图标
procedure TForm1.FormCreate(Sender:TObject);
var
pIcon:TNotifyIcondata;
begin
pIcon.Wnd:=Handle;
pIcon.uID:=10;
pIcon.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
pIcon.uCallbackMessage:=CM_Icon.Handle;
pIcon.szTip:='托盘程序';
Shell_NotifyIcon(NIM_ADD,@pIcon);
end;
//在主窗体关闭时,卸载托盘图标
procedure TForm1.FormClose(Sender:TObject;var Action:TCloseAction);
var
pIcon:TNotifyIcondata;
begin
pIcon.Wnd:=Handle;
pIcon.uID:=10;
pIcon.hIcon:=Application.Icon.Handle;
Shell_NotifyIcon(NIM_Delete,@pIcon);
end;
自定义过程,应用程序最小化时执行该过程
procedure TForm1.FormMin(Sender:TObject);
begin
Hide;
end;

那个参数代表程序图标?
 
pIcon.hIcon:=Application.Icon.Handle;
 
这是句柄吧?只知道图标的文件名,从那里获得图标的句柄呢?
 
是句柄啊。
知道图像的文件名,要获得图标的句柄要先用Picture控件载入才行。
 
image控件也行吧?
 
编译后显示:
CM_IconMsg Undeclared identifier
Undeclared identifier:Hide
Hide是不是像Close等一样可以使用的函数?
 
好像有个控件吧。
 
这段代码编译通不过是怎么回事?

unit Unit1;

interface

uses
Windows, Messages, ShellAPI, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender:TObject;var Action:TCloseAction);

private
{ Private declarations }
public
{ Public declarations }
IconData:TNotifyIconData;
end;
function Shell_NotifyIcon(dwMessage:DWORD;lpData:PNotifyIconData):BOOL;stdcall;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
IconData.cbSize:=sizeof(IconData);
IconData.Wnd:=Handle;
IconData.uID:=100;
IconData.uFlgs:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
IconData.uCallbackMessage:=WM_USER+1;
IconData.hIcon:=Application.Icon.Handle;

StrPCopy(IconData.szTip,Application.Title);
Shell_notifyIcon(NIM_ADD,@IconData);
end;

procedure TForm1.FormClose(Sender:TObject;var Action:TCloseAction);
begin
Shell_NotifyIcon(NIM_DELETE,@IconData);
Application.ProcessMessages;
Application.Terminate;
end;

end.


[Error] Unit1.pas(33):Undeclared identifier:'uFlgs'
[Error] Unit1.pas(20):Unsatisfied forward or external declaration:'Shell_NotifyIcon'
[Fatal Error] Project1.dpr(5):Could not compile used unit 'Unit1.pas'
 
[Error] Unit1.pas(33):Undeclared identifier:'uFlgs'
[red]uFlgs 是 uFlags 才对[/red]
[Error] Unit1.pas(20):Unsatisfied forward or external declaration:'Shell_NotifyIcon'
[red]function Shell_NotifyIcon(dwMessage:DWORD;lpData:PNotifyIconData):BOOL;stdcall; //ShellApi单元中已经有声明,你重复声明了[/red]
 
接受答案了.
 

Similar threads

S
回复
0
查看
758
SUNSTONE的Delphi笔记
S
S
回复
0
查看
795
SUNSTONE的Delphi笔记
S
S
回复
0
查看
906
SUNSTONE的Delphi笔记
S
S
回复
0
查看
884
SUNSTONE的Delphi笔记
S
后退
顶部