簡體   English   中英

使用Qt5在Windows上獲取HWND(來自WId)

[英]Get HWND on windows with Qt5 (from WId)

我正在嘗試將Qt4應用程序轉換為Qt5。 我唯一想知道的是如何獲得Widget的HWND 該程序使用EcWin7顯示win 7+上任務欄圖標的進度,但需要一個HWND 在將Q_WS_WIN更改為Q_OS_WIN之后,lib本身似乎編譯正常。在Windows上的Qt4中, WId只是HWND的typedef,所以這沒問題。 在Qt5中,情況不再如此。 我找到了一些可以提供線索的郵件列表發布 ,但似乎QPlatformNativeInterface不再Qt5的公共API的一部分了。

該程序調用EcWin7.init(this-> winId()); 我需要某種方式將此ID轉換為HWND ID或其他方式來獲取此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();

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();

試試這個函數: QWindowsNativeInterface::nativeResourceForWindow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM