• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi托盘程序转

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

Delphi的托盘编程   。现在很多程序都用这个,比如傲游,迅雷等,主要代码如下:

uses

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

Dialogs, ShellAPI, AppEvnts, StdCtrls, Menus;

const WM_NID = WM_User + 1000; //声明一个常量

private

    { Private declarations } // 定义两个函数

    procedure SysCommand(var SysMsg: TMessage); message WM_SYSCOMMAND;

    procedure WMNID(var msg:TMessage); message WM_NID;

public

end;

var

Form1: TForm1;

NotifyIcon: TNotifyIconData; // 全局变量

implementation

{$R *.dfm}

procedure TForm1.WMNID(var msg:TMessage);

var

mousepos: TPoint;

begin

GetCursorPos(mousepos); //获取鼠标位置

case msg.LParam of

    WM_LBUTTONUP: // 在托盘区点击左键后

    begin

      Form1.Visible := not Form1.Visible; // 显示主窗体与否

      Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 显示主窗体后删除托盘区的图标

      SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW); // 在任务栏显示程序

    end;

  

    WM_RBUTTONUP: PopupMenu1.Popup(mousepos.X, mousepos.Y); // 弹出菜单

end;

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标

end;

procedure TForm1.SysCommand(var SysMsg: TMessage);

begin

case SysMsg.WParam of

    SC_MINIMIZE: // 当最小化时

    begin

      SetWindowPos(Application.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW);

      Hide; // 在任务栏隐藏程序

      // 在托盘区显示图标

      with NotifyIcon do

      begin

        cbSize := SizeOf(TNotifyIconData);

        Wnd := Handle;

        uID := 1;

        uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;

        uCallBackMessage := WM_NID;

        hIcon := Application.Icon.Handle;

        szTip := '托盘程序';

      end;

      Shell_NotifyIcon(NIM_ADD, @NotifyIcon); // 在托盘区显示图标

    end;

else

    inherited;

end;

end;

{以下三个函数为托盘右键菜单,可自行添加功能}

procedure TForm1.N1Click(Sender: TObject);

begin

ShowMessage('Delphi托盘小程序!');

end;

procedure TForm1.N2Click(Sender: TObject);

begin

Form1.Visible := true; // 显示窗体

SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW);

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标

end;

procedure TForm1.N3Click(Sender: TObject);

begin

Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);

Application.Terminate;

end;

end.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB之心形图绘制发布时间:2022-07-18
下一篇:
matlab画图中线型及颜色设置发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap