为什么win32代码中的Window资产以中文或日文而不是英文打印?

时间:2015-08-29 11:27:54

标签: c++ windows

这个程序与win32编程中的Window Assets有关。当我执行它时,不是用英文打印出“在这里插入文本......”,而是用中文或日文打印出来。为什么呢?

#include "stdafx.h"
#include "Window-Assets.h"
#include <windows.h>
#define IDC_MAIN_BUTTON 101         // Button identifier
#define IDC_MAIN_EDIT   102         // Edit box identifier
HWND hEdit;

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName=L"Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;

if(!RegisterClassEx(&wClass))
{
    int nResult=GetLastError();
    MessageBox(NULL,
        L"Window class creation failed\r\n",
        L"Window Class Failed",
        MB_ICONERROR);
}

HWND hWnd=CreateWindowEx(NULL,
        L"Window Class",
        L"Windows application",
        WS_OVERLAPPEDWINDOW,
        200,
        200,
        640,
        480,
        NULL,
        NULL,
        hInst,
        NULL);

if(!hWnd)
{
    int nResult=GetLastError();

    MessageBox(NULL,
        L"Window creation failed\r\n",
        L"Window Creation Failed",
        MB_ICONERROR);
}

ShowWindow(hWnd,nShowCmd);

MSG msg;
ZeroMemory(&msg,sizeof(MSG));

while(GetMessage(&msg,NULL,0,0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
    case WM_CREATE:
    {
        // Create an edit box
        hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
            L"EDIT",
            L"",
            WS_CHILD|WS_VISIBLE|
            ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
            50,
            100,
            200,
            100,
            hWnd,
            (HMENU)IDC_MAIN_EDIT,
            GetModuleHandle(NULL),
            NULL);
        HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
        SendMessage(hEdit,
            WM_SETFONT,
            (WPARAM)hfDefault,
            MAKELPARAM(FALSE,0));
        SendMessage(hEdit,
            WM_SETTEXT,
            NULL,
            (LPARAM)"Insert text here...");

        // Create a push button
        HWND hWndButton=CreateWindowEx(NULL,
            L"BUTTON",
            L"OK",
            WS_TABSTOP|WS_VISIBLE|
            WS_CHILD|BS_DEFPUSHBUTTON,
            50,
            220,
            100,
            24,
            hWnd,
            (HMENU)IDC_MAIN_BUTTON,
            GetModuleHandle(NULL),
            NULL);
        SendMessage(hWndButton,
            WM_SETFONT,
            (WPARAM)hfDefault,
            MAKELPARAM(FALSE,0));
    }
    break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_MAIN_BUTTON:
            {
                char buffer[256];
                SendMessage(hEdit,
                    WM_GETTEXT,
                    sizeof(buffer)/sizeof(buffer[0]),
                    reinterpret_cast<LPARAM>(buffer));
                MessageBox(NULL,
                    L"buffer",
                    L"Information",
                    MB_ICONINFORMATION);
            }
            break;
        }
        break;

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
    break;
    }

    return DefWindowProc(hWnd,msg,wParam,lParam);
    }

2 个答案:

答案 0 :(得分:2)

似乎您的应用程序被定义为Unicode(=期望获得const wchar_t*个字符串),并且您在此行中发送了一些ASCII字符串(const char*):

(LPARAM)"Insert text here...");

尝试将其更改为:

(LPARAM)L"Insert text here...");

答案 1 :(得分:0)

你可以替换(LPARAM)“在这里插入文字......”); 通过 (LPARAM)TEXT(“在此处插入文字......”)); 检查windows TEXT宏。