Qt-判断激活窗口(当前窗口)是不是自己
Qt 判断 自己 当前 窗口 激活 是不是
2023-09-14 08:57:11 时间
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1786386/o_220613091753_Qt-%E5%88%A4%E6%96%AD%E6%BF%80%E6%B4%BB%E7%AA%97%E5%8F%A3(%E5%BD%93%E5%89%8D%E7%AA%97%E5%8F%A3)%E6%98%AF%E4%B8%8D%E6%98%AF%E8%87%AA%E5%B7%B1.png)
相关资料:
https://download.csdn.net/download/zhujianqiangqq/85630059 CSDN代码包下载
.pro
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 QT += core gui 2 3 #TEMPLATE = lib 4 #CONFIG+=staticlib 5 6 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 7 8 CONFIG += c++11 9 10 # The following define makes your compiler emit warnings if you use 11 # any Qt feature that has been marked deprecated (the exact warnings 12 # depend on your compiler). Please consult the documentation of the 13 # deprecated API in order to know how to port your code away from it. 14 DEFINES += QT_DEPRECATED_WARNINGS 15 16 # You can also make your code fail to compile if it uses deprecated APIs. 17 # In order to do so, uncomment the following line. 18 # You can also select to disable deprecated APIs only up to a certain version of Qt. 19 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 20 21 SOURCES += \ 22 main.cpp \ 23 mainwindow.cpp 24 25 HEADERS += \ 26 mainwindow.h 27 28 FORMS += \ 29 mainwindow.ui 30 31 LIBS += -lUser32 32 33 34 # Default rules for deployment. 35 qnx: target.path = /tmp/$${TARGET}/bin 36 else: unix:!android: target.path = /opt/$${TARGET}/bin 37 !isEmpty(target.path): INSTALLS += target
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <windows.h> 5 #include <QMainWindow> 6 #include <QDebug> 7 #include <QTimer> 8 9 QT_BEGIN_NAMESPACE 10 namespace Ui { class MainWindow; } 11 QT_END_NAMESPACE 12 13 class MainWindow : public QMainWindow 14 { 15 Q_OBJECT 16 17 public: 18 MainWindow(QWidget *parent = nullptr); 19 ~MainWindow(); 20 21 private slots: 22 void on_ShowForegroundWindow(); 23 24 private: 25 Ui::MainWindow *ui; 26 QTimer * m_pTimer = NULL; 27 }; 28 #endif // MAINWINDOW_H
mainwindow.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 setWindowTitle(QStringLiteral("Qt-判断激活窗口(当前窗口)是不是自己")); 11 12 m_pTimer = new QTimer(this); 13 m_pTimer->setSingleShot(false); 14 connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_ShowForegroundWindow); 15 m_pTimer->start(1000); 16 } 17 18 MainWindow::~MainWindow() 19 { 20 delete ui; 21 } 22 23 24 void MainWindow::on_ShowForegroundWindow() 25 { 26 HWND hwnd= GetForegroundWindow(); 27 DWORD Fid = GetWindowThreadProcessId(hwnd, NULL); 28 DWORD Cid = GetCurrentThreadId(); 29 30 // qDebug() << "-----------------"; 31 // qDebug() << "Fid" << Fid; 32 // qDebug() << "Cid" << Cid; 33 // if (Fid == Cid) 34 // { 35 // qDebug() << "isOK"; 36 // } 37 if (Fid != Cid) 38 { 39 ui->textEdit->append("false"); 40 } 41 else 42 { 43 ui->textEdit->append("true"); 44 } 45 }
mainwindow.ui
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>555</width> 10 <height>286</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"> 17 <layout class="QHBoxLayout" name="horizontalLayout"> 18 <item> 19 <widget class="QTextEdit" name="textEdit"/> 20 </item> 21 </layout> 22 </widget> 23 </widget> 24 <resources/> 25 <connections/> 26 </ui>
相关文章
- VS中新建Qt项目工程后显示无法打开源文件“QtWidgets/QApplication”的解决方案「建议收藏」
- qt 当前窗口句柄_QT获取窗口句柄
- C/C++ Qt 数据库与ComBox多级联动
- Qt - 聊天室发送图片/文件
- Qt自绘系列-频谱图
- 建立Linux下QT动态库建立快速指南(linux下qt动态库)
- 如何在Linux系统中安装Qt开发环境(linux下qt安装教程)
- 使用Qt快速设置Linux时间(qt设置linux时间)
- Qt元对象和属性系统详解
- 实现64位QT环境下的MySQL使用(qt64位mysql)
- Linux虚拟机中安装Qt的指南(linux虚拟机安装qt)
- Qt 静态编译在 Linux 的实践(qt静态编译linux)
- Linux下Qt实现串口通信(linux下qt串口)
- 使用 Qt 连接 MySql (qt使用mysql)
- Qt与Oracle数据库的高效结合(qt数据库oracle)
- 简明易懂的Linux下QT安装指南(linux下安装qt教程)
- 程序开启Linux之旅:使用Qt搭建聊天程序(linuxqt聊天)
- 在Linux上开发应用程序:Qt系列版本深入解析(linux版本qt)
- Linux平台上Qt程序开发发布实践(linuxqt程序发布)
- 文件在Linux上使用Qt开发可执行文件(qtlinux可执行)
- QT and MySQL: A Comprehensive Tutorial for Building Database Connections(qt连接mysql教程)
- 如何在QT应用程序中使用MYSQL进行编程?(qt的mysql编程)
- Qt实现MySQL数据库操作详解(qt操作mysql数据库)
- MySQL驱动Qt开发:新开发者的进阶指南(mysql驱动 qt)
- 界面让Linux拥有Qt完美图形界面的魅力(linux qt 图形)