c - 来自 Charles Petzold 的纯 C 中的 MsgBoxPrintf(win32 api) 提供中文输出...我做错了什么?

标签 c winapi unicode

VS2013社区版操作系统:Win7 sp1

#include<Windows.h>
#include<stdio.h>


    //VS2013 Community edidtion OS:Win7 sp1 

    //Using wchar_t for unicode and L"  " for strings

    //The MessageBoxPrintf from the book modified to take wide chars

    int CDECL MsgBoxPrintf(wchar_t *szCaption,const wchar_t *szFormat, ...)
    {
        wchar_t szBuffer[1024];
        va_list pArgsList;
        va_start(pArgsList, szFormat);

     //Using _vsnwprintf_s since _vsntprintf is deprecated   
        _vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(wchar_t), 1024-1, szBuffer, pArgsList);
        va_end(pArgsList);
    //Using MessageBoxW instead of MessageBox
        return MessageBoxW(0, szBuffer, szCaption, 0);
    }

    int
    WINAPI
    WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            PSTR szCmdLine,
            int iCmdShow)
    {
        int cxScreen, cyScreen;
        cxScreen = GetSystemMetrics(SM_CXSCREEN);
        cyScreen = GetSystemMetrics(SM_CYSCREEN);
        MsgBoxPrintf(L"ScreenSize",L"The Screen is %i Pixels Width and %i Pixels Height.***Resolution(%ix%i)***",
            cxScreen, cyScreen, cxScreen, cyScreen
        );


        return(0);

    }

除了字符输出都是一样的中文外,一切正常吗?符号。无论我使用哪种 printf,我都无法得到正确的结果。 我做错了什么?

最佳答案

您对 _vsnwprintf_s 的倒数第二个参数是错误的。

_vsnwprintf_s(szBuffer, sizeof(szBuffer)/sizeof(wchar_t), 1024-1, szBuffer, pArgsList);
//                                                       this ======^

应该是szFormat;不是 szBuffer

关于c - 来自 Charles Petzold 的纯 C 中的 MsgBoxPrintf(win32 api) 提供中文输出...我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28262937/

相关文章:

c - 使用 getchar 读取用户输入并创建二维矩阵

c - 从函数参数获取数组的大小

c - 如何将用 C 编写的 block 传递给 Ruby 的 rb_mod_refine?

c++ - 为什么 rundll 缺少条目?

c - libmodbus 的多线程?

c - 关于进程和生成它的进程 (win32/C)

winapi - 使用 PrintWindow 制作屏幕截图时出现黑色图片

unicode - 为什么 Unicode.org 不再提供引用 UTF-8/16/32 转换器?

将 uint8_t 数组转换为 uint16_t 数组(ASCII 到 Unicode)

unicode - Inno Setup 读取 Ansi 和 Unicode 编码的文件