在Windows上显示带有自定义按钮标题的警报? [英] Display alert with custom button titles on Windows?

查看:94
本文介绍了在Windows上显示带有自定义按钮标题的警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CoreFoundation,我可以显示以下警告对话框:

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

如何使用Windows C API复制此文件?我得到的最接近的是:

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

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

但是硬编码"OK"和"Cancel"作为按钮标题,这不是我想要的.有什么办法解决这个问题,或者可以使用替代功能?

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?

推荐答案

您可以使用SetWindowText更改按钮上的图例.因为MessageBox()阻止了执行流程,所以您需要某种机制来解决这一问题-下面的代码使用了计时器.

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.

我认为FindWindow代码可能取决于MessageBox()没有父级,但我不确定.

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

这篇关于在Windows上显示带有自定义按钮标题的警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆