注册 登录
编程论坛 Windows论坛

Win32编程使用CreateWindowW 宽字符L“abc”作为标题名称,为什么标题名称只显示“a"?

kekuri 发布于 2015-06-14 01:05, 309 次点击
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
static unsigned short szAppName[] = L"abc";
int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE,
                    LPSTR szCmdLine,
                    int iCmdShow)
{   
    HWND     hWnd;
    MSG      Msg;
    WNDCLASSW WndClass;
    WndClass.style       = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra  = 0;
    WndClass.cbWndExtra  = 0;
    WndClass.hInstance   = hInstance;
    WndClass.hIcon       = LoadIcon (NULL, IDI_APPLICATION);
    WndClass.hCursor     = LoadCursor (NULL, IDC_ARROW);
    WndClass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH);
    WndClass.lpszMenuName  = NULL;
    WndClass.lpszClassName = szAppName;
   
    RegisterClassW (&WndClass);

    hWnd = CreateWindowW (szAppName,
                         szAppName,[/color]
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         NULL,
                         NULL,
                         hInstance,
                         NULL);

    ShowWindow (hWnd, iCmdShow);
    UpdateWindow (hWnd);

    while (GetMessage (&Msg, NULL, 0, 0))
    {
        TranslateMessage (&Msg);
        DispatchMessage  (&Msg);
    }
    return Msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hDc;
    PAINTSTRUCT Ps;
    RECT        Rect;

    switch (message)
    {
    case WM_PAINT:
        hDc = BeginPaint (hWnd, &Ps);
        GetClientRect (hWnd, &Rect);
        DrawTextW (hDc, szAppName, -1, &Rect,
                  DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint (hWnd, &Ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage (0);
        return 0;
    }
    return DefWindowProc (hWnd, message, wParam, lParam);
}
3 回复
#2
w2009w2015-06-14 15:09
不知道,百度一下嘛!
#3
yuccn2015-08-10 10:08
static unsigned short szAppName[] = L"abc";
改成w_char szAppName[] = L"abc" 或者 WCHAR szAppName[] = L"abc"
#4
blza31276722016-09-23 08:28
short是短整型,类名是一个字符串,改成WCHAR 就行了
1