-7

我想显示一个简单的“是/否”消息框,但“否”按钮呈灰色/不活动状态。用 C win32api

4

2 回答 2

4

使用SetWindowsHookEx()SetWinEventHook()与线程本地挂钩来捕获MessageBox()对话框的 HWND,然后您可以使用EnableWindow()来禁用按钮。

以下是如何使用SetWindowsHookEx()

HHOOK hHook = NULL;

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if( nCode == HCBT_ACTIVATE )
    {
        HWND hDlg = (HWND) wParam;
        HWND hBtnWnd = GetDlgItem(hDlg, IDNO);
        if (hBtnWnd != NULL)
            EnableWindow(hBtnWnd, FALSE);
    }

    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

{
    //...

    hHook = SetWindowsHookEx(WH_CBT, &CBTProc, NULL, GetCurrentThreadId());

    MessageBox(NULL, TEXT("Test"), TEXT("Test"), MB_YESNO);

    if (hHook != NULL)
        UnhookWindowsHookEx(hHook);

    //...
}

以下是如何使用SetWinEventHook()

void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
    if( event == EVENT_SYSTEM_DIALOGSTART )
    {
        HWND hBtnWnd = GetDlgItem(hwnd, IDNO);
        if (hBtnWnd != NULL)
            EnableWindow(hBtnWnd, FALSE);
    }
}

{
    //...

    HRESULT hRes = CoInitialize(NULL);

    HWINEVENTHOOK hHook = SetWinEventHook(EVENT_SYSTEM_DIALOGSTART, EVENT_SYSTEM_DIALOGSTART, NULL, &WinEventProc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT);

    MessageBox(NULL, TEXT("Test"), TEXT("Test"), MB_YESNO);

    if (hHook != NULL)
        UnhookWinEvent(hHook);

    if (SUCCEEDED(hRes))
        CoUninitialize();

    //...
}
于 2013-06-03T21:49:17.010 回答
1

这个问题相当虚伪。同样,看看 XMessageBox。

http://www.codeproject.com/Articles/1239/XMessageBox-A-reverse-engineered-MessageBox

于 2013-06-03T21:46:57.540 回答