procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel; const Rect: TRect); begin with StatusBar1.Canvas do begin ImageList1.Draw(StatusBar1.Canvas,Rect.Left,Rect.Top,Panel.Index);//取ImageList1中的图标 TextOut(Rect.Left+30,Rect.Top+2,StatusBar1.Panels[0].Text);//由于增加了图标~所以原来Panel的文字右移~~ end; end;
别忘了把 StatusBar1.Panels[0].Style设置为psOwnerDraw
//自己做一个可接受控件的状态栏,想放什么就放什么。 unit StatusBarExt;interfaceuses Windows, Messages, SysUtils, Classes, Controls, ComCtrls;type TStatusBarExt = class(TStatusBar) private { Private declarations } protected { Protected declarations } public { Public declarations } constructor Create(AOwner: TComponent);override; published { Published declarations } end;procedure Register;implementationprocedure Register; begin RegisterComponents('rainbowsoft', [TStatusBarExt]); end;{ TStatusBarExt }constructor TStatusBarExt.Create(AOwner: TComponent); begin inherited; ControlStyle := ControlStyle + [csAcceptsControls]; end;end.
http://delphi.home.sohu.com/Delphi中任务栏状态区的编程-------------------------------------------------------------------------------- 在Windows桌面的任务栏上有一个凹陷的区域,其中显示着系统时钟以及一些图标,这个长方形的区域便是Windows的任务栏状态区(taskbar status area)。本文将介绍使用Borland Delphi进行任务栏状态区的编程,即怎样将应用程序的图标显示在任务栏状态区中。 实现原理 任务栏状态区的图标添加、删除、以及修改是通过Windows API函数Shell_NotifyIcon来实现的,该函数是由Windows的SHELL32.DLL动态联接库提供的。在Delphi中,Shell_NotifyIcon函数是在ShellAPI单元声明的,其 函数原型如下: function Shell_NotifyIcon(dwMessage: DWORD; lpData: PNotifyIconData): BOOL; stdcall; 其中参数dwMessage的取值决定函数Shell_NotifyIcon所要进行的操作的类型,它的取值可以是以下三者之一: NIM_ADD(值为0):在任务栏状态区插入一个图标。 NIM_DELETE(值为1):从任务栏状态区删除一个图标。 NIM_MODIFY(值为2):修改任务栏状态区的图标、提示信息、或者通知消息。 参数lpData 是一个记录类型(结构类型)的指针,记录类型NotifyIconData的定义如下: NOTIFYICONDATA = record cbSize: DWORD; Wnd: HWND; uID: UINT; uFlags: UINT; uCallbackMessage: UINT; hIcon: HICON; szTip: array [0..63] of AnsiChar; end; ---- cbSize:NOTIFYICONDATA记录的大小。 Wnd:与此状态区图标相关联的窗口句柄,此窗口将负责处理uCallbackMessage消息。 uID:程序自定义的状态区图标的标识符。 uFlags:这个字段指明NOTIFYICONDATA记录中的成员uCallbackMessage、hIcon和szTip这三者的哪些项的值有效。它的取值可以是下列三者的组合(or运算): NIF_MESSAGE (值为1):uCallbackMessage项包含了有效的信息。 NIF_ICON(值为2):hIcon项包含了有效的信息。 NIF_TIP(值为4): szTip项包含了有效的信息。 uCallbackMessage:程序定义的消息标识符(32位的整数)。当鼠标在状态区图标上移动或者点击(即,发生了鼠标事件)时,操作系统将向Wnd指定的那个窗口发送uCallbackMessage消息。在uCallbackMessage消息中,lParam参数包含了Windows的鼠标消息的类型,而wParam参数则包含了图标标识(即uID)。有效的鼠标消息包括以下几个:WM_LBUTTONDOWN、WM_RBUTTONDOWN、WM_MBUTTONDOWN、WM_LBUTTONUP、WM_RBUTTONUP、WM_MBUTTONUP、WM_MOUSEMOVE、WM_LBUTTONDBLCLK、WM_RBUTTONDBLCLK以及WM_MBUTTONDBLCLK。 hIcon:指定一个图标句柄。 szTip:显示在图标上的提示信息(少于63个字符)。 Delphi中的实现 通过上面的介绍中,我们不难看出,任务栏状态区的编程主要是处理两方面的工作:添加、删除、修改图标;以及处理通知消息。对于图标的添加、删除、修改操作,可以通过调用Shell_NotifyIcon函数来实现。而对于自定义的通知消息,我们就应该在消息循环中给予处理了。 下面的示例给出了状态区图标的添加、修改和删除操作的例子,以及图标的通知消息的基本处理框架。 unit Unit1;interfaceuses 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.西安交通大学 刘明华
Panel: TStatusPanel; const Rect: TRect);
begin
with StatusBar1.Canvas do
begin
ImageList1.Draw(StatusBar1.Canvas,Rect.Left,Rect.Top,Panel.Index);//取ImageList1中的图标
TextOut(Rect.Left+30,Rect.Top+2,StatusBar1.Panels[0].Text);//由于增加了图标~所以原来Panel的文字右移~~
end;
end;
StatusBar1.Panels[0].Style设置为psOwnerDraw
unit StatusBarExt;interfaceuses
Windows, Messages, SysUtils, Classes, Controls, ComCtrls;type
TStatusBarExt = class(TStatusBar)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
published
{ Published declarations }
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('rainbowsoft', [TStatusBarExt]);
end;{ TStatusBarExt }constructor TStatusBarExt.Create(AOwner: TComponent);
begin
inherited;
ControlStyle := ControlStyle + [csAcceptsControls];
end;end.
cbSize: DWORD;
Wnd: HWND;
uID: UINT;
uFlags: UINT;
uCallbackMessage: UINT;
hIcon: HICON;
szTip: array [0..63] of AnsiChar;
end;
---- cbSize:NOTIFYICONDATA记录的大小。 Wnd:与此状态区图标相关联的窗口句柄,此窗口将负责处理uCallbackMessage消息。 uID:程序自定义的状态区图标的标识符。 uFlags:这个字段指明NOTIFYICONDATA记录中的成员uCallbackMessage、hIcon和szTip这三者的哪些项的值有效。它的取值可以是下列三者的组合(or运算): NIF_MESSAGE (值为1):uCallbackMessage项包含了有效的信息。 NIF_ICON(值为2):hIcon项包含了有效的信息。 NIF_TIP(值为4): szTip项包含了有效的信息。 uCallbackMessage:程序定义的消息标识符(32位的整数)。当鼠标在状态区图标上移动或者点击(即,发生了鼠标事件)时,操作系统将向Wnd指定的那个窗口发送uCallbackMessage消息。在uCallbackMessage消息中,lParam参数包含了Windows的鼠标消息的类型,而wParam参数则包含了图标标识(即uID)。有效的鼠标消息包括以下几个:WM_LBUTTONDOWN、WM_RBUTTONDOWN、WM_MBUTTONDOWN、WM_LBUTTONUP、WM_RBUTTONUP、WM_MBUTTONUP、WM_MOUSEMOVE、WM_LBUTTONDBLCLK、WM_RBUTTONDBLCLK以及WM_MBUTTONDBLCLK。 hIcon:指定一个图标句柄。 szTip:显示在图标上的提示信息(少于63个字符)。 Delphi中的实现 通过上面的介绍中,我们不难看出,任务栏状态区的编程主要是处理两方面的工作:添加、删除、修改图标;以及处理通知消息。对于图标的添加、删除、修改操作,可以通过调用Shell_NotifyIcon函数来实现。而对于自定义的通知消息,我们就应该在消息循环中给予处理了。 下面的示例给出了状态区图标的添加、修改和删除操作的例子,以及图标的通知消息的基本处理框架。 unit Unit1;interfaceuses
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.西安交通大学 刘明华