如何实现系统托盘的动态图标

xiaofengking 2003-03-21 04:38:50
就像QQ那样实现动态的变换图标,如果启动任务就把A.ico,B.ico,C.ico三个图标,不停的更换起到一个动画效果,就知道任务在启动,结束后恢复默认图标。
...全文
52 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hch_45 2003-03-21
  • 打赏
  • 举报
回复
贴给你完整版
hch_45 2003-03-21
  • 打赏
  • 举报
回复
unit Unit1;

interface

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

const
WM_TRAYNOTIFY=WM_USER+1;//定义通知消息

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;

var
Form1: TForm1;
nd0, nd1:NotifyIconData;
hs:array[0..9]of LongWord;

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
//加载Icon0..Icon9这10个图标资源,
并且保存它们的句柄。
//图标Icon0..Icon9分别对应与0..9这9个数字。
hs[0]:=LoadIcon(hInstance, 'Icon0');
hs[1]:=LoadIcon(hInstance, 'Icon1');
hs[2]:=LoadIcon(hInstance, 'Icon2');
hs[3]:=LoadIcon(hInstance, 'Icon3');
hs[4]:=LoadIcon(hInstance, 'Icon4');
hs[5]:=LoadIcon(hInstance, 'Icon5');
hs[6]:=LoadIcon(hInstance, 'Icon6');
hs[7]:=LoadIcon(hInstance, 'Icon7');
hs[8]:=LoadIcon(hInstance, 'Icon8');
hs[9]:=LoadIcon(hInstance, 'Icon9');

//填充NotifyIconData记录型变量nd0
nd0.cbSize := sizeof(NotifyIconData);
nd0.Wnd := handle;
nd0.uID := 0;
nd0.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd0.uCallbackMessage := WM_TRAYNOTIFY;
nd0.hIcon := hs[0];
StrPLCopy(nd0.szTip, 'Hello, World!', 63);

//填充NotifyIconData记录型变量nd1
nd1.cbSize := sizeof(NotifyIconData);
nd1.Wnd := handle;
nd1.uID := 1;
nd1.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd1.uCallbackMessage := WM_TRAYNOTIFY;
nd1.hIcon := hs[0];
StrPLCopy(nd1.szTip, 'Simon Loves Daisy', 63);

//在任务栏状态区添加图标
Shell_NotifyIcon(NIM_ADD, @nd0);
Shell_NotifyIcon(NIM_ADD, @nd1);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
st:SystemTime;
begin
//每秒钟更新一次图标:图标0显示秒数的十位,
图标1显示秒数的个位。
GetLocalTime(st);
nd0.hIcon := hs[st.wSecond div 10];
nd1.hIcon := hs[st.wSecond mod 10];
//修改任务栏状态区的图标
Shell_NotifyIcon(NIM_MODIFY, @nd0);
Shell_NotifyIcon(NIM_MODIFY, @nd1);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
//将图标从任务栏状态区删除
Shell_NotifyIcon(NIM_DELETE, @nd0);
Shell_NotifyIcon(NIM_DELETE, @nd1);
end;

//处理 通知消息
procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt);

//通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
//鼠标右键被按下
end;
WM_RBUTTONDOWN:
begin
//鼠标左键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;

end.
xylyge 2003-03-21
  • 打赏
  • 举报
回复
unit Unit1;

interface

{ 记住在uses部分中包括 ShellAPI}
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ShellAPI, StdCtrls;

{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}
{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}
const MY_MESSAGE = WM_USER + 100;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
private
procedure OnIconNotify(var Message: TMessage);
message MY_MESSAGE;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


{当小图标捕捉到鼠标事件时进入此过程}
{当小图标捕捉到鼠标事件时进入此过程}
procedure TForm1.OnIconNotify(var Message: TMessage);
const
Busy: Boolean = false;
begin
if not Busy then begin
Busy := true;
if Message.LParam=WM_LBUTTONDOWN then
if Application.MessageBox('Are you sure',
'Exit', MB_YESNO)=IDYES then Close;
Busy := false;
end;
end;

{当主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.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('Failed!');
Application.Terminate;
end;
{将程序的窗口样式设为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.cbSize := sizeof(nid); // nid变量的字节数
nid.uID := -1; //内部标识,与加入小图标时的数一致
nid.Wnd := Handle; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
end;

{主窗口初始化完毕并显示时将激活Paint重画事件,此时将主窗口隐藏}
procedure TForm1.FormPaint(Sender: TObject);
begin
Hide;
end;

end.
贴给你一段!
huojiehai 2003-03-21
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1056/1056654.xml?temp=.1269495

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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