如何将delphi程序最小化到系统托盘中?

shepher 2003-10-27 11:39:29
如题
...全文
430 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bl7009 2003-10-28
  • 打赏
  • 举报
回复
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI, StdCtrls, Menus;
const WM_NID=WM_USER+1000;
type


TForm2 = class(TForm)
PopupMenu1: TPopupMenu;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
NotifyIcon:TNotifyIconData;
procedure WMNID(var msg:TMessage);message WM_NID;
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
Begin
//NotifyIcon为全局变量,在程序的开头已经定义了 
with NotifyIcon do
begin
cbSize:=SizeOf(TNotifyIconData);
Wnd:=Handle; //指向当前窗体Form1的句柄 
uID:=1;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallBackMessage:=WM_NID;
hIcon:=Application.Icon.Handle;
szTip:='张家恶少';
end;
//把设置好的变量NotifyIcon加入到系统中以便处理 
Shell_NotifyIcon(NIM_ADD,@NotifyIcon);

End;
procedure tform2.WMNID(var msg:TMessage);
begin
case msg.LParam of
WM_LBUTTONUp:Form2.Visible:=not Form2.Visible;
//WM_RBUTTONUP: ShowMessage('您点击的是右键');
WM_RBUTTONUP:
begin
PopupMenu1.Popup(mouse.CursorPos.X,mouse.CursorPos.Y);
end;
End;
End;


end.

micher_yan 2003-10-28
  • 打赏
  • 举报
回复
比如RX的RXtrayIcon就可以轻松搞定,而且支持鼠标事件
vagerent 2003-10-28
  • 打赏
  • 举报
回复
用第三方控件吧,很轻松的
kingting 2003-10-28
  • 打赏
  • 举报
回复
如果你用RX系统控件的话就很简单
原来我用系统API作,但结果却不如用RxTrayIcon感觉效果好
而且它也有例子,很快就可以搞定
atuchina 2003-10-27
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellapi, StdCtrls;
const
MY_MESSAGE = wm_user+100;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
procedure OnIconNotify(var Message: TMessage);message MY_MESSAGE;

public
{ Public declarations }
end;

var
Form1: TForm1;
procedure AddSystray(hnd:HWnd);
procedure DelSystray(hnd:HWnd);
implementation

{$R *.dfm}
procedure AddSystray(hnd:HWnd);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid变量的字节数
nid.Wnd := hnd; // 主窗口句柄
nid.uID := 11; // 内部标识,可设为任意数
nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?
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; // 指明哪些字段有?
if not Shell_NotifyIcon(NIM_ADD, @nid) then
begin
ShowMessage('Add systray Failed!');
end;
end;

procedure DelSystray(hnd:HWnd);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid变量的字节数
nid.cbSize := sizeof(nid); // nid变量的字节数
nid.uID := 11; //内部标识,与加入小图标时的数一致
nid.Wnd := hnd; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
end;

{ TForm1 }

procedure TForm1.OnIconNotify(var Message: TMessage);
var
Busy: Boolean;
begin
Busy:=False;
if not Busy then
begin
Busy := true;
if Message.LParam=WM_LBUTTONDOWN then
begin
if Application.MessageBox('Are you sure','Exit', MB_YESNO)=IDYES then
show;
end;
end;
Busy := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
AddSystray(handle);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DelSystray(handle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
hide;
end;

end.

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧