#pragma once
int InitClass(char WindowName[]);
LRESULT CALLBACK MsgHandler(HWND, UINT, WPARAM, LPARAM);
// Globals.....I know
RECT WindowRect = {100, 100, 400, 400};
HINSTANCE hInstance;
int iCmdShow;
InitClass(char WindowName[])
{
MSG msg;
HWND hwnd;
WNDCLASSEX wndclass;
// Class Properties
wndclass.cbSize = sizeof(wndclass);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = MsgHandler;
wndclass.lpszClassName = "Reusable Window";
wndclass.style = CS_HREDRAW | CS_VREDRAW;
// Visual properties
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd=CreateWindow("Reusable Window", WindowName, WS_OVERLAPPEDWINDOW || WS_VISIBLE,
WindowRect.left, WindowRect.top, WindowRect.right, WindowRect.bottom,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static TEXTMETRIC TextInfo;
static int Length;
PAINTSTRUCT PaintStruct;
HDC hdc=NULL;
int x, y;
char Message[]="cmoWindow Test";
switch(iMsg)
{
case WM_CREATE:
hdc=GetWindowDC(hwnd);
GetTextMetrics(hdc, &TextInfo);
Length=strlen(Message);
ReleaseDC(hwnd, hdc);
break;
case WM_SIZE:
GetWindowRect(hwnd, &WindowRect);
break;
case WM_PAINT:
hdc=BeginPaint(hwnd, &PaintStruct);
SetTextColor(hdc, RGB(0, 0, 255));
x=(WindowRect.right-WindowRect.left)/2;
x=x-(Length*TextInfo.tmAveCharWidth)/2;
y=(WindowRect.bottom-WindowRect.top)/2;
y=y-(TextInfo.tmHeight)/2;
TextOut(hdc, x, y, Message, Length);
EndPaint(hwnd, &PaintStruct);
break;
case WM_DESTROY:
PostQuitMessage(NULL);
break;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
#include <windows.h>
#include "cmoWindow.h"
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
InitClass("Hello World");
return 0;
}