Firing_sky 说得很详细了,但是要做通知栏的图标也很简单,应用几个函数(自己写),
InstallIcon,UnInstallIcon,IconClick.
下面是他们的简单实例:
安装图表:
procedure TForm1.InstallIcon;
var
CDIcon:TNotifyICondata;
begin
normalIcon:=TIcon.Create;
normalIcon:=form1.Icon;
CDIcon.cbSize:=sizeof(CDIcon);
CDIcon.Wnd:=handle;
CDIcon.uID:=ICON_ID;
CDIcon.uFlags:= NIF_ICON or NIF_MESSAGE or NIF_TIP;
CDIcon.uCallbackMessage:=MI_ICONEVENT;
CDIcon.hIcon:=normalicon.Handle;
CDIcon.szTip:='光驱控制';
Shell_NotifyIcon(NIM_ADD,@CDIcon);
end;
卸载图标:
procedure TForm1.UnInstallIcon;
var
CDIcon:TNotifyIconData;
begin
CDIcon.cbSize:=sizeof(CDIcon);
CDIcon.Wnd:=Handle;
CDIcon.uID:=ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@CDIcon);
// normalIcon.Free;
end;
响应click事件:
procedure TForm1.IconOnClick(var message:TMessage);
var
p:TPoint;
begin
if (message.LParam=WM_LBUTTONDBLCLK) then
ShowWindow(handle,SW_SHOW);
if (message.LParam=WM_RBUTTONDOWN) then
begin
GetCursorPos(p);
pop1.Popup(p.x,p.y);
end;
end;
TPopupStyle=Set of (Left_Click,Right_Click,Left_DbClick,Right_DbClick);
TWhoButton=(b_Left,b_Right);
TMouseEvent=
procedure(Sender:TObject;x,y:Word;WhoButton:TWhoButton)
of Object;
procedure Register;
begin
RegisterComponents('MyControl', [TNotifyIcon]);
end;
procedure TNotifyIcon.ShowIt(Accept:boolean);
begin
if Accept=true then ShowIcon
else HideIcon;
end;
procedure TNotifyIcon.Paint;
begin
if (csDesigning in ComponentState) then
begin
Width:=33;
Height:=33;
With Canvas do
begin
Brush.Color:=clInfoBk;
Ellipse(0,0,Self.Width,Self.Height);
Font.Color:=clBlue;
Brush.Style:=bsClear;
FloodFill(5,5,clInfoBk,fsBorder);
Brush.Color:=clInfoBk;
TextOut(3,Self.Height div 2-6,'Notify');
end
end;
end;
procedure TNotifyIcon.NotifyIconClick(var msg : TMessage);
var p:TPoint;
begin
try
case msg.LParam of
WM_LBUTTONDOWN:
begin
GetCursorPos(p);
if Left_Click in FPopupStyle then
begin
SetForegroundWindow(ParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
FOnIconMouseDown(Self,p.x,p.y,b_Left);
end;
end;
WM_RBUTTONDOWN:
begin
GetCursorPos(p);
if Right_Click in FPopupStyle then
begin
SetForegroundWindow(ParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconMouseDown) then
begin
FOnIconMouseDown(Self,p.x,p.y,b_Right);
end;
end;
WM_LBUTTONDBLCLK:
begin
GetCursorPos(p);
if Left_DbClick in FPopupStyle then
begin
SetForegroundWindow(ParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
FOnIconDoubleClick(Self,p.x,p.y,b_Left);
end;
end;
WM_RBUTTONDBLCLk:
begin
GetCursorPos(p);
if Right_Click in FPopupStyle then
begin
SetForegroundWindow(ParentWindow);
FPopupMenu.Popup(p.x,p.y);
end;
if Assigned(FOnIconDoubleClick) then
begin
FOnIconDoubleClick(Self,p.x,p.y,b_Right);
end;
end;
end;
except
end;
end;
function MAKELANGID(p, s:word):Cardinal;
begin
result:= (((s)shl 10) or(p));
end;
constructor TNotifyIcon.Create(AOwner: TComponent);
begin
try
inherited Create(AOwner);
FIcon:=TIcon.Create;
Height:=36;
Width:=36;
Visible:=false;
FTitle:='Welcome';
FIconVisible:=false;
//-------------set tray info---------
ParentWindow:=TWinControl(AOwner).Handle;
New(Fpda);
With FPda^ do
begin
uCallbackMessage:=WM_MY_Notify;
cbsize:=SizeOf(FPda^);
uID:=200;
wnd:=Handle;
uFlags:=NIF_ICON+NIF_Tip+NIF_MESSAGE;
end;
if (csDesigning in ComponentState) then
begin
if GetUserDefaultLCID = MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED) then
Application.MessageBox(
'Write by 南昌大学 付昱纲'#13#13'E-mail:fyg@163.net'#13#13' 1998.8.17',
'TNotifyIcon 控件 V 1.01',MB_OK+ MB_ICONINFORMATION)
else
Application.MessageBox(
'Write by NanChang University FuYuGang'#13#13'E-mail:fyg@163.net'#13#13' 1998.8.17',
'TNotifyIcon Component V 1.01',MB_OK+ MB_ICONINFORMATION);
end;
except
ShowMessage('TNotifyIcon Create error');
end;
end;
procedure TNotifyIcon.SetIcon(Icon:TICON);
begin
FIcon.Assign(Icon);
end;
procedure TNotifyIcon.ShowIcon;
begin
try
if FIcon.Handle=0 then
begin
Exit;
end;
if FIcon.Handle<>FPda^.hIcon then
HideIcon;
if FIconVisible=false then
begin
FPda^.hIcon:=FIcon.handle;
FIconVisible:=true;
Shell_NotifyIcon(NiM_ADD,FPda);
end;
except
ShowMessage('TNotifyIcon Show Error ');
end;
end;
procedure TNotifyIcon.HideIcon;
begin
try
if FIconVisible then
begin
FIconVisible:=false;
Shell_NotifyIcon(NiM_DELETE,FPda);
end;
except
ShowMessage('TNotifyIcon Hide Error');
end;
end;
procedure TNotifyIcon.SetTitle(NewTitle:string);
begin
FTitle:=NewTitle;
StrCopy(FPda^.szTip,PChar(FTitle));
if FIconVisible then
begin
HideIcon;
ShowIcon;
end;
end;