2009-12-10 67 views
0

使用的CoreFoundation,我可以顯示以下內容的警告對話框:在Windows上顯示自定義按鈕標題的提醒?

CFUserNotificationDisplayAlert(0.0, 
           kCFUserNotificationPlainAlertLevel, 
           NULL, NULL, NULL, 
           CFSTR("Alert title"), 
           CFSTR("Yes?), 
           CFSTR("Affirmative"), 
           CFSTR("Nah"), 
           NULL, NULL); 

這個使用Windows C API如何複製?我已經得到最接近的是:

MessageBox(NULL, "Yes?", "Alert title", MB_OKCANCEL); 

但硬編碼「確定」和「取消」的按鈕標題,這不是我想要的。有沒有辦法解決這個問題,或者使用其他功能?

回答

4

您可以使用SetWindowText更改按鈕上的圖例。因爲MessageBox()阻止執行流程,所以需要一些機制來解決這個問題 - 下面的代碼使用計時器。

我認爲FindWindow代碼可能依賴於MessageBox()沒有父項,但我不確定。

int CustomMessageBox(HWND hwnd, const char * szText, const char * szCaption, int nButtons) 
{ 
    SetTimer(NULL, 123, 0, TimerProc); 
    return MessageBox(hwnd, szText, szCaption, nButtons); 
} 

VOID CALLBACK TimerProc(  
    HWND hwnd, 
    UINT uMsg, 
    UINT_PTR idEvent, 
    DWORD dwTime 
) 
{ 
    KillTimer(hwnd, idEvent); 
    HWND hwndAlert; 
    hwndAlert = FindWindow(NULL, "Alert title"); 
    HWND hwndButton; 
    hwndButton = GetWindow(hwndAlert, GW_CHILD); 
    do 
    { 
     char szBuffer[512]; 
     GetWindowText(hwndButton, szBuffer, sizeof szBuffer); 
     if (strcmp(szBuffer, "OK") == 0) 
     { 
      SetWindowText(hwndButton, "Affirmative"); 
     } 
     else if (strcmp(szBuffer, "Cancel") == 0) 
     { 
      SetWindowText(hwndButton, "Hah"); 
     } 
    } while ((hwndButton = GetWindow(hwndButton, GW_HWNDNEXT)) != NULL); 
} 
+0

絕對邪惡的+1! – Eric 2009-12-22 22:31:00

+0

+1 LOL這實際上會工作 – 2009-12-23 08:34:27

+0

niice代碼。的確,愚蠢的傢伙。 +1 – Hobhouse 2009-12-23 21:39:45

4

Windows MessageBox函數僅支持有限數量的樣式。如果你想要提供更復雜的東西,你需要創建自己的對話框。有關可能的MessageBox類型的列表,請參見MessageBox

如果您決定製作自己的對話框,我建議您查看DialogBox Windows功能。

1

如果您願意將自己綁定到Windows Vista及更高版本,則可能需要考慮「TaskDialog」功能。我相信它會讓你做你想做的事。

+0

按鈕名稱是預定義的。 – 2009-12-23 08:32:48

相關問題