在写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;
}