怎么使delphi小程序可以最小化到系统托盘?
发布网友
发布时间:2022-04-23 06:57
我来回答
共2个回答
热心网友
时间:2022-06-17 03:13
Shell_NotifyIcon这个API可以实现系统托盘图标功能,然后你在自定义一下最小化的消息就可以了,图标功能有个例子
托盘就是在任务栏右下角的小图标。创建、更改、删除只需要一个Windows API就能实现。这个函数叫做Shell_NotifyIcon()。这个函数和其他处理Windows 外壳的函数都包含在ShellAPI单元中。本例程在窗口创建时在任务栏的右下角创建一个托盘图标。图标由两个ICON交替出现。当窗口最小化时任务栏中只有托盘图标,当左键单击托盘图标窗口恢复。图标停止交替。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,SHELLAPI,
Menus, Dialogs,
ExtCtrls, ImgList;
const
MI_ICONEVENT = WM_USER + 1;
ICON_ID = 10;
type
TForm1 = class(TForm)
Timer1: TTimer;
PopupMenu2: TPopupMenu;
N1: TMenuItem;
ImageList1: TImageList;
procere N1Click(Sender: TObject);
procere FormCreate(Sender: TObject);
procere FormDestroy(Sender: TObject);
procere FormClose(Sender: TObject; var Action: TCloseAction);
procere Timer1Timer(Sender: TObject);
private
procere modifyicon;
procere IconOnClick(var message: TMessage); MESSAGE MI_ICONEVENT;
procere tranmessage(var m:TWMSYSCOMMAND); message WM_SYSCOMMAND;
public
{ Public declarations }
end;
var
Form1: TForm1;
ydic:array [0..1] of ticon;
icint:integer;
xsyc:BOOLEAN;
implementation
{$R *.DFM}
procere TForm1.N1Click(Sender: TObject);
begin
APPLICATION.Terminate ;
end;
procere TForm1.FormCreate(Sender: TObject);
var
IconData: TNotifyIconData;
begin
xsyc:=true;
ydic[0]:=ticon.Create;
ydic[1]:=ticon.Create;
ydic[0].Handle :=loadicon(hinstance,'icon0');//资源文件(ICONS.RES)
ydic[1].Handle :=loadicon(hinstance,'icon1');
IconData.cbSize:=SizeOf(IconData );
IconData.Wnd:= form1.Handle;
IconData.uID:= ICON_ID;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := MI_ICONEVENT;
IconData.hIcon :=ydic[0].Handle;
IconData.szTip := '我笑、我笑、笑笑笑';
icint:=0;
Shell_NotifyIcon( NIM_ADD, @IconData );
ShowWindow(Application.Handle, SW_HIDE );
end;
procere TForm1.FormDestroy(Sender: TObject);
var
IconData: TNotifyIconData;
begin //卸载图标
IconData.cbSize := SizeOf( IconData );
IconData.Wnd :=form1.Handle;
IconData.uID := ICON_ID;
Shell_NotifyIcon( NIM_DELETE, @IconData );
end;
procere TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
IconData: TNotifyIconData;
begin //卸载图标
IconData.cbSize := SizeOf( IconData );
IconData.Wnd :=form1.Handle;
IconData.uID := ICON_ID;
Shell_NotifyIcon( NIM_DELETE, @IconData );
end;
procere TForm1.Timer1Timer(Sender: TObject);
begin
modifyicon
end;
procere TForm1.modifyicon;
var
IconData: TNotifyIconData;
begin
IconData.cbSize := SizeOf( IconData );
IconData.Wnd :=form1.Handle;
IconData.uID := ICON_ID;
icint:=(icint+1) mod 2;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := MI_ICONEVENT;
icondata.hIcon:= ydic[icint].Handle;
IconData.szTip := '我笑、我笑、笑笑笑';
shell_notifyicon(nim_modify,@icondata);
end;
procere TForm1.IconOnClick(var message: TMessage);
var
a:tpoint;
begin //单击事件
if (message.lParam = WM_RBUTTONDOWN) then
begin
getcursorpos(a);
PopupMenu2.Popup(a.x,a.y);
end
else if (message.lParam = WM_LBUTTONDOWN) then
begin
timer1.Enabled:=FALSE;
if xsyc then
sendmessage(form1.Handle,wm_syscommand,sc_minimize,0)
else
begin
ShowWindow(Application.Handle,SW_RESTORE);//SW_maximize
setforegroundwindow(form1.handle);
xsyc:=true;
end;
end;
end;
procere TForm1.tranmessage(var m: TWMSYSCOMMAND);
begin
if m.CmdType=SC_MINIMIZE then
begin
timer1.Enabled :=true;
sendmessage(Application.Handle,WM_SYSCOMMAND,SC_MINimize,0);
ShowWindow(Application.Handle, SW_HIDE );
xsyc:=false;
end
else
inherited;
end;
end.
最小化消息重载可以这样
procere pgame(var msg:twmsyscommand);message wm_syscommand;
procere Tmainfrm.pgame(var msg: twmsyscommand); //窗口最小化
begin
if msg.CmdType=SC_MINIMIZE then
begin
//处理代码
end;
inherited;
end;
热心网友
时间:2022-06-17 03:13
记得RXLIB控件库里有这个功能的控件,想学的话可以看下源码。