在Windows上显示带有自定义按钮标题的警报?

使用CoreFoundation,我可以显示一个警告对话框,其中包含以下内容:

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); 

但硬编码“OK”和“取消”作为按钮标题,这不是我想要的。 有没有办法解决这个问题,或者使用其他function?

您可以使用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 ); } 

Windows MessageBox函数仅支持有限数量的样式。 如果你想要提供更复杂的东西,你需要创建自己的对话框。 请参阅MessageBox以获取可能的MessageBox类型列表。

如果您决定创建自己的对话框,我建议您查看DialogBox Windowsfunction。

如果您愿意将自己绑定到Windows Vista及更高版本,则可能需要考虑“ TaskDialog ”function。 我相信它会让你做你想做的事。