Windows编写第一个程序注意:

 
VS200X的字符问题
 
今天学着写了一段C++程序,有一些问题,下面将问题与解决方法记录如下:
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK CallWindowProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = CallWindowProc;
wndcls.lpszClassName = "ZhengZhixin1983";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow("ZhengZhixin1983","ZhengZhixin1983",WS_OVERLAPPEDWINDOW,
   0,0,600,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK CallWindowProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch(Msg)
{
case WM_CHAR:
    char szChar[20];
    sprintf(szChar,"Char is %d",wParam);
    MessageBox(hWnd,szChar,"ZhengZhiXin1983",0);
    break;
case WM_LBUTTONDOWN:
    MessageBox(hWnd,"Mouse Click","ZhengZhiXin1983",0);
    HDC hdc;
    hdc = GetDC(hWnd);
    TextOut(hdc,0,50,"ZhengZhixin1983",strlen("ZhengZhixin1983"));
    ReleaseDC(hWnd,hdc);
    break;
case WM_PAINT:
    HDC hDC;
    PAINTSTRUCT ps;
    hDC = BeginPaint(hWnd,&ps);
    TextOut(hdc,0,0,"ZhengZhiXin1983",strlen("ZhengZhiXin1983"));
    EndPaint(hWnd,&ps);
    break;
case WM_CLOSE:
    if(IDYES == MessageBox(hWnd,"是否退出?","ZhengZhiXin1983",MB_YESNO))
     DestroyWindow(hWnd);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
      break;
default:
    return DefWindowProc(hWnd,Msg,wParam,lParam);
}
return 0;
}
编译后出现问题:
d:\vc++_lesson1\winmain\winmain\winmain.cpp(28) : error C2440: '=' : cannot convert from 'const char [16]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(35) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(63) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(66) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(69) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [16]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(76) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [16]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\vc++_lesson1\winmain\winmain\winmain.cpp(80) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
基本都是字符串无法转换问题,并且添加强制转换后,程序运行出现乱码。
解决方法:项目-->属性-->配置属性-->常规-->字符集-->使用多字节字符集,并且不用加强制转换。

另外一点就是:建立工程的时候应该建win32应用程序,而不是win32控制台程序,因为后者的入口函数是main函数,而win32应用程序的入口函数是WinMain函数。这就是win32应用程序和控制台程序的不同之处。
 
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值