找回密码
 立即注册
查看: 549|回复: 0

DELPHI 应用程序图标托盘至右下角

[复制链接]

194

主题

0

回帖

986

积分

管理员

积分
986
发表于 2024-6-15 17:25:00 | 显示全部楼层 |阅读模式


  1. unit MSTSCUnit1;

  2. interface

  3. uses
  4.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  5.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.IniFiles,Winapi.ShellAPI,Vcl.AppEvnts;

  6. const
  7. MouseMsg =WM_USER + 1;

  8. type
  9.   TForm1 = class(TForm)
  10.     procedure FormShow(Sender: TObject);
  11.     procedure FormCreate(Sender: TObject);
  12.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  13.   private
  14.     { Private declarations }
  15.     procedure MouseMessage(var message: tmessage); message mousemsg;    //这里要复制,系统创建少最后一段内容。
  16.   public
  17.     { Public declarations }
  18.   end;

  19. var
  20.   Form1: TForm1;
  21.   ntida:TNotifyIcondataA;

  22. implementation

  23. {$R *.dfm}


  24. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  25. begin
  26.   Action := caNone; //不对窗体进行任何操作
  27.   ShowWindow(Handle, SW_HIDE); //隐藏主窗体
  28.   //隐藏应用程序窗口在任务栏上的显示
  29.   ShowWindow(Application.Handle, SW_HIDE);
  30.   SetWindowLong(Application.Handle, GWL_EXSTYLE,
  31.   not (GetWindowLong(Application.handle, GWL_EXSTYLE)
  32.   or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW));
  33. end;

  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   ntida.cbSize := sizeof(tnotifyicondataa); //指定ntida的长度
  37.   ntida.Wnd := handle; //取应用程序主窗体的句柄
  38.   ntida.uID := 100; //用户自定义的一个数值,在uCallbackMessage参数指定的消息中使
  39.   ntida.uFlags := nif_icon + nif_tip +
  40.   nif_message; //指定在该结构中uCallbackMessage、hIcon和szTip参数都有效
  41.   ntida.uCallbackMessage := mousemsg;
  42.   //指定的窗口消息
  43.   ntida.hIcon := Application.Icon.handle;
  44.   //指定系统状态栏显示应用程序的图标句柄
  45.   ntida.szTip := 'MSTSC';
  46.   //当鼠标停留在系统状态栏该图标上时,出现该提示信息
  47.   shell_notifyicona(NIM_ADD, @ntida);
  48.   //在系统状态栏增加一个新图标
  49. end;

  50. procedure TForm1.MouseMessage(var message: tmessage);
  51. var
  52. mousept: TPoint; //鼠标点击位置
  53. begin
  54. inherited;
  55. if message.LParam = wm_rbuttonup then begin //用鼠标右键点击图标
  56. if MessageBox(Handle, '你确定要退出吗?', '信息提示', MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
  57.   begin
  58.     ntida.cbSize := sizeof(tnotifyicondataa);
  59.     ntida.wnd := handle;
  60.     ntida.uID := 100;
  61.     ntida.uFlags := nif_icon + nif_tip + nif_message;
  62.     ntida.uCallbackMessage := mousemsg;
  63.     ntida.hIcon := Application.Icon.handle;
  64.     ntida.szTip := 'MSTSC';
  65.     shell_notifyicona(NIM_DELETE, @ntida);
  66.     //删除已有的应用程序图标
  67.     Application.Terminate;
  68.     //中断应用程序运行,退出应用程序
  69.   end;
  70. end;
  71. if message.LParam = wm_lbuttonup then begin //用鼠标左键点击图标
  72.     //显示应用程序窗口
  73.     ShowWindow(Handle, SW_SHOW);
  74.    //在任务栏上显示应用程序窗口
  75.    // ShowWindow(Application.handle, SW_SHOW);
  76.     SetWindowLong(Application.Handle, GWL_EXSTYLE,
  77.     not (GetWindowLong(Application.handle, GWL_EXSTYLE)
  78.     or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW));
  79. end;
  80.     message.Result := 0;
  81. end;

  82. procedure TForm1.FormShow(Sender: TObject);
  83. begin
  84.   //显示位置
  85.   Form1.top:=(screen.height-form1.height) div 5;
  86.   Form1.left:=(screen.width-form1.width) div 1;
  87. end;

  88. end.

复制代码

商务合作
业务洽谈
Wechat:S3-SH-CN  
QQ群:128669090
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|S3软件

GMT+8, 2026-6-6 07:47 , Processed in 0.038027 second(s), 19 queries .

Powered by S3

©2001-2025 S3 Team.

快速回复 返回顶部 返回列表