Author:Insun
Blog:http://yxmhero1989.blog.163.com
From:www.4safer.com
Delphi的系統托盤實現的方法很多。
一般都是這樣:利用Shell_NotifyIcon這個API,然後定義下最小化的消息。此函數包含在shellapi裡,所以要use。
//SIcon.pas
unit SIcon;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellapi, Menus, ImgList, WinSkinData;
const WM_NID=wm_user+1000; //定義一個消息常量.系統規定從WM_USER開始為用戶自定義消息。
type
TFIcon1 = class(TForm)
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
ImageList1: TImageList;
SkinData1: TSkinData;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
procedure WMNID(var msg: TMessage); message WM_NID;
{ Public declarations }
end;
type TNotiFyIconData=record
cbSize:DWORD;
Wnd:HWND;
uID:UINT;
uFlags:UINT;
uCallbackMessage:UINT;
uIcon:HICON;
szTip:array[0..63] of AnsiChar;
end;
var
FIcon1: TFIcon1;
Notifyicon:TNotiFyiconData;//定義全局變量
implementation
{$R *.dfm}
procedure TFIcon1.FormCreate(Sender: TObject);
begin
with NotifyIcon do
begin
cbsize:=sizeof(TNotifyiconData);
wnd:=Handle;
uID:=1;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallBackMessage:=WM_NID;
uIcon:=Application.Icon.Handle;//icon設置,此處設為了應用程序圖標的圖標
szTip:=InSun安全衛士; //tip
end;
Shell_NotiFYIcon(NIM_ADD,@NotifyIcon);
end;
procedure TFIcon1.FormDestroy(Sender: TObject);
begin
Shell_NotiFYIcon(NIM_DELETE,@NotifyIcon);
end;
procedure TFIcon1.WMNID(var msg: TMessage);
begin
case msg.LParam of
WM_LBUTTONUP:
FIcon1.visible:=not FIcon1.visible ;
WM_RBUTTONUP:
//ShowMessage(ok) ;
popupMenu1.Popup(1200,1000); //add by Insun
end; end;
procedure TFIcon1.N2Click(Sender: TObject);
begin
close ;
end;
procedure TFIcon1.N1Click(Sender: TObject);
begin
Shellexecute(handle, nil, pchar(http://yxmhero1989.blog.163.com/), nil, nil, sw_shownormal);
end;
end.
這個例子裡我添加了imagelist方便圖標,popupmenu做彈窗,skindata做皮膚美化。
安裝第三方控件BusinessSkinForm VCL
{.dpk install
添加VCL頁面SkinData組件,active屬性改為true
tools--environment options-Library--Library Path找到dpk所在目錄--OK
其他組件安裝也是類似,出現下面錯誤基本技術沒Library Path
[Fatal Error] Unit1.pas(7): File not found: UWSStartItemPanel.dcu
}
使用 BusinessSkinForm VCL組中的bsTrayIcon控件即可,裡面屬性自己設置。
Rize的控件也一樣,用RzTrayIcon。
介紹CoolTrayIcon這個控件,這個控件有點好。安裝CoolTrayIcon_D6plus.dpk ,方法同上。
裡面有兩個十分必要且有趣的Demo。
CoolTrayTest:
彈出氣球提示文字
procedure TMainForm.Button4Click(Sender: TObject);
begin
TrayIcon1.ShowBalloonHint(Balloon hint,
Use the balloon hint to display important information. + #13 +
The text can be max. 255 chars. and the title max. 64 chars.,
bitInfo, 10);//內容隨便改啦
end;
有左鍵彈窗,右鍵彈出,最小化到托盤,關閉到托盤等多選框。
有各種tray icons,各種動態圖標。
動態圖標是利用imagelist調用多個圖標,然後cycle。
TrayIcon1.IconList := ImageList3;
TrayIcon1.CycleInterval := 300;
TrayIcon1.CycleIcons := True;
再看看
BigHint:
話說這個hint挺撒的,還有一種風格。
具體代碼就不貼了,大家自己去看Demo