目录
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()
- {
-
- }
-
相关阅读:
累加出整个范围所有的数最少还需要几个数
pandas(进阶操作)-- 处理非数值型数据 -- 数据分析三剑客(核心)
java.lang.ClassNotFoundException:如何解决
【C++智能指针】智能指针的发展和循环引用的原理和解决
Mysql中DQL(查询类)语句的执行顺序
点胶缺陷视觉检测都是怎么检测的?
解决端口占用
iOS端如何实现带UI截屏分享
TikTok与老年用户:社交媒体的跨代交流
闭包学习记录-iOS开发
-
原文地址:https://blog.csdn.net/kupe87826/article/details/126166173