当前位置:编程学习 > Delphi >>

纵谈Delphi的系统托盘实现

Author:Insun

Blog:http://yxmhero1989.blog.163.com

From:www.4safer.com

 

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun
 
 

 

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做皮肤美化。

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

 

 

安装第三方控件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:

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

弹出气球提示文字

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;

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

 有左键弹窗,右键弹出,最小化到托盘,关闭到托盘等多选框。

有各种tray icons,各种动态图标。

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

 

 动态图标是利用imagelist调用多个图标,然后cycle。

 TrayIcon1.IconList := ImageList3;
 TrayIcon1.CycleInterval := 300;
 TrayIcon1.CycleIcons := True;

 

再看看

BigHint:

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

  话说这个hint挺撒的,还有一种风格。

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

 

纵谈Delphi的系统托盘实现 - InSun - Minghacker is Insun

 

具体代码就不贴了,大家自己去看Demo

补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,