在windows上使用Qt5(从WId)获取HWND
Get HWND on windows with Qt5 (from WId)
我正在尝试将Qt4应用程序转换为Qt5。我唯一弄不明白的是如何获得Widget的HWND。该程序使用EcWin7在win7 +上的任务栏图标上显示进度,但需要一个HWND。在将Q_WS_WIN更改为Q_OS_WIN之后,库本身似乎可以很好地编译。在Windows的Qt4中,WId只是HWND的类型定义,所以这不是问题。在Qt5中,情况不再是这样了。我发现了一些邮件列表的帖子,可以给出一个线索,但似乎QPlatformNativeInterface不再是Qt5的公共API的一部分。
程序调用EcWin7.init(this->winId());我需要一些方法将这个ID转换为HWND ID或其他方法来获得这个
在Qt5中winEvent
被nativeEvent
取代:
bool winEvent(MSG* pMsg, long* result)
现在bool nativeEvent(const QByteArray & eventType, void * message, long *result)
在EcWin7::winEvent
中,你必须将void
转换为MSG
:
bool EcWin7::winEvent(void * message, long * result)
{
MSG* msg = reinterpret_cast<MSG*>(message);
if (msg->message == mTaskbarMessageId)
{
...
我能够让应用程序工作!只是替换:
mWindowId = wid;
mWindowId = (HWND)wid;
#include <QtGui/5.0.0/QtGui/qpa/qplatformnativeinterface.h>
static QWindow* windowForWidget(const QWidget* widget)
{
QWindow* window = widget->windowHandle();
if (window)
return window;
const QWidget* nativeParent = widget->nativeParentWidget();
if (nativeParent)
return nativeParent->windowHandle();
return 0;
}
HWND getHWNDForWidget(const QWidget* widget)
{
QWindow* window = ::windowForWidget(widget);
if (window && window->handle())
{
QPlatformNativeInterface* interface = QGuiApplication::platformNativeInterface();
return static_cast<HWND>(interface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
}
return 0;
}
你可以试试:
(HWND)QWidget::winId();
试试这个函数:QWindowsNativeInterface::nativeResourceForWindow
winId()在Qt 5.1上为我工作至少当我使用
时它有相同的值bool Widget::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
MSG* msg = reinterpret_cast<MSG*>(message);
qDebug() << msg->hwnd;
return false;
}
和
qDebug() << winId();
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- 在QT中获取窗口WID的列表
- 在windows上使用Qt5(从WId)获取HWND