注册 登录
编程论坛 VC++/MFC

vc2010中怎么解决乱码???

环及 发布于 2012-05-12 17:17, 756 次点击
#include <windows.h>
#include<cstdio>

// Global variable
 
HINSTANCE hinst;
 
// Function prototypes.
 
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
 
// Application entry point.
 
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
 
    if (!InitApplication(hinstance))
        return FALSE;
 
    if (!InitInstance(hinstance, nCmdShow))
        return FALSE;
 
    while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
        UNREFERENCED_PARAMETER(lpCmdLine);
}
 
BOOL InitApplication(HINSTANCE hinstance)
{
    WNDCLASSEX wcx;
 
    // Fill in the window class structure with parameters
    // that describe the main window.
 
    wcx.cbSize = sizeof(wcx);          // size of structure
    wcx.style = CS_HREDRAW |
        CS_VREDRAW;                    // redraw if size changes
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure
    wcx.cbClsExtra = 0;                // no extra class memory
    wcx.cbWndExtra = 0;                // no extra window memory
    wcx.hInstance = hinstance;         // handle to instance
    wcx.hIcon = LoadIcon(NULL,
        IDI_ERROR);              // predefined app. icon
    wcx.hCursor = LoadCursor(NULL,
        IDC_ARROW);                    // predefined arrow
    wcx.hbrBackground =(HBRUSH)GetStockObject(
        WHITE_BRUSH);                  // white background brush
    wcx.lpszMenuName =  (LPCWSTR)"MainMenu";    // name of menu resource
    wcx.lpszClassName =(LPCWSTR) "MainWClass";  // name of window class
    wcx.hIconSm =(HICON)LoadImage(hinstance, // small class icon
        MAKEINTRESOURCE(5),
        IMAGE_ICON,
        GetSystemMetrics(SM_CXSMICON),
        GetSystemMetrics(SM_CYSMICON),
        LR_DEFAULTCOLOR);
 
    // Register the window class.
 
    return RegisterClassEx(&wcx);
}
 
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
    HWND hwnd;
 
    // Save the application-instance handle.
 
    hinst = hinstance;
 
    // Create the main window.
 
    hwnd = CreateWindow(
       (LPCWSTR)"MainWClass",        // name of window class
        (LPCWSTR)"哈哈",            // title-bar string
        WS_OVERLAPPEDWINDOW, // top-level window
        CW_USEDEFAULT,       // default horizontal position
        CW_USEDEFAULT,       // default vertical position
        CW_USEDEFAULT,       // default width
        CW_USEDEFAULT,       // default height
        (HWND) NULL,         // no owner window
        (HMENU) NULL,        // use class menu
        hinstance,           // handle to application instance
        (LPVOID) NULL);      // no window-creation data
 
    if (!hwnd)
        return FALSE;
 
    // Show the window and send a WM_PAINT message to the window
    // procedure.
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
 
}
LRESULT   CALLBACK   MainWndProc(
      HWND   hwnd,
      UINT   uMsg,
      WPARAM   wParam,
      LPARAM   lParam
    )
{
switch(uMsg)
{
case   WM_CHAR:
char   szchar[20];
sprintf(szchar,"char   is   %d",wParam);
MessageBox(hwnd,(LPCWSTR)szchar,(LPCWSTR)"哈哈",0);
break;
case   WM_LBUTTONDOWN:
        MessageBox(hwnd,(LPCWSTR)"mouse   click",(LPCWSTR)"哈哈",0);
        HDC   hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,50,(LPCWSTR)"heihei",strlen("heihei"));
        ReleaseDC(hwnd,hdc);
break;
case   WM_PAINT:
        HDC   hDC;
        PAINTSTRUCT   ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,0,(LPCWSTR)"嘿嘿",strlen("嘿嘿"));
        EndPaint(hwnd,&ps);
break;
case   WM_CLOSE:
        if(IDYES==MessageBox(hwnd,(LPCWSTR)"是否结束?",(LPCWSTR)"哈哈",MB_YESNO))
        {
                DestroyWindow(hwnd);
                  }
break;
case   WM_DESTROY:
        PostQuitMessage(0);
break;
default:
return   DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return   0;
}
怎么才能输出不乱码呢?
4 回复
#2
hellovfp2012-05-13 10:10
#include <windows.h>
#include <tchar.h>
#include<cstdio>

// Global variable

HINSTANCE hinst;

// Function prototypes.

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Application entry point.

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
   
    if (!InitApplication(hinstance))
        return FALSE;
   
    if (!InitInstance(hinstance, nCmdShow))
        return FALSE;
   
    while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
    UNREFERENCED_PARAMETER(lpCmdLine);
}

BOOL InitApplication(HINSTANCE hinstance)
{
    WNDCLASSEX wcx;
   
    // Fill in the window class structure with parameters
    // that describe the main window.
   
    wcx.cbSize = sizeof(wcx);          // size of structure
    wcx.style = CS_HREDRAW |
        CS_VREDRAW;                    // redraw if size changes
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure
    wcx.cbClsExtra = 0;                // no extra class memory
    wcx.cbWndExtra = 0;                // no extra window memory
    wcx.hInstance = hinstance;         // handle to instance
    wcx.hIcon = LoadIcon(NULL,
        IDI_ERROR);              // predefined app. icon
    wcx.hCursor = LoadCursor(NULL,
        IDC_ARROW);                    // predefined arrow
    wcx.hbrBackground =(HBRUSH)GetStockObject(
        WHITE_BRUSH);                  // white background brush
    wcx.lpszMenuName =  _T("MainMenu");    // name of menu resource
    wcx.lpszClassName = _T("MainWClass");  // name of window class
    wcx.hIconSm =(HICON)LoadImage(hinstance, // small class icon
        MAKEINTRESOURCE(5),
        IMAGE_ICON,
        GetSystemMetrics(SM_CXSMICON),
        GetSystemMetrics(SM_CYSMICON),
        LR_DEFAULTCOLOR);
   
    // Register the window class.
   
    return RegisterClassEx(&wcx);
}

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
    HWND hwnd;
   
    // Save the application-instance handle.
   
    hinst = hinstance;
   
    // Create the main window.
   
    hwnd = CreateWindow(
        _T("MainWClass"),        // name of window class
        _T("哈哈"),            // title-bar string
        WS_OVERLAPPEDWINDOW, // top-level window
        CW_USEDEFAULT,       // default horizontal position
        CW_USEDEFAULT,       // default vertical position
        CW_USEDEFAULT,       // default width
        CW_USEDEFAULT,       // default height
        (HWND) NULL,         // no owner window
        (HMENU) NULL,        // use class menu
        hinstance,           // handle to application instance
        (LPVOID) NULL);      // no window-creation data
   
    if (!hwnd)
        return FALSE;
   
    // Show the window and send a WM_PAINT message to the window
    // procedure.
   
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
   
}
LRESULT   CALLBACK   MainWndProc(
                                 HWND   hwnd,
                                 UINT   uMsg,
                                 WPARAM   wParam,
                                 LPARAM   lParam
                                 )
{
    switch(uMsg)
    {
    case   WM_CHAR:
        TCHAR  szchar[20];
        wsprintf(szchar,_T("char   is   %d"),wParam);
        MessageBox(hwnd,szchar,_T("哈哈"),0);
        break;
    case   WM_LBUTTONDOWN:
        MessageBox(hwnd,_T("mouse   click"),_T("哈哈"),0);
        HDC   hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,50,_T("heihei"),strlen("heihei"));
        ReleaseDC(hwnd,hdc);
        break;
    case   WM_PAINT:
        HDC   hDC;
        PAINTSTRUCT   ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,0,_T("嘿嘿"),lstrlen(_T("嘿嘿")));
        EndPaint(hwnd,&ps);
        break;
    case   WM_CLOSE:
        if(IDYES==MessageBox(hwnd,_T("是否结束?"),_T("哈哈"),MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;
    case   WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return   DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return   0;
}
#3
环及2012-05-13 11:37
谢谢
#4
TWO2LIFE2012-05-13 14:32
一楼的也可以把LPCWSTR 改为 LPCSTR执行也可以!
#5
环及2012-05-14 08:45
我照你那样不行喔把LPCWSTR 改为 LPCSTR。
1