Code/C C++
[MFC] Simplified MFC Structure
Hide Code
2010. 4. 24. 19:56
MyFC.h
#pragma once
#include <windows.h>
#include <tchar.h>
#ifndef AFXAPI
#define AFXAPI __stdcall
#endif
////////////////////////////////////////
class CFrameWnd
{
public:
CFrameWnd();
BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName);
BOOL ShowWindow(int nCmdShow);
void UpdateWindow();
private:
HWND m_hWnd;
};
////////////////////////////////////////
class CWinApp
{
public:
CWinApp();
virtual BOOL InitInstance();
virtual int Run();
CFrameWnd* m_pMainWnd;
};
////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#ifndef AFXAPI
#define AFXAPI __stdcall
#endif
////////////////////////////////////////
class CFrameWnd
{
public:
CFrameWnd();
BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName);
BOOL ShowWindow(int nCmdShow);
void UpdateWindow();
private:
HWND m_hWnd;
};
////////////////////////////////////////
class CWinApp
{
public:
CWinApp();
virtual BOOL InitInstance();
virtual int Run();
CFrameWnd* m_pMainWnd;
};
////////////////////////////////////////
MyFC.cpp
#include "MyFC.h"
////////////////////////////////////////
int AFXAPI AfxWinMain(HINSTANCE, HINSTANCE, LPTSTR, int);
CWinApp* AFXAPI AfxGetApp();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////
HINSTANCE g_hInstance = NULL;
CWinApp* afxCurrentWinApp = NULL;
CFrameWnd* afxCurrentFrameWnd = NULL;
////////////////////////////////////////
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
g_hInstance = hInstance;
int nReturnCode = -1;
CWinApp* pApp = AfxGetApp();
if (afxCurrentWinApp != NULL)
{
pApp->InitInstance();
if (afxCurrentFrameWnd != NULL)
{
nReturnCode = pApp->Run();
}
}
return nReturnCode;
}
////////////////////////////////////////
CWinApp* AFXAPI AfxGetApp()
{
return afxCurrentWinApp;
}
////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
////////////////////////////////////////
CFrameWnd::CFrameWnd()
{
afxCurrentFrameWnd = this;
}
BOOL CFrameWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName)
{
TCHAR szClass[100];
TCHAR szTitle[100];
lstrcpy(szClass, lpszClassName);
lstrcpy(szTitle, lpszWindowName);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
::RegisterClassEx(&wcex);
m_hWnd = ::CreateWindowEx(NULL, szClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, g_hInstance, NULL);
return TRUE;
}
BOOL CFrameWnd::ShowWindow(int nCmdShow)
{
return ::ShowWindow(m_hWnd, nCmdShow);
}
void CFrameWnd::UpdateWindow()
{
::UpdateWindow(m_hWnd);
}
////////////////////////////////////////
CWinApp::CWinApp()
{
afxCurrentWinApp = this;
}
BOOL CWinApp::InitInstance()
{
return TRUE;
}
int CWinApp::Run()
{
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return (int)msg.wParam;
}
////////////////////////////////////////
////////////////////////////////////////
int AFXAPI AfxWinMain(HINSTANCE, HINSTANCE, LPTSTR, int);
CWinApp* AFXAPI AfxGetApp();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////
HINSTANCE g_hInstance = NULL;
CWinApp* afxCurrentWinApp = NULL;
CFrameWnd* afxCurrentFrameWnd = NULL;
////////////////////////////////////////
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
g_hInstance = hInstance;
int nReturnCode = -1;
CWinApp* pApp = AfxGetApp();
if (afxCurrentWinApp != NULL)
{
pApp->InitInstance();
if (afxCurrentFrameWnd != NULL)
{
nReturnCode = pApp->Run();
}
}
return nReturnCode;
}
////////////////////////////////////////
CWinApp* AFXAPI AfxGetApp()
{
return afxCurrentWinApp;
}
////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
////////////////////////////////////////
CFrameWnd::CFrameWnd()
{
afxCurrentFrameWnd = this;
}
BOOL CFrameWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName)
{
TCHAR szClass[100];
TCHAR szTitle[100];
lstrcpy(szClass, lpszClassName);
lstrcpy(szTitle, lpszWindowName);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
::RegisterClassEx(&wcex);
m_hWnd = ::CreateWindowEx(NULL, szClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, g_hInstance, NULL);
return TRUE;
}
BOOL CFrameWnd::ShowWindow(int nCmdShow)
{
return ::ShowWindow(m_hWnd, nCmdShow);
}
void CFrameWnd::UpdateWindow()
{
::UpdateWindow(m_hWnd);
}
////////////////////////////////////////
CWinApp::CWinApp()
{
afxCurrentWinApp = this;
}
BOOL CWinApp::InitInstance()
{
return TRUE;
}
int CWinApp::Run()
{
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return (int)msg.wParam;
}
////////////////////////////////////////
Program.cpp
#include "MyFC.h"
class CMyWinApp : public CWinApp
{
public:
BOOL InitInstance()
{
CFrameWnd *pFrameWnd = new CFrameWnd;
m_pMainWnd = pFrameWnd;
pFrameWnd->Create(TEXT("MyClass"), TEXT("Hello World~")); // calls ::RegisterClassEx & ::CreateClassEx
pFrameWnd->ShowWindow(SW_SHOW); // calls ::ShowWindow
pFrameWnd->UpdateWindow(); // calls ::UpdateWindow
return TRUE;
}
};
CMyWinApp theApp;
class CMyWinApp : public CWinApp
{
public:
BOOL InitInstance()
{
CFrameWnd *pFrameWnd = new CFrameWnd;
m_pMainWnd = pFrameWnd;
pFrameWnd->Create(TEXT("MyClass"), TEXT("Hello World~")); // calls ::RegisterClassEx & ::CreateClassEx
pFrameWnd->ShowWindow(SW_SHOW); // calls ::ShowWindow
pFrameWnd->UpdateWindow(); // calls ::UpdateWindow
return TRUE;
}
};
CMyWinApp theApp;
[MFC] Simplest MFC Program