window api创建窗体的一般步骤

//共三个主要步骤,1 注册wndclassex,2 产生窗体并实现msg循环,3 实现winproc回调函数

#include 

void registerMyWndclass(HINSTANCE hInstance);
LRESULT CALLBACK winProc(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam);


int APIENTRY WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	registerMyWndclass(hInstance);


//2创建窗口并实现循环
	HWND hWnd=CreateWindow("MYWINDOW","running snake",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,300,640,480,0,0,hInstance,NULL);
	if(hWnd)
	{
		ShowWindow(hWnd,SW_NORMAL);
		UpdateWindow(hWnd);
	}



	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
//1注册窗口类
void registerMyWndclass(HINSTANCE hInstance)
{
	WNDCLASSEX* wc=new WNDCLASSEX;
	wc->cbClsExtra=NULL;
	wc->cbSize=sizeof(WNDCLASSEX);
	wc->cbWndExtra=NULL;
	wc->hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc->hCursor=LoadCursor(NULL,IDC_ARROW);
	wc->hIcon=LoadIcon(NULL,IDI_WINLOGO);
	wc->hIconSm=NULL;
	wc->hInstance=hInstance;
	wc->lpfnWndProc=winProc;
	wc->lpszMenuName=NULL;
	wc->lpszClassName="MYWINDOW";
	wc->style=CS_HREDRAW |CS_VREDRAW;
	RegisterClassEx(wc);
}

//3回调函数
LRESULT CALLBACK winProc(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	///...more case statement

	default:
		break;
	}
	return 	DefWindowProc(hWnd,msg,wParam,lParam);
}


你可能感兴趣的:(windows)