由于公司项目需要,最近开始学VC++,跟Java差别还真是大,看得我云里雾里的。。。下面是照着孙鑫老师的VC++深入详解第一章中的例子做的,我用VS2008做的,刚开始编译老报错,百度了下终于解决啦。但是没效果啊。。。郁闷。。琢磨中。。。


WinMain.cpp


 

 
  
  1. #include <windows.h> 
  2. #include <stdio.h> 
  3. LRESULT CALLBACK WinSunProc( 
  4.        HWND hwnd, 
  5.        UINT uMsg, 
  6.        WPARAM wParam, 
  7.        LPARAM lParam 
  8.        ); 
  9. int WINAPI WinMain( 
  10.        HINSTANCE hInstance, 
  11.        HINSTANCE hPreInstance, 
  12.        LPSTR lpCmdLine, 
  13.        int nShowCmd) 
  14.  WNDCLASS wndcls; 
  15.  wndcls.cbClsExtra=0
  16.  wndcls.cbWndExtra=0
  17.  wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); 
  18.  wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); 
  19.  wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); 
  20.  wndcls.hInstance=hInstance; 
  21.  wndcls.lpfnWndProc=WinSunProc
  22.  wndcls.lpszClassName="hqh2011"
  23.  wndcls.lpszMenuName=NULL
  24.  wndcls.style=CS_HREDRAW|CS_VREDRAW; 
  25.  RegisterClass(&wndcls); 
  26.  HWND hwnd; 
  27.  hwnd=CreateWindow("Hqh 2011","); 
  28.  ShowWindow(hwnd,SW_SHOWNORMAL); 
  29.  UpdateWindow(hwnd); 
  30.  MSG msg; 
  31.  while(GetMessage(&msg,NULL,0,0)) 
  32.  { 
  33.   TranslateMessage(&msg); 
  34.   DispatchMessage(&msg); 
  35.  } 
  36.  return msg.wParam; 
  37. LRESULT CALLBACK WinSunProc( 
  38.        HWND hwnd, 
  39.        UINT uMsg, 
  40.        WPARAM wParam, 
  41.        LPARAM lParam 
  42.        ) 
  43.  switch(uMsg) 
  44.  { 
  45.  case WM_CHAR: 
  46.   char szChar[20]; 
  47.   sprintf(szChar,"Char Code is %d",wParam); 
  48.   MessageBox(hwnd,szChar,"char",0); 
  49.   break; 
  50.  case WM_LBUTTONDOWN: 
  51.   MessageBox(hwnd,"mouse clicked","message",0); 
  52.   HDC hdc; 
  53.   hdc=GetDC(hwnd); 
  54.   TextOut(hdc,0,50,"程序员之家",strlen("程序员之家")); 
  55.   ReleaseDC(hwnd,hdc); 
  56.   break; 
  57.  case WM_PAINT: 
  58.   HDC hDC; 
  59.   PAINTSTRUCT ps; 
  60.   hDC=BeginPaint(hwnd,&ps); 
  61.   TextOut(hDC,0,50,"hi.baidu.com",strlen("hi.baidu.com")); 
  62.   EndPaint(hwnd,&ps); 
  63.   break; 
  64.  case WM_DESTROY: 
  65.   PostQuitMessage(0); 
  66.   break; 
  67.  default: 
  68.   return DefWindowProc(hwnd,uMsg,wParam,lParam); 
  69.  } 
  70.  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 个 ==========

但是运行了啥东西都看不到啊。。。奇怪,琢磨中。。。解决后跟上。。。。

菜鸟一个,还希望大侠们指点指点!!!