C++ 使用 Qt5 在 Windows 上获取 HWND(来自 WId)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14048565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:55:59  来源:igfitidea点击:

Get HWND on windows with Qt5 (from WId)

c++windowsqtuser-interfaceqt5

提问by Josef says Reinstate Monica

I am trying to convert a Qt4 Application to Qt5. The only thing I couldn't figure out is how to get the HWNDof a Widget. The program uses EcWin7to show the progress on the taskbar icon on win 7+ but expects a HWND. The lib itself seems to compile fine after changing Q_WS_WINto Q_OS_WIN) In Qt4 on Windows WIdwas just a typedef for HWND, so this was no problem. In Qt5 this is not the case anymore. I found some mailing list postingthat could give a clue but it seems QPlatformNativeInterfaceis not part of the public API of Qt5 anymore.

我正在尝试将 Qt4 应用程序转换为 Qt5。我唯一想不通的是如何获得小部件的HWND。该程序使用EcWin7在 win 7+ 上的任务栏图标上显示进度,但需要HWND。在将Q_WS_WIN更改为Q_OS_WIN后,lib 本身似乎编译得很好)在 Windows 上的 Qt4 WId只是HWND 的类型定义,所以这没问题。在 Qt5 中,情况不再如此。我发现了一些邮件列表中,可以给一个线索,但似乎QPlatformNativeInterface不是QT5的公共API的一部分了。

The program calls EcWin7.init(this->winId());and I need to some way to convert this ID into the HWNDid or some other way to get this.

程序调用EcWin7.init(this->winId()); 我需要某种方式将此 ID 转换为HWNDid 或以其他方式获取此 ID。

采纳答案by MrElmar

In Qt5 winEventwas replaced by nativeEvent:

在 Qt5 中winEvent被替换为nativeEvent

bool winEvent(MSG* pMsg, long* result)

is now

就是现在

bool nativeEvent(const QByteArray & eventType, void * message, long *result)

And in EcWin7::winEventyou have to cast voidto MSG:

而在EcWin7::winEvent你必须投射voidMSG

bool EcWin7::winEvent(void * message, long * result)
{
    MSG* msg = reinterpret_cast<MSG*>(message);
    if (msg->message == mTaskbarMessageId)
    {
      ...

I was able to get the application to work! Just replace:

我能够让应用程序工作!只需更换:

 mWindowId = wid;

with

 mWindowId = (HWND)wid;

回答by KindDragon

#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; 
}

回答by TheFox

You may try:

你可以试试:

(HWND)QWidget::winId();

回答by user1111324

winId() worked for me on Qt 5.1 at least it has the same value when I'm using

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;
}

and

qDebug() << winId();

回答by Filippok

Try this function: QWindowsNativeInterface::nativeResourceForWindow

试试这个功能: QWindowsNativeInterface::nativeResourceForWindow