Windows編寫第一個程式注意:

來源:互聯網
上載者:User

 

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應用程式和控制台程式的不同之處。  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.