win32程序封装

    在写win32程序时,若每次都从头开始写代码,真的太累了,用MFC框架比较容易,可它是怎么实现的却不知道,有些书中有介绍,看起来好复杂,如果能有自己的框架就好了,就像java,那样的话就能少记些API了,还是比较像java那样把所有代码都写到类里,最近在学游戏编程,如果每天写点这样的代码,说不定就出来个小型的游戏引擎呢

 

#ifndef _APPLICATION_
#define _APPLICATION_
#include "windows.h"

class CApplication
{
protected:
	HWND hwnd;//窗口句柄
	WNDCLASS wnd;//窗口类
	DWORD style;//窗口风格
	HICON hIcon;//窗口图标
	HMENU hMenu;//窗口菜单
	HCURSOR hCursor;//窗口光标
	int m_fullScreen;//是否是全屏
	int width;//窗口宽
	int height;
	int locX;//窗口位置X
	int locY;
	BOOL bActive;//窗口激活状态
	char* title;
	DWORD backGroundColor;//背景颜色
	

public:
	CApplication();
	void SetTitle(char* t);
	HWND CreateWin(HINSTANCE hInstance,char* winName,WNDPROC winProc);//创建窗口
	void SetStyle(DWORD s);
	void SetIcon(HICON hicon);//未实现
	void SetCursor(HCURSOR hcursor);
	void SetMenu(HMENU hmenu);
	void SetFullScreen(BOOL b);
	void SetBound(int x,int y,int width,int height);
	void SetSize(int width,int height);//设置窗口大小
	void SetBackGroudColor(DWORD bgc);//设置前景颜色
	void ShowWindow();//显示窗口
	int RunDefault();//主窗口消息循环
	virtual ~CApplication();
};

#endif

  

 

实现:

 

#include "Application.h"

CApplication::CApplication()
{
	title="程序";
	backGroundColor=BLACK_BRUSH;
	hCursor=::LoadCursor(0,IDC_ARROW);
	hIcon=::LoadIcon(0,IDI_APPLICATION);
	style=WS_OVERLAPPEDWINDOW;
	locX=0;
	locY=0;
	width=800;
	height=600;

	
}
CApplication::~CApplication(){}
HWND CApplication::CreateWin(HINSTANCE hInstance,char* winName,WNDPROC winProc)
{

	wnd.lpszClassName=winName;
	wnd.hInstance=hInstance;
	wnd.cbClsExtra=0;
	wnd.cbWndExtra=0;
	wnd.hbrBackground=(HBRUSH)GetStockObject(backGroundColor);
	wnd.hCursor=hCursor;
	wnd.hIcon=hIcon;
	wnd.lpfnWndProc=winProc;
	wnd.lpszMenuName=0;
	wnd.style=CS_VREDRAW|CS_HREDRAW;
	if(!::RegisterClass(&wnd)){
		MessageBox(0,"注册窗口出错!","error",0);
		return 0;
	}
	hwnd=::CreateWindow(
		winName,
		title,
		style,
		locX,
		locY,
		width,
		height,
		0,
		0,
		hInstance,
		0);

	return hwnd;
}
void CApplication:: SetStyle(DWORD s)
{
}
void CApplication:: SetIcon(HICON hicon)
{}
void CApplication:: SetCursor(HCURSOR hcursor)
{}
void CApplication:: SetMenu(HMENU hmenu)
{}
void CApplication:: SetFullScreen(BOOL b)
{}
void CApplication:: SetBound(int x,int y,int width,int height)
{}
void CApplication:: SetSize(int width,int height)
{}
void CApplication::SetTitle(char* t)
{
	title=t;
}
void CApplication:: SetBackGroudColor(DWORD bgc)
{
	backGroundColor=bgc;
}

void CApplication::ShowWindow()
{
	if(!hwnd)
	{
		MessageBox(0,"没有窗口要显示!","error",0);
		return;
	}

	::ShowWindow(hwnd,SW_SHOW);
}
int CApplication::RunDefault()
{
	MSG msg;
	while(::GetMessage(&msg,0,0,0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}

 

 

用例:

#include "windows.h"
#include "Application.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
)
{
	CApplication a;
	a.CreateWin(hInstance,"sdfsdf",WindowProc);//创建窗口
	a.ShowWindow();//显示
	a.RunDefault();//消息循环
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用C++代码封装win32操作类, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值