메시지만 집중한 window

By | 2014년 6월 10일

classwindow
stdafx.h

#include <windows.h>

#ifndef _stdafx_
#define _stdafx_

#define DECLARE_MESSAGE_MAP()    static MessageMap messageMap[];
#define BEGIN_MESSAGE_MAP(class_name)	MessageMap class_name::messageMap[]={
#define END_MESSAGE_MAP()	{0,NULL}};
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);

#endif _stdafx_

stdafx.cpp
#include <windows.h>
#include "CView.h"

extern CView app;

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
    int i=0;

	while(CView::messageMap[i].iMsg!=0){
		if(iMsg==CView::messageMap[i].iMsg){
			fpCViewGlobal=CView::messageMap[i].fp;
			(app.*fpCViewGlobal)(wParam,lParam);
			return 0L;
		}
		++i;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
	app.InitInstance(hInstance,szCmdLine,iCmdShow);
	app.Run();
	return app.ExitInstance();
}

CObject.h

#include <Windows.h>

#ifndef _CObject_
#define _CObject_
//Class CObject
class CObject{
protected:
    static char szAppName[];
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wndclass;
public:
	void InitInstance(HINSTANCE hInstance, PSTR szCmdLine, int iCmdShow);
	void Run();//Message
    WPARAM ExitInstance(); //exit
};
#endif _CObject_

CObject.cpp
#include <Windows.h>
#include "stdafx.h"
#include "CObject.h"

void CObject::InitInstance(HINSTANCE hInstance, PSTR szCmdLine, int iCmdShow)
{
    wndclass.cbSize = sizeof(wndclass);
	wndclass.style  = CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra =0;
	wndclass.cbWndExtra= 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;
	wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

	RegisterClassEx(&wndclass);

	hwnd = CreateWindow(szAppName,
						"Hello Program",
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,
						0,
						CW_USEDEFAULT,
						0,
						NULL,
						NULL,
						hInstance,
						NULL);
	ShowWindow(hwnd,iCmdShow);
	UpdateWindow(hwnd);
}

void CObject::Run(){
	while(GetMessage(&msg,NULL,0,0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

WPARAM CObject::ExitInstance(){
	return msg.wParam;
}

char CObject::szAppName[]="HelloWin";

CView.h 메시지만 집중

#include <windows.h>
#include "stdafx.h"
#include "CObject.h"

#ifndef _CView_
#define _CView_

class CView;

typedef LRESULT (CView::*CViewFunPointer)(WPARAM,LPARAM);//함수포인터

typedef struct tagMessgeMap{ //구조체
    UINT iMsg;
	CViewFunPointer fp;  //함수포인터 주소
}MessageMap;
static CViewFunPointer fpCViewGlobal; //정적 주소

class CView:public CObject{
public:
	//{{AFX_MESSAGE
	LRESULT OnCreate(WPARAM,LPARAM);
	LRESULT OnDraw(WPARAM,LPARAM);
	LRESULT OnDestroy(WPARAM,LPARAM);
	LRESULT OnLButtonDown(WPARAM,LPARAM);
	//}}AFX_MESSAGE

	DECLARE_MESSAGE_MAP()
};

#endif _CView_

CView.cpp
#include <windows.h>
#include "stdafx.h"
#include "CView.h"

CView app;

//{{AFX_MESSAGE
BEGIN_MESSAGE_MAP(CView)
    {WM_CREATE,&CView::OnCreate},
	{WM_PAINT,&CView::OnDraw},
	{WM_DESTROY,&CView::OnDestroy},
	{WM_LBUTTONDOWN,&CView::OnLButtonDown},
END_MESSAGE_MAP()
//}}AFX_MESSAGE

LRESULT CView::OnCreate(WPARAM wParam,LPARAM lParam){
	return 0L;
}

LRESULT CView::OnDraw(WPARAM wParam,LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	hdc=BeginPaint(hwnd,&ps);
	GetClientRect(hwnd,&rect);
	DrawText(hdc,"Hello Window",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	EndPaint(hwnd,&ps);
	return 0L;
}

LRESULT CView::OnDestroy(WPARAM wParam,LPARAM lParam){
	PostQuitMessage(0);
	return 0L;
}

LRESULT CView::OnLButtonDown(WPARAM wParam,LPARAM lParam) {
	PostQuitMessage(0);
	return 0L;
}

MFC가 생성한 코드에 CView.h CView.cpp 만 있다고 생각해봐라.
간결함 그 자체이다.

Category: C++