由于公司项目需要,最近开始学VC++,跟Java差别还真是大,看得我云里雾里的。。。下面是照着孙鑫老师的VC++深入详解第一章中的例子做的,我用VS2008做的,刚开始编译老报错,百度了下终于解决啦。但是没效果啊。。。郁闷。。琢磨中。。。
WinMain.cpp
- #include <windows.h>
- #include <stdio.h>
- LRESULT CALLBACK WinSunProc(
- HWND hwnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam
- );
- int WINAPI WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPreInstance,
- LPSTR lpCmdLine,
- int nShowCmd)
- {
- WNDCLASS wndcls;
- wndcls.cbClsExtra=0;
- wndcls.cbWndExtra=0;
- wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
- wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
- wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
- wndcls.hInstance=hInstance;
- wndcls.lpfnWndProc=WinSunProc;
- wndcls.lpszClassName="hqh2011";
- wndcls.lpszMenuName=NULL;
- wndcls.style=CS_HREDRAW|CS_VREDRAW;
- RegisterClass(&wndcls);
- HWND hwnd;
- hwnd=CreateWindow("Hqh 2011",");
- ShowWindow(hwnd,SW_SHOWNORMAL);
- UpdateWindow(hwnd);
- MSG msg;
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WinSunProc(
- HWND hwnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam
- )
- {
- switch(uMsg)
- {
- case WM_CHAR:
- char szChar[20];
- sprintf(szChar,"Char Code is %d",wParam);
- MessageBox(hwnd,szChar,"char",0);
- break;
- case WM_LBUTTONDOWN:
- MessageBox(hwnd,"mouse clicked","message",0);
- HDC hdc;
- hdc=GetDC(hwnd);
- TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
- ReleaseDC(hwnd,hdc);
- break;
- case WM_PAINT:
- HDC hDC;
- PAINTSTRUCT ps;
- hDC=BeginPaint(hwnd,&ps);
- TextOut(hDC,0,50,"hi.baidu.com",strlen("hi.baidu.com"));
- EndPaint(hwnd,&ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd,uMsg,wParam,lParam);
- }
- return 0;
- }
编译。。。报错。。error C2440: “=”: 无法从“const char [8]”转换为“LPCWSTR”。。。
百度了下,通过修改工程属性右边的字符集 改为 使用多字节字符集搞定,默认是 使用 Unicode 字符集。
重新编译,一切OK。。
1>------ 已启动生成: 项目: HiWin32, 配置: Debug Win32 ------
1>正在编译...
1>WinMain.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\helloworld\hiwin32\winmain.cpp(56) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(366) : 参见“sprintf”的声明
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\HelloWorld\HiWin32\Debug\BuildLog.htm”
1>HiWin32 - 0 个错误,1 个警告
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========
但是运行了啥东西都看不到啊。。。奇怪,琢磨中。。。解决后跟上。。。。
菜鸟一个,还希望大侠们指点指点!!!