在/覆盖第3方窗口上显示QT小部件(在Windows中)

ora*_*001 2 c++ hook winapi qt overlay

这不是我以前尝试过的东西,并且对HWND,Hooking等都是完全新手。

基本上,我想在第三方应用程序的窗口顶部显示/叠加一个QT窗口小部件(我无法控制,我只知道非常基本的信息,例如窗口标题/标题及其类名),并且我绝对不知道该怎么办。我还希望QT小部件相对于第三方应用程序的窗口保持在相对位置,即使该窗口在屏幕上移动也是如此。

Pav*_*hov 5

WinAPI部分

  1. 使用FindWindow函数获取目标窗口的HWND。
  2. 使用GetWindowRect获取窗口的当前位置。

Qt部分

  1. 使用窗口标志和来使您的顶层QWidgetQMainWindow无框框架保持在顶层。 Qt::FramelessWindowHintQt::WindowStaysOnTopHint
  2. 使用attribute 使它透明Qt::WA_TranslucentBackground
  3. 设置一个QTimer以定期请求窗口rect并调整小部件位置。

示例代码(已测试)

添加标题:

private:
  HWND target_window;

private slots:
  void update_pos();
Run Code Online (Sandbox Code Playgroud)

资源:

#include "Windows.h"
#include <QDebug>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  setAttribute(Qt::WA_TranslucentBackground);
  // example of target window class: "Notepad++"
  target_window = FindWindowA("Notepad++", 0); 
  if (!target_window) {
    qDebug() << "window not found";
    return;
  }
  QTimer* timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(update_pos()));
  timer->start(50); // update interval in milliseconds 
}

MainWindow::~MainWindow() {
  delete ui;
}

void MainWindow::update_pos() {
  RECT rect;
  if (GetWindowRect(target_window, &rect)) {
    setGeometry(rect.left, rect.top, rect.right - rect.left, 
                rect.bottom - rect.top);
  } else {
    //maybe window was closed
    qDebug() << "GetWindowRect failed";
    QApplication::quit();
  }
}
Run Code Online (Sandbox Code Playgroud)