`

win32的最简单程序

阅读更多

要建立一个窗口的过程

1,编写WNDCLASS结构

 

WNDCLASS wnd;
 wnd.lpszClassName="Window1";
 wnd.hInstance=hInstance;
 wnd.cbClsExtra=0;
 wnd.cbWndExtra=0;
 wnd.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//黑色画面
 wnd.hCursor=::LoadCursor(0,IDC_ARROW);
 wnd.hIcon=::LoadIcon(0,IDI_APPLICATION);
 wnd.lpfnWndProc=WindowProc;
 wnd.lpszMenuName=0;
 wnd.style=CS_VREDRAW|CS_HREDRAW;

 

2.注册WNDCLASS,调用RegistorClass

 

 if(!::RegisterClass(&wnd)){
  MessageBox(0,"注册窗口出错!","error",0);
  return 0;
 }

 

 

 

3.创建窗口,CreateWindow

 

 HWND hwnd=::CreateWindow(
  "Window1",
  "第一个游戏",
  WS_OVERLAPPEDWINDOW,
  100,
  100,
  800,
  600,
  0,
  0,
  hInstance,
  0);

 

 

4.显示窗口,ShowWindow()

 

::ShowWindow(hwnd,SW_SHOW);

 

 

5.while循环,处理消息

 

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

 

全部代码:

 

#include "windows.h"
//回调函数,处理消息
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
  )
{

	switch(uMsg)
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
	default:
		return ::DefWindowProc(hwnd,uMsg,wParam,lParam);

	}
	return 0;
}

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wnd;
	wnd.lpszClassName="Window1";
	wnd.hInstance=hInstance;
	wnd.cbClsExtra=0;
	wnd.cbWndExtra=0;
	wnd.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wnd.hCursor=::LoadCursor(0,IDC_ARROW);
	wnd.hIcon=::LoadIcon(0,IDI_APPLICATION);
	wnd.lpfnWndProc=WindowProc;
	wnd.lpszMenuName=0;
	wnd.style=CS_VREDRAW|CS_HREDRAW;
	if(!::RegisterClass(&wnd)){
		MessageBox(0,"注册窗口出错!","error",0);
		return 0;
	}
	HWND hwnd=::CreateWindow(
		"Window1",
		"第一个游戏",
		WS_OVERLAPPEDWINDOW,
		100,
		100,
		800,
		600,
		0,
		0,
		hInstance,
		0);
	::ShowWindow(hwnd,SW_SHOW);

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

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics