我的第一个windows程序

  今天下午本来是有课来着,是通信电子电路,现在是在是不喜欢这方面的东西了,然后在宿舍写程序,我上个礼拜买了一本windows程序设计,就是charles Petzold的英文版的,看了三章了,实践很重要,决定写一个windows程序出来,看了孙鑫老师的视屏教程,开始写了,写好了后好多问题,最主要的是由于ASCII与Unicode而引起的,其实之前在Petzold的书上看到过一点相关知识,但是真正遇到问题还是不能解决,一些函数不知道,多谢csdn的兄弟们,再次谢谢!解决了我的问题,现在总结是这样的!

1。
然后在vs2008上编译产生了这样的错误!
1>------ Build started: Project: WinMain, Configuration: Debug Win32 ------
1>Compiling...
1>WinMain.cpp
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(27) : error C2440: '=' : cannot convert from 'const char [11]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(45) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(47) : error C2065: 'SW_SHOWNOMAL' : undeclared identifier
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(72) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(75) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [23]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(78) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [20]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(85) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [18]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>e:/mystudyresource/学习记录/windows编程/winmain/winmain/winmain.cpp(89) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [19]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://e:/MyStudyResource/学习记录/windows编程/WinMain/WinMain/Debug/BuildLog.htm"
1>WinMain - 8 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这个就是由于ASCII和Unicode的问题引起的,第一个程序就遇到这个问题,还是很幸运的,以后就会注意了!

注意到了MessageBox和TextOut都是有多种表示的,它们里面的参数直接加上L就能够解决!在最开始我就给都加上了L,但是还是出现了问题,这个问题就是问题2!

2.

sprintf函数,这个函数加上后就会出现不匹配的问题,据网友得知,这个函数的宽字符形式为:swprintf

我重新定义了szChar,wchar_t szChar;

这样szChar就是宽字符型了,然后用swprintf函数,swprintf(szChar,L"char is %d",wParam);

还有一个函数是我自己发现的,呵呵,兴奋!就是strlen这个函数,在这儿当然也是不可以用的,我在书上见到过这个函数的宽字符型,wcslen,然后这样程序就算全部转成了Unicode型的!运行通过,兴奋啊!呵呵,代码贴出来:

#include<windows.h>
#include<stdio.h>


LRESULT CALLBACK winmainpro(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);
 
int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=winmainpro;
    wndcls.lpszClassName=L"weiwenjing";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndcls);

    HWND hwnd;
    hwnd=CreateWindow(
  L"weiwenjing",  // pointer to registered class name
  L"weiwenjing", // pointer to window name
  WS_OVERLAPPEDWINDOW,        // window style
  0,                // horizontal position of window
  0,                // vertical position of window
  600,           // window width
  400,          // window height
  NULL,      // handle to parent or owner window
  NULL,          // handle to menu or child-window identifier
  hInstance,     // handle to application instance
  NULL        // pointer to window-creation data
);

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

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

    return 0;
}

LRESULT CALLBACK winmainpro(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
    switch(uMsg)
    {
    case WM_CHAR:
        wchar_t szChar[20];
        swprintf(szChar,L"char is %d",wParam);
        MessageBox(hwnd,szChar,L"iloveyou",0);
        break;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,L"left button is checked",L"iloveyou",0);
        HDC hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,50,L"第一个程序,happy !",wcslen(L"第一个程序,happy !"));
        ReleaseDC(hwnd,hdc);
        break;
    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,0,L"windows编程第一课",wcslen(L"windows编程第一课"));
        EndPaint(hwnd,&ps);
        break;
    case WM_CLOSE:
        if(IDYES==MessageBox(hwnd,L"是否真的退出窗口!",L"iloveyou",MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/jackhub/p/3147255.html