以管理员身份运行qt无效为什么?

时间:2017-07-25 09:42:49

标签: qt mfc administrator

qt以管理员丢弃的方式运行无效, 我在qt中使用了win32 api ChangeWindowMessageFilterEx但是没有用, 我在MFC中使用win32 api ChangeWindowMessageFilterEx工作正常。

当我以管理员身份运行时,无法在qt中收到WM_DROPFILES消息。 这是在qt

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DropTest w;
    w.show();

    CHANGEFILTERSTRUCT chfit1 = { sizeof(CHANGEFILTERSTRUCT) };
    CHANGEFILTERSTRUCT chfit2 = { sizeof(CHANGEFILTERSTRUCT) };
    CHANGEFILTERSTRUCT chfit3 = { sizeof(CHANGEFILTERSTRUCT) };
    HWND hwnd = (HWND)w.winId();;
    DragAcceptFiles(hwnd, TRUE);

    DropTest::ChangeWndMessageFilterOk(hwnd, WM_DROPFILES, MSGFLT_ALLOW, &chfit1);
    DropTest::ChangeWndMessageFilterOk(hwnd, WM_COPYDATA, MSGFLT_ALLOW, &chfit2);
    DropTest::ChangeWndMessageFilterOk(hwnd, 0x0049, MSGFLT_ALLOW, &chfit3);       // 0x0049 == WM_COPYGLOBALDATA


    //MyXcbEventFilter nativeEventFilterZ;
    //a.installNativeEventFilter(&nativeEventFilterZ);
    return a.exec();
}


BOOL DropTest::ChangeWndMessageFilterOk(HWND hWnd, UINT nMessage, DWORD dwAction, PCHANGEFILTERSTRUCT chfit)
{
    //typedef BOOL (WINAPI * ChangeWindowMessageFilterOkFn)(__in HWND hWnd, __in UINT message,  __in DWORD action, __inout_opt PCHANGEFILTERSTRUCT pChangeFilterStruct);
    typedef BOOL (WINAPI *ChangeWindowMessageFilterOkFn)(HWND, UINT, DWORD, PCHANGEFILTERSTRUCT);
    HMODULE hModUser32 = NULL;
    hModUser32 = LoadLibrary(L"user32.dll");
    if (hModUser32 == NULL) {
        return FALSE;
    }

    ChangeWindowMessageFilterOkFn pfnChangeWindowMessageFilter = (ChangeWindowMessageFilterOkFn) GetProcAddress(hModUser32, "ChangeWindowMessageFilterEx");
    if (pfnChangeWindowMessageFilter == NULL)
    {
        FreeLibrary(hModUser32);
        return FALSE;
    }

    FreeLibrary(hModUser32);
    BOOL ret = pfnChangeWindowMessageFilter(hWnd, nMessage, dwAction, chfit);
    QString strOut = QString("%1 ret = [%2] ExtStatus=[%3]\n").arg(__FUNCTION__, QString::number(ret), QString::number(chfit->ExtStatus));
    OutputDebugString(strOut.toStdWString().c_str());
    return ret;
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案, 使用RevokeDragDrop是好的! 这是qt源

void QWindowsWindow::setDropSiteEnabled(bool dropEnabled)
{
    if (isDropSiteEnabled() == dropEnabled)
        return;
    qCDebug(lcQpaMime) << __FUNCTION__ << window() << dropEnabled;
#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_DRAGANDDROP)
    if (dropEnabled) {
        Q_ASSERT(m_data.hwnd);
        m_dropTarget = new QWindowsOleDropTarget(window());
        RegisterDragDrop(m_data.hwnd, m_dropTarget);
        CoLockObjectExternal(m_dropTarget, true, true);
    } else {
        CoLockObjectExternal(m_dropTarget, false, true);
        m_dropTarget->Release();
        RevokeDragDrop(m_data.hwnd);
        m_dropTarget = 0;
    }
#endif // !QT_NO_CLIPBOARD && !QT_NO_DRAGANDDROP
}