目录
windows下当程序关闭,有时系统右下角的系统托盘图标还会存在。如不停的关闭和打开程序,任务栏右下角通知区域,同一个图标重复很多。Qt使用QSystemTrayIcon类开发时,也会存在这个问题。如何解决?
方法是调用windows API。
对于Qt GUI程序,如果想要实现当最小化时,程序从任务栏消失,在系统托盘显示一个图标,表示此程序,并能在托盘内通过双击或者菜单使程序界面恢复。
主要使用的此类:QSystemTrayIcon。其中QSystemTrayIcon是主要操作系统托盘的操作类,通过此类,可以在托盘显示指定程序的图标,显示指定消息,显示菜单等。
- #ifndef TESTTRAYICON_H
- #define TESTTRAYICON_H
-
- #include
- #include "ui_testtrayicon.h"
-
- class testTrayIcon : public QMainWindow
- {
- Q_OBJECT
-
- public:
- testTrayIcon(QWidget *parent = 0, Qt::WFlags flags = 0);
- ~testTrayIcon();
-
- private:
- Ui::testTrayIconClass ui;
- };
-
- #endif // TESTTRAYICON_H
- #include "testtrayicon.h"
- #include
- testTrayIcon::testTrayIcon(QWidget *parent, Qt::WFlags flags)
- : QMainWindow(parent, flags)
- {
- ui.setupUi(this);
-
- QIcon icon = QIcon( ":/logo.png" );
- QSystemTrayIcon *trayIcon = new QSystemTrayIcon( this );
- trayIcon->setIcon(icon);
- trayIcon->setToolTip(tr( "hello" ));
- QString titlec=tr( "hello" );
- QString textc=tr( "hello" );
- trayIcon->show();
- trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
- QMenu*pMenu=new QMenu(this);
- pMenu->addAction("test1");
- pMenu->addAction("test2");
- trayIcon->setContextMenu(pMenu);
- }
-
- testTrayIcon::~testTrayIcon()
- {
-
- }
-
- #include "testtrayicon.h"
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- testTrayIcon w;
- //w.show();
- w.hide();
- return a.exec();
- }
- #ifdef WIN32
- #include
- void RefreshIcon()//任务栏图标刷新一遍
- {
- //任务栏窗口
- HWND hShellTrayWnd = FindWindow(L"Shell_TrayWnd",NULL);
- //任务栏右边托盘图标+时间区
- HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd,0,L"TrayNotifyWnd",NULL);
- //不同系统可能有可能没有这层
- HWND hSysPager = FindWindowEx(hTrayNotifyWnd,0,L"SysPager",NULL);
- //托盘图标窗口
- HWND hToolbarWindow32;
- if (hSysPager)
- {
- hToolbarWindow32 = FindWindowEx(hSysPager,0,L"ToolbarWindow32",NULL);
- }
- else
- {
- hToolbarWindow32 = FindWindowEx(hTrayNotifyWnd,0,L"ToolbarWindow32",NULL);
- }
- if (hToolbarWindow32)
- {
- RECT r;
- GetWindowRect(hToolbarWindow32,&r);
- int width = r.right - r.left;
- int height = r.bottom - r.top;
- //从任务栏中间从左到右 MOUSEMOVE一遍,所有图标状态会被更新
- for (int x = 1; x
- {
- SendMessage(hToolbarWindow32,WM_MOUSEMOVE,0,MAKELPARAM(x,height/2));
- }
- }
-
- }
- #endif
2、托盘刷新代码的调用
- #include "testtrayicon.h"
- #include
- #ifdef WIN32
- #include
- void RefreshIcon()//任务栏图标刷新一遍
- {
- //任务栏窗口
- HWND hShellTrayWnd = FindWindow(L"Shell_TrayWnd",NULL);
- //任务栏右边托盘图标+时间区
- HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd,0,L"TrayNotifyWnd",NULL);
- //不同系统可能有可能没有这层
- HWND hSysPager = FindWindowEx(hTrayNotifyWnd,0,L"SysPager",NULL);
- //托盘图标窗口
- HWND hToolbarWindow32;
- if (hSysPager)
- {
- hToolbarWindow32 = FindWindowEx(hSysPager,0,L"ToolbarWindow32",NULL);
- }
- else
- {
- hToolbarWindow32 = FindWindowEx(hTrayNotifyWnd,0,L"ToolbarWindow32",NULL);
- }
- if (hToolbarWindow32)
- {
- RECT r;
- GetWindowRect(hToolbarWindow32,&r);
- int width = r.right - r.left;
- int height = r.bottom - r.top;
- //从任务栏中间从左到右 MOUSEMOVE一遍,所有图标状态会被更新
- for (int x = 1; x
- {
- SendMessage(hToolbarWindow32,WM_MOUSEMOVE,0,MAKELPARAM(x,height/2));
- }
- }
-
- }
- #endif
-
- testTrayIcon::testTrayIcon(QWidget *parent, Qt::WFlags flags)
- : QMainWindow(parent, flags)
- {
- ui.setupUi(this);
-
- QIcon icon = QIcon( ":/logo.png" );
- QSystemTrayIcon *trayIcon = new QSystemTrayIcon( this );
- trayIcon->setIcon(icon);
- trayIcon->setToolTip(tr( "hello" ));
- QString titlec=tr( "hello" );
- QString textc=tr( "hello" );
- trayIcon->show();
- trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
- QMenu*pMenu=new QMenu(this);
- pMenu->addAction("test1");
- pMenu->addAction("test2");
- trayIcon->setContextMenu(pMenu);
-
- RefreshIcon();
- }
-
- testTrayIcon::~testTrayIcon()
- {
-
- }
-
相关阅读:
一文了解Nginx及其基本配置
nodejs+vue+ElementUi废品废弃资源回收系统
python+selenium进行cnblog的自动化登录测试
DRAC2022比赛总结+前五解决方案
Java with RocketMQ
lime-util 前端模块化 JavaScript 工具库
用SuperMap版leaflet框架对SuperMap iServer发布的3个数据集进行叠加分析
Java项目使用intellij-IDEA查看依赖包版本是否有冲突(方法及工具)
centos 利用selenium抓包
【C++】类和对象——拷贝构造函数
-
原文地址:https://blog.csdn.net/kupe87826/article/details/126166173