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

发布于 2024-08-14 18:14:09 字数 660 浏览 3 评论 0 原文

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

但是将“确定”和“取消”硬编码为按钮标题,这不是我想要的。有什么办法可以解决这个问题,或者可以使用替代函数吗?

Using CoreFoundation, I can display an alert dialog with the following:

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

How do I replicate this using the Windows C API? The closest I've gotten is:

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

but that hard-codes "OK" and "Cancel" as the button titles, which is not what I want. Is there any way around this, or an alternative function to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴梦长久 2024-08-21 18:14:09

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

You can use SetWindowText to change the legend on the buttons. Because the MessageBox() blocks the flow of execution you need some mechanism to get round this - the code below uses a timer.

I think the FindWindow code may be dependent on there being no parent for MessageBox() but I'm not sure.

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 );
}
送君千里 2024-08-21 18:14:09

Windows MessageBox 函数仅支持有限数量的样式。如果您想要比所提供的更复杂的内容,则需要创建自己的对话框。有关可能的 MessageBox 的列表,请参阅 MessageBox类型。

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

The Windows MessageBox function only supports a limited number of styles. If you want anything more complicated that what's provided, you'll need to create your own dialog box. See MessageBox for a list of possible MessageBox types.

If you decide to make your own dialog box, I'd suggest looking at the DialogBox Windows function.

一萌ing 2024-08-21 18:14:09

如果您愿意使用 Windows Vista 及更高版本,您可能需要考虑“TaskDialog”函数。我相信它会让你做你想做的事。

If you're willing to tie yourself to Windows Vista and above, you might want to consider the "TaskDialog" function. I believe it will allow you do do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文