zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Qt-判断激活窗口(当前窗口)是不是自己

Qt 判断 自己 当前 窗口 激活 是不是
2023-09-14 08:57:11 时间
相关资料:
https://download.csdn.net/download/zhujianqiangqq/85630059       CSDN代码包下载
 
 
.pro
 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
View Code

main.cpp

 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 }
View Code

mainwindow.h

 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
View Code

mainwindow.cpp

 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 }
View Code

mainwindow.ui

 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>
View Code