CreateWindowExW 创建的窗体标题中文乱码,英文只显示首字母

tuja 2007-05-09
CreateWindowExW 创建的窗体标题中文乱码,英文标题的话只显示标题首字母

	HWND hWnd = CreateWindowExW(0, 
		toUTF16z("Class name"),
		toUTF16z("我的程序"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,CW_USEDEFAULT,800,600,
		HWND_DESKTOP,
		cast(HMENU) null,
		hInst,
		null);


请各位指教,这是什么原因,LPCWSTR的参数用toUTF16z转换照量不应该有问题啊!

我试过了,MessageBox没有乱码:
MessageBoxExW(null,toUTF16z("欢迎"),toUTF16z("error"),MB_ICONERROR | MB_HELP,0);
oldrev 2007-05-09
源程序要保存为 utf8
tuja 2007-05-09
源程序是utf8啊,不然有中文字符串,编译无法通过。
我的编译是通过了,程序运行也正常,MessageBoxExW没有乱码,但windows窗体标题乱码。
qiezi 2007-05-09
我这里没有问题:
import std.c.windows.windows;
import win32.winuser;
import win32.unicode;
import std.utf;

/*  Make the class name into a global variable  */
const char[] szClassName = "Test Window";


extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
    int result;

    gc_init();			// initialize garbage collector
    _minit();			// initialize module constructor table

    try
    {
	_moduleCtor();		// call module constructors
	_moduleUnitTests();	// run unit tests (optional)

	result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

	_moduleDtor();		// call module destructors
    }

    catch (Object o)		// catch any uncaught exceptions
    {
	MessageBoxA(null, cast(char *)o.toString(), "Error",
		    MB_OK | MB_ICONEXCLAMATION);
	result = 0;		// failed
    }

    gc_term();			// run finalizers; terminate garbage collector
    return result;
}

int myWinMain (HINSTANCE hThisInstance,
			 HINSTANCE hPrevInstance,
			 LPSTR lpszArgument,
			 int nFunsterStil)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEXW wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = toUTF16z(szClassName);
    wincl.lpfnWndProc = &WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = WNDCLASSEXW.sizeof;

    /* Use default icon and mouse-pointer */
    wincl.hIcon = null;
    wincl.hIconSm = null;
    wincl.hCursor = null;
    wincl.lpszMenuName = null;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = cast(HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassExW (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowExW (
           0,                   /* Extended possibilites for variation */
           toUTF16z(szClassName),         /* Classname */
           toUTF16z("中华人民共和国万岁!"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           null,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           null                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessageW (&messages, null, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessageW(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

extern(Windows)
LRESULT WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProcW (hwnd, message, wParam, lParam);
    }

    return 0;
}
tuja 2007-05-10
谢谢oldrev和qiezi。
找到原因了。
原来用的是ascii版本,改写成utf版本,忘记把
return DefWindowProcA(hwnd, uMsg, wParam, lParam);

改成:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);


导致处理默认消息的错误。