您现在的位置是:首页 >
企业资讯
微博秒刷点赞 - 微博刷点赞违法吗,微博热门刷赞是啥意思
时间:2024-05-28 05:29
评论
windows下当程序关闭,有时系统右下角的系统托盘图标还会存在。二、托盘程序的QT实现GUI程序,如果想要实现当最小化时,程序从任务栏消失,在系统托盘显示一个图标,表示此程序,并能在托盘内通过双击或者菜单使程序界面恢复。其中QSystemTrayIcon是主要操作系统托盘的操作类,通过此类,可以在托盘显示指定程序的图标,显示指定消息,显示菜单等。1、托盘刷新代码2、托盘刷新代码的调用...
自助下单地址(拼多多砍价,ks/qq/dy赞等业务):点我进入
目录
一、现象
Windows下程序关闭时,有时系统右下角的系统托盘图标还会存在。 例如,程序不断关闭和打开,在任务栏右下角的通知区域重复出现多次相同的图标。 使用QSystemTrayIcon类开发Qt时也存在这个问题。 怎么解决?
方法是调用windows API。
二、托盘程序的QT实现
一、功能说明
对于Qt GUI程序qt 托盘程序,如果要实现程序最小化时从任务栏消失,系统托盘会显示一个图标代表该程序,双击或菜单可以恢复程序界面在托盘中。
2.使用的类
这个类主要用到:QSystemTrayIcon。 其中,QSystemTrayIcon是主操作系统托盘的操作类。 通过这个类qt 托盘程序,可以在托盘上显示指定程序的图标、指定的消息、菜单。
3.代码实现
#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();
}
三、Windows系统托盘刷新 1.托盘刷新代码
#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
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; xsetIcon(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()
{
}