WinAPI first gui, 2nd edition, from sun xin

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

LONG WINAPI WndProc(HWND, UINT,WPARAM,LPARAM);	//回调原型

int WINAPI WinMain(      
				   HINSTANCE 
				   hInstance,
				   HINSTANCE 
				   hPrevInstance,
				   LPSTR 
				   lpCmdLine,
				   int 
				   nCmdShow
				   )
{

	WNDCLASS wc;
	HWND hwnd;
	MSG msg;

	//1.设计窗体
	//wc.style = CS_VREDRAW | CS_HREDRAW;
	wc.style = CS_HREDRAW;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	//wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
	wc.hIcon = LoadIcon(NULL,IDI_ERROR);
	//wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hCursor = LoadCursor(NULL,IDC_CROSS);
	//wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	//wc.hbrBackground = (HBRUSH)(COLOR_GRAYTEXT);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "MyWndClass";

	RegisterClass(&wc);		//2.注册窗体

	hwnd = CreateWindow(	//3.创建窗体
			"MyWndClass",			//WNDCLASS NAME
			"SDK APPLICATION标题",		//WINDOW TITLE
			//WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,	//WINDOW STYLE
			WS_TILEDWINDOW,
			10,			//Horizontal position
			20,			//Vertical position
			800,			//Initial width
			600,			//Initial height
			HWND_DESKTOP,			//Handle of parent window
			NULL,					//Menu handle
			hInstance,				//Application's instance handle
			NULL					//Window-creation data
		);


	ShowWindow(hwnd,nCmdShow);	//4.显示更新窗体
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0)){		//5.消息循环
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam){
	
	PAINTSTRUCT ps;
	HDC hdc;
	const char chMsg[]="我是中国人";
	switch(message){
	case WM_CHAR:
		char szCh[20];
		sprintf(szCh,"char is %d",wParam);
		MessageBox(hwnd,szCh,"info",0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,"Mouse Clicked","提示",0);
		hdc=GetDC(hwnd);
		TextOut(hdc,0,50,chMsg,strlen(chMsg));
		ReleaseDC(hwnd,hdc);
		break;
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
		Ellipse(hdc,0,0,200,100);
		EndPaint(hwnd,&ps);
		break;
	case WM_CLOSE:
		if(IDYES == MessageBox(hwnd,"确定退出吗?","确认",MB_YESNO)){
			DestroyWindow(hwnd);
		}		
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,message,wParam,lParam);
	}
	return 0;
}

 

posted @ 2012-02-16 01:50  庚武  Views(193)  Comments(0Edit  收藏  举报