2009-12-10 75 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

相关问题