Delphi实现系统托盘管理

Delphi
placeholder image
admin 发布于:2017-11-17 17:21:46
阅读:loading

基本介绍

几乎所有的CS端程序都有在系统托盘显示图标的功能,如:最小化显示托盘,右键进行对软件的其他*作等,我们使用Delphi写的一些“渣渣”程序经常使用且能最小化至系统托盘当然是最好的了,在综合了好多资料后,实现了本例,才有了本文。本例实现的功能点主要有:

(1)打开窗口软件时使其在系统托盘显示图标;

(2)点击软件最小化按钮,将其隐藏;

(3)右键系统托盘软件可显示相关菜单按钮,点击不同菜单的功能实现;

参考代码

image.png

unit NotifyTray;


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls , TLHelp32, Menus , ShellApi , StrUtils;


type

  TForm1 = class(TForm)

    Label1: TLabel;

    PopupMenu1: TPopupMenu;

    N1: TMenuItem;

    N2: TMenuItem;

    N3: TMenuItem;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject); 

    procedure N1Click(Sender: TObject);

    procedure N2Click(Sender: TObject);

    procedure HideForm(Sender: TObject);

  private

    { Private declarations }

    procedure WMSysCommand(var message: TMessage); message WM_SYSCOMMAND;

  public

    { Public declarations }

    procedure WndProc(var msg:TMessage);override;

  end;


var

  Form1: TForm1;

  nd0,nd1 : NotifyIconData;

  WM_TRAYNOTIFY: Cardinal;

  hs: array[0..1] of LongWord;

  IconFlag : Integer;

  dt:String;

  const

      TrayMsgStr = 'TrayNotifyIconMsg';


implementation


{$R *.dfm}


procedure TForm1.WMSysCommand(var message: TMessage);

begin

    if Message.WParam = SC_CLOSE then

    begin

        Application.Terminate;

    end

    else if  Message.WParam = SC_MINIMIZE then

    begin

        Form1.Visible := false;

    end

    else

    begin

        //如果是其它行为则调用系统自带的实现

        DefWindowProc(Form1.Handle , Message.msg , Message.WParam , Message.LParam);

    end;

end;


procedure TForm1.WndProc(var msg:Tmessage);

var

   pt:TPoint;

begin

    with msg do

    begin

        if msg = WM_TRAYNOTIFY then

        begin

            case lParam of

                WM_LBUTTONDOWN:

                begin

                     hs[1] := LoadIcon(hInstance , 'Icon1');

                     nd1.cbSize := 88;

                     nd1.Wnd := Handle;

                     nd1.uId := 1;

                     nd1.szTip := '提示信息';

                     nd1.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;

                     nd1.uCallbackMessage := WM_TRAYNOTIFY;

                     nd1.hIcon := hs[1];

                     Shell_notifyIcon(NIM_MODIFY , @nd1);

                     IconFlag := 1;

                     Form1.Show;

                end;

                WM_LBUTTONDBLCLK:

                begin

                    Self.Visible := true;

                end;

                WM_RBUTTONDOWN:

                begin

                    SetForegroundWindow(Handle);

                    GetCursorPos(pt);

                    PopupMenu1.popup(pt.x , pt.y);

                end;

             end;

        end

    end;

    inherited;

end;


procedure TForm1.N1Click(Sender: TObject);

begin

    Form1.Visible := true;

end;


procedure TForm1.N2Click(Sender: TObject);

begin

    Application.Terminate;

end;


procedure TForm1.HideForm(Sender: TObject);

begin

     showmessage('hideform');

     Form1.Visible := false;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

    WM_TRAYNOTIFY := RegisterWindowMessage(TrayMsgStr);

    hs[0] := LoadIcon(hInstance , 'MainIcon');

    nd0.cbSize := 88;

    nd0.Wnd := Handle;

    nd0.szTip := '提示信息';

    nd0.uID := 0;

    nd0.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;

    nd0.uCallbackMessage := WM_TRAYNOTIFY;

    nd0.hIcon := hs[0];

    Shell_notifyicon(NIM_ADD , @nd0);

    IconFlag := 0;

end;


procedure TForm1.FormDestroy(Sender: TObject);

begin

     if (IconFlag = 0) then

         Shell_NotifyIcon(NIM_DELETE , @nd0)

     else

         Shell_NotifyIcon(NIM_DELETE , @nd1);

end;


end.

运行效果

image.png

image.png

image.png

image.png

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:https://www.chendd.cn/information/viewInformation/other/211.a

最后更新:2017-11-17 17:21:46 

访问次数:296

评论次数:0

点赞个数:0

 点赞


 发表评论

当前回复:作者

 评论列表


留言区