Get HWND on windows with Qt5 (from WId)

Get HWND on windows with Qt5 (from WId)

Asked on January 2, 2019 in Qt.
Add Comment


  • 3 Answer(s)

    HWND on windows with Qt5:

    Qt5 winEvent was regained by nativeEvent

    bool winEvent(MSG* pMsg, long* result)
    

    By using directly:

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

    The EcWin7::winEvent  used  the void  to MSG

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

    The application used to work and  change

    mWindowId = wid;
    

    along

    mWindowId = (HWND)wid;
    

     

     

    Answered on January 2, 2019.
    Add Comment

    The initialize code is:

    #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;
    }
    
    Answered on January 2, 2019.
    Add Comment

    The simple statement is:

    (HWND)QWidget::winId();
    
    
    Answered on January 2, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.