obsidian/笔记文件/2.笔记/qt获取HWND的分辨率大小和字体大小替换.md
2025-03-26 00:02:56 +08:00

118 lines
4.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#qt
在Qt中如果你已经有一个窗口句柄HWND但你想要获取该窗口的分辨率通常指的是窗口的客户区大小而不是整个窗口包括边框和标题栏的大小你不能直接从HWND获取所谓的“分辨率”。但是你可以通过Windows API函数GetClientRect来获取窗口客户区的大小这可以视为窗口的“分辨率”或内部尺寸。
以下是如何使用Qt和Windows API来实现这一点的步骤
包含必要的头文件:
你需要包含windows.h头文件来使用Windows API函数。
使用Windows API获取窗口客户区大小
使用GetClientRect函数来获取HWND对应的窗口客户区大小。
在Qt中使用这些信息
将获取到的大小转换为Qt可以使用的尺寸QSize
下面是一个简单的示例代码:
``` cpp
#include <QWidget>
#include <QDebug>
#include <windows.h>
// 假设你有一个HWND句柄这里用hwnd表示
void getWindowSizeFromHWND(HWND hwnd) {
RECT rect;
if (GetClientRect(hwnd, &rect)) {
// rect现在包含了窗口客户区的左上角和右下角的坐标
// 转换为Qt的QSize
QSize size(rect.right - rect.left, rect.bottom - rect.top);
qDebug() << "Window size:" << size;
} else {
// 处理错误
qDebug() << "Failed to get window size";
}
}
```
// 在你的Qt代码中你可以像这样调用这个函数
// 例如如果你有一个QAxWidget的实例并且你想要获取其HWND的大小
// QAxWidget* axWidget = ...; // 你的QAxWidget实例
// HWND hwnd = (HWND)axWidget->winId(); // 获取HWND
// getWindowSizeFromHWND(hwnd);
注意在上面的代码中我使用了QAxWidget的winId()函数来获取HWND。但是请注意winId()函数可能在窗口未完全创建或未显示时返回无效的句柄。因此确保在调用winId()之前窗口已经被正确创建和显示。
获取主窗体句柄
``` cpp
bool Core::initHWND()
{
if(!s_hwnd){
s_hwnd = GetProcessMainWnd();
qInfo() << "Core.initHWND.hwnd.title:" << getWindowTitle(s_hwnd);
if(!s_oldWndProc){
s_oldWndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(s_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CustomWndProc)));
}
qInfo() << "Core.initHWND: init hwnd success";
}
return s_hwnd != nullptr;
}
```
获取分辨率,参考逻辑:
``` cpp
void Core::initGeometry()
{
RECT rect;
if (GetClientRect(s_hwnd, &rect)) {
QSize hwnd_size(rect.right - rect.left, rect.bottom - rect.top);
qInfo() << "分辨率:" << hwnd_size;
int w = hwnd_size.width();
QSize size(1920, 1080);
if(w <= 1280){
m_scale = 0.5;
}else if(w <= 1920){
m_scale = 2.0/3.0;
}
}
}
```
如果是qt本体而不是外部句柄直接使用qApp即可要对应修改窗口按钮字体等缩放参考逻辑
``` cpp
QScreen *primaryScreen = qApp->primaryScreen();
int w = primaryScreen->geometry().width();
int h = primaryScreen->geometry().height();
QSize size(1920, 1080);
if(w <= 1280){
m_scale = 0.5;
}else if(w <= 1920){
m_scale = 2.0/3.0;
}
size *= m_scale;
int x = (w - size.width()) / 2;
int y = (h - size.height()) / 2;
this->move(x, y);
if(m_scale == 1) return;
// 设置窗口尺寸
this->setFixedSize(size);
ui->widget->setFixedHeight(ui->widget->height()*m_scale); // 标题栏
ui->widget_3->setFixedSize(ui->widget_3->size()*m_scale); // logo图标
// 标题名称
ui->label->setStyleSheet(ui->label->styleSheet().replace("14px", QString("%1px").arg(int(14*m_scale)))
.replace("21px", QString("%1px").arg(int(21*m_scale))));
QSize buttonSize = ui->pushButton->size()*m_scale;
```
替换单个字体 大小 逻辑参考
``` cpp
ui->label_13->setStyleSheet(ui->label_13->styleSheet().replace("36px", QString("%1px").arg(int(36*m_scale))));
```