孙鑫VC++深入详解:Lesson1


1. 开始我从MSDN 中copy WinMain原型时,搞错了

下面的代码编译是说 WinMain不能重载.....原来是参数不对. LPWSTR, 指向宽字符串的指针,16位=2个字节=一个字word

LPCWSTR--- Pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. 

下面这个WinMain是基于WIN CE

This function is called by the system as the initial entry point for WindowsCE-based applications.

int WINAPI WinMain(
HINSTANCE hInstance, 
HINSTANCE hPrevInstance, 
LPWSTR lpCmdLine, 
int nShowCmd ); 

下面这个是基于Windows ,  for a Windows-based application.

The WinMain function is called by the system as the initial entry point for a Windows-based application. 

应是这个, LPSTR 

LPCSTR--- Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. 

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
 LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
);

2. 用couter 来统计 WM_PAINT 的消息.

最小化,最大化,被其他程序盖住界面,改变窗口的宽度 都会产生 WM_PAINT

奇怪的是 改变窗口高度 不会产生 WM_PAINT消息


    

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

LRESULT CALLBACK WindowProc(
							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,          // command line
				   int nCmdShow              // show state
				   )
{
    WNDCLASS wc={0};
//	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
	wc.hCursor = LoadCursor(NULL,IDC_CROSS);
	wc.hIcon = LoadIcon(NULL,IDI_ASTERISK);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WindowProc;
	wc.lpszClassName ="weixin2003";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW |CS_HREDRAW;

	RegisterClass(&wc);
	
	HWND hwnd;
	hwnd = CreateWindow("weixin2003",
						"--窗口标题--",
						WS_OVERLAPPEDWINDOW &~WS_MAXIMIZEBOX,/* 不是WS_MAXIMIZE */
						200,200,600,400,
						 NULL,NULL,
						 hInstance,
						 NULL);

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

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

//---
	return msg.wParam;
}

LRESULT CALLBACK WindowProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	char szChar[20];
	 HDC hdc;
	 char pchar[40]; //输出 window painted 的次数信息
	 static int cnt=0; // 统计WM_PAINT的次数,看看那些操作会导致WM_PAINT

switch(uMsg) 
		{
case WM_CHAR:

	sprintf(szChar,"char is %d",wParam);
	MessageBox(hwnd,szChar,"weixin",MB_OKCANCEL|MB_ICONQUESTION);
	  break;
case WM_LBUTTONDOWN:
	  MessageBox(hwnd,"mouse clicked","caption",0);
	 
	  hdc = GetDC(hwnd);
	  TextOut(hdc,0,50,"mouse clicked",strlen("mouse clicked"));
	  ReleaseDC(hwnd,hdc);
	  break;
case WM_PAINT:
	 cnt++;
	// sprintf(pchar,"window painted: %d",cnt);
	PAINTSTRUCT ps;
	  hdc = BeginPaint(hwnd,&ps);

	  TextOut(hdc,0,0,pchar, sprintf(pchar,"window painted: %d",cnt));
	  EndPaint(hwnd,&ps);
		break;



case WM_CLOSE:
	 DestroyWindow(hwnd);
			break;
case WM_DESTROY:
	PostQuitMessage(0);
			break;
default:
			return DefWindowProc(hwnd,uMsg,wParam,lParam);	
		
		}

return 0;	

}

孙鑫VC++深入详解:Lesson1_第1张图片


你可能感兴趣的:(孙鑫VC++深入详解:Lesson1)