It is a fairly simple matter to create a class that implements Window creation. Here is a simple example:
1. resource.h
#define IDR_MAINFRAME 128
#define IDD_ABOUTDLG 130
#define IDM_FILE 200
#define ID_FILE_OPEN (IDM_FILE + 1)
#define ID_APP_EXIT (IDM_FILE + 99)
#define IDM_HELP 300
#define ID_APP_ABOUT (IDM_HELP + 1)
2. Win32App1.cpp
#include <windows.h> // general windows definitions
#include <vector>
#include <string>
#include <iterator>
#include "resource.h"
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "Gdiplus")
using namespace std;
class CWindow
{
private:
BOOL OnCreate(
HWND hWnd,
LPCREATESTRUCT pCreateStruct
)
{
return TRUE; }
void OnPaint(
HWND hWnd
)
{
PAINTSTRUCT paintStruct; HDC hDC = BeginPaint(hWnd, &paintStruct);
RECT clientRect; GetClientRect(hWnd, &clientRect);
Graphics graphics(hDC);
PointF point;
point.Y = static_cast<REAL>(clientRect.top + 4);
point.X = static_cast<REAL>(clientRect.left + 8);
Font font(L"Verdana", 10, FontStyleRegular, UnitPoint, NULL);
REAL fontHeight = font.GetHeight(graphics.GetDpiY());
RectF boundRect;
graphics.MeasureString(L"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", 43, &font, RectF(0, 0, 600, fontHeight), &boundRect);
REAL tabValue = 4 * boundRect.Width / 43;
StringFormat stringFormat;
stringFormat.SetTabStops(0.0f, 1, &tabValue);
SolidBrush brush(Color(128, 0, 128));
EndPaint(hWnd, &paintStruct);
}
LRESULT CALLBACK WindowProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMessage)
{
case WM_CREATE:
return OnCreate(hWnd, reinterpret_cast<LPCREATESTRUCT>(lParam));
case WM_PAINT:
OnPaint(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0); break;
default:
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
return 0; }
static LRESULT CALLBACK StaticProc(
HWND hWnd,
UINT uMessage,
WPARAM wParam,
LPARAM lParam
)
{
if (uMessage == WM_NCCREATE)
{
LPCREATESTRUCT pCreatestruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
::SetWindowLongPtr(hWnd, GWL_USERDATA, PtrToUlong(pCreatestruct->lpCreateParams));
}
CWindow* pWindow = (CWindow*)::GetWindowLongPtr(hWnd, GWL_USERDATA);
if (pWindow != nullptr)
return pWindow->WindowProc(hWnd, uMessage, wParam, lParam);
else
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
public:
CWindow()
{
}
~CWindow()
{
}
HRESULT Create(
HINSTANCE hInstance
)
{
WNDCLASSEX wndClassEx; HWND hWnd;
wndClassEx.cbSize = sizeof wndClassEx;
wndClassEx.style = CS_HREDRAW | CS_VREDRAW;
wndClassEx.lpfnWndProc = StaticProc;
wndClassEx.cbClsExtra = 0;
wndClassEx.cbWndExtra = 0;
wndClassEx.hInstance = hInstance;
wndClassEx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
wndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClassEx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wndClassEx.lpszMenuName = MAKEINTRESOURCE(IDR_MAINFRAME);
wndClassEx.lpszClassName = TEXT("Win32App1");
wndClassEx.hIconSm = NULL;
if (RegisterClassEx(&wndClassEx))
{
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE, wndClassEx.lpszClassName, wndClassEx.lpszClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 600, 350, NULL, NULL, hInstance, this );
}
HRESULT hr = hWnd ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
ShowWindow(hWnd, SW_SHOWNORMAL); UpdateWindow(hWnd); }
return hr;
}
};
int APIENTRY wWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PWSTR pszCmdLine,
int nCmdShow
)
{
CWindow myWindow;
int nAppResult = -1;
ULONG_PTR gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
if (SUCCEEDED(myWindow.Create(hInstance))) {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg); }
nAppResult = msg.wParam;
}
GdiplusShutdown(gdiplusToken);
return nAppResult;
}