Windows Mobile 编程 (Win32) - 格式化消息框

《Windows 程序设计》第二章主要介绍Unicode。在Windows 98中只是部分支持Unicode,但在Windows Mobile 5/6中,我感觉是底层支持Unicode,不过我没有去查看官方文档是不是这样。

最后介绍了“格式化消息框”。在命令行C语言编程中,大家非常喜欢使用printf()函数。但在Windows程序中不能使用printf(),但可以通过变参技术来实现一个“格式化消息框”。我还是在Windows Mobile平台上做实验,代码几乎没改就可以在Windows Mobile平台运行。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int CDECL MessageBoxPrintf(TCHAR * szCaption, TCHAR * szFormat, ...)
{
    TCHAR szBuffer[1024];
    va_list pArgList;

    // The va_start macro (defined in STDARG.H) is usually equivalent to:
    // pArgList = (char *) &szFormat + sizeof (szFormat) ;

    va_start(pArgList, szFormat);

    // The last argument to wvsprintf points to the arguments

    _vsntprintf(szBuffer, sizeof(szBuffer) / sizeof(TCHAR), szFormat, pArgList);

    // The va_end macro just zeroes out pArgList for no good reason

    va_end(pArgList);

    return MessageBox(NULL, szBuffer, szCaption, 0);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPTSTR lpCmdLine, int nShowCmd)
{
    int cxScreen, cyScreen;

    cxScreen = GetSystemMetrics(SM_CXSCREEN);
    cyScreen = GetSystemMetrics(SM_CYSCREEN);

    MessageBoxPrintf(TEXT("ScrnSize"),
        TEXT("The screen is %i pixels wide by %i pixels high."),
        cxScreen, cyScreen);

    return 0 ;
}
posted @ 2009-03-10 15:57  可乐罐  阅读(698)  评论(0编辑  收藏  举报