【问题标题】:How to concate values in messagebox text using C++ win32 API?如何使用 C++ win32 API 连接消息框文本中的值?
【发布时间】:2012-10-31 05:50:14
【问题描述】:

我想使用 win32 API 显示消息框...

int pwdexpirydays=5; MessageBox(hdlg,(LPCSTR)("Your password will expire in %d days",&pwdexpirydays),(LPCSTR)"Logon Message",MB_OK | MB_ICONINFORMATION);

但我无法获得价值...

我如何将pwdexpirydays 值连接到"Your password will expire in %d days" 这个字符串中。

【问题讨论】:

    标签: c++ windows winapi dialog


    【解决方案1】:

    您可以使用snprintf 或 std::string 进行连接。

    【讨论】:

    • @ Drakosha : sprintf (msg,"Your password will expire in %d days.", pwdexpirydays); strcat (msg,"Do you want to change it now?"); MessageBox(hDlg, TEXT(msg),TEXT("Logon Message"), MB_OKCANCEL| MB_ICONWARNING) 这是代码...当我运行此 GIVE ERROR LIKE THIS... Lmsg 未声明的标识符... TEXT(msg ) 问题出在哪里……怎么办?
    • @SanjuMonu - TEXT() 仅用于静态字符串,而不是变量 - 它所做的只是在 UNICODE 构建中添加 L 前缀 - 例如。 TEXT("foo") 变成 L"foo"。如果要编译为 unicode,则使用 _snwprintf 而不是 ANSI sprintf。
    • @ Drakosha :是的,我明白了。使用(LPCWSTR)msg 正确地给出了字符串值...谢谢你的帮助。
    【解决方案2】:

    如果你经常做这件事,你可能想考虑一个让它变得快速和简单的函数。

    int MsgBoxPrint(HWND hWnd, int Type, char *Caption, char *Format, ...)
    {
        va_list ArgList;
        char Temp[4096];
    
        va_start(ArgList, Format);
        vsnprintf(Temp, 4096, Format, ArgList); 
        va_end(ArgList);
    
        return MessageBox(hWnd, Temp, Caption, Type);
    }
    

    那么你可以这样称呼它:

    MsgBoxPrint(hdlg, MB_OK | MB_ICONINFORMATION, "Logon Message", \
         "Your password will expire in %d days", pwdexpirydays);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多