• Windows下QT实现托盘程序及系统托盘刷新


    目录

    一、现象

    二、托盘程序的QT实现

    1、功能说明

    2、使用的类

    3、代码实现

    三、windows系统托盘刷新

    1、托盘刷新代码

    2、托盘刷新代码的调用


    一、现象

          windows下当程序关闭,有时系统右下角的系统托盘图标还会存在。如不停的关闭和打开程序,任务栏右下角通知区域,同一个图标重复很多。Qt使用QSystemTrayIcon类开发时,也会存在这个问题。如何解决?
    方法是调用windows API

    二、托盘程序的QT实现


    1、功能说明


            对于Qt GUI程序,如果想要实现当最小化时,程序从任务栏消失,在系统托盘显示一个图标,表示此程序,并能在托盘内通过双击或者菜单使程序界面恢复。


    2、使用的类


            主要使用的此类:QSystemTrayIcon。其中QSystemTrayIcon是主要操作系统托盘的操作类,通过此类,可以在托盘显示指定程序的图标,显示指定消息,显示菜单等。

    3、代码实现

    1. #ifndef TESTTRAYICON_H
    2. #define TESTTRAYICON_H
    3. #include
    4. #include "ui_testtrayicon.h"
    5. class testTrayIcon : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. testTrayIcon(QWidget *parent = 0, Qt::WFlags flags = 0);
    10. ~testTrayIcon();
    11. private:
    12. Ui::testTrayIconClass ui;
    13. };
    14. #endif // TESTTRAYICON_H
    1. #include "testtrayicon.h"
    2. #include
    3. testTrayIcon::testTrayIcon(QWidget *parent, Qt::WFlags flags)
    4. : QMainWindow(parent, flags)
    5. {
    6. ui.setupUi(this);
    7. QIcon icon = QIcon( ":/logo.png" );
    8. QSystemTrayIcon *trayIcon = new QSystemTrayIcon( this );
    9. trayIcon->setIcon(icon);
    10. trayIcon->setToolTip(tr( "hello" ));
    11. QString titlec=tr( "hello" );
    12. QString textc=tr( "hello" );
    13. trayIcon->show();
    14. trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
    15. QMenu*pMenu=new QMenu(this);
    16. pMenu->addAction("test1");
    17. pMenu->addAction("test2");
    18. trayIcon->setContextMenu(pMenu);
    19. }
    20. testTrayIcon::~testTrayIcon()
    21. {
    22. }
    1. #include "testtrayicon.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. testTrayIcon w;
    7. //w.show();
    8. w.hide();
    9. return a.exec();
    10. }

    三、windows系统托盘刷新

    1、托盘刷新代码

    1. #ifdef WIN32
    2. #include
    3. void RefreshIcon()//任务栏图标刷新一遍
    4. {
    5. //任务栏窗口
    6. HWND hShellTrayWnd = FindWindow(L"Shell_TrayWnd",NULL);
    7. //任务栏右边托盘图标+时间区
    8. HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd,0,L"TrayNotifyWnd",NULL);
    9. //不同系统可能有可能没有这层
    10. HWND hSysPager = FindWindowEx(hTrayNotifyWnd,0,L"SysPager",NULL);
    11. //托盘图标窗口
    12. HWND hToolbarWindow32;
    13. if (hSysPager)
    14. {
    15. hToolbarWindow32 = FindWindowEx(hSysPager,0,L"ToolbarWindow32",NULL);
    16. }
    17. else
    18. {
    19. hToolbarWindow32 = FindWindowEx(hTrayNotifyWnd,0,L"ToolbarWindow32",NULL);
    20. }
    21. if (hToolbarWindow32)
    22. {
    23. RECT r;
    24. GetWindowRect(hToolbarWindow32,&r);
    25. int width = r.right - r.left;
    26. int height = r.bottom - r.top;
    27. //从任务栏中间从左到右 MOUSEMOVE一遍,所有图标状态会被更新
    28. for (int x = 1; x
    29. {
    30. SendMessage(hToolbarWindow32,WM_MOUSEMOVE,0,MAKELPARAM(x,height/2));
    31. }
    32. }
    33. }
    34. #endif

    2、托盘刷新代码的调用

    1. #include "testtrayicon.h"
    2. #include
    3. #ifdef WIN32
    4. #include
    5. void RefreshIcon()//任务栏图标刷新一遍
    6. {
    7. //任务栏窗口
    8. HWND hShellTrayWnd = FindWindow(L"Shell_TrayWnd",NULL);
    9. //任务栏右边托盘图标+时间区
    10. HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd,0,L"TrayNotifyWnd",NULL);
    11. //不同系统可能有可能没有这层
    12. HWND hSysPager = FindWindowEx(hTrayNotifyWnd,0,L"SysPager",NULL);
    13. //托盘图标窗口
    14. HWND hToolbarWindow32;
    15. if (hSysPager)
    16. {
    17. hToolbarWindow32 = FindWindowEx(hSysPager,0,L"ToolbarWindow32",NULL);
    18. }
    19. else
    20. {
    21. hToolbarWindow32 = FindWindowEx(hTrayNotifyWnd,0,L"ToolbarWindow32",NULL);
    22. }
    23. if (hToolbarWindow32)
    24. {
    25. RECT r;
    26. GetWindowRect(hToolbarWindow32,&r);
    27. int width = r.right - r.left;
    28. int height = r.bottom - r.top;
    29. //从任务栏中间从左到右 MOUSEMOVE一遍,所有图标状态会被更新
    30. for (int x = 1; x
    31. {
    32. SendMessage(hToolbarWindow32,WM_MOUSEMOVE,0,MAKELPARAM(x,height/2));
    33. }
    34. }
    35. }
    36. #endif
    37. testTrayIcon::testTrayIcon(QWidget *parent, Qt::WFlags flags)
    38. : QMainWindow(parent, flags)
    39. {
    40. ui.setupUi(this);
    41. QIcon icon = QIcon( ":/logo.png" );
    42. QSystemTrayIcon *trayIcon = new QSystemTrayIcon( this );
    43. trayIcon->setIcon(icon);
    44. trayIcon->setToolTip(tr( "hello" ));
    45. QString titlec=tr( "hello" );
    46. QString textc=tr( "hello" );
    47. trayIcon->show();
    48. trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
    49. QMenu*pMenu=new QMenu(this);
    50. pMenu->addAction("test1");
    51. pMenu->addAction("test2");
    52. trayIcon->setContextMenu(pMenu);
    53. RefreshIcon();
    54. }
    55. testTrayIcon::~testTrayIcon()
    56. {
    57. }
  • 相关阅读:
    累加出整个范围所有的数最少还需要几个数
    pandas(进阶操作)-- 处理非数值型数据 -- 数据分析三剑客(核心)
    java.lang.ClassNotFoundException:如何解决
    【C++智能指针】智能指针的发展和循环引用的原理和解决
    Mysql中DQL(查询类)语句的执行顺序
    点胶缺陷视觉检测都是怎么检测的?
    解决端口占用
    iOS端如何实现带UI截屏分享
    TikTok与老年用户:社交媒体的跨代交流
    闭包学习记录-iOS开发
  • 原文地址:https://blog.csdn.net/kupe87826/article/details/126166173