• Advertisement
Sign in to follow this  

encapsulating windows

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, I want to encapsulate the windows functions into an easy to use class. Here's the code I have so far.
#include <windows.h>

class Window {

	Window(char* title, int width, int height);
	void show();
	void hide();
	void update();
	LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
	bool keys[256];

	HWND hwnd;
	HDC hdc;


void Window::update() {
	MSG msg;
	if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {

LRESULT CALLBACK Window::wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
		case WM_KEYDOWN:
			keys[wParam] = true;
			return 0;
		case WM_KEYUP:
			keys[wParam] = false;
			return 0;
		case WM_CLOSE:
			return 0;
	return (DefWindowProc(hwnd, message, wParam, lParam));

Window::Window(char* title, int width, int height) {

	// window properties
	WNDCLASS wincls;
	wincls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wincls.lpfnWndProc = (WNDPROC)wndProc;
	wincls.cbClsExtra = 0;
	wincls.cbWndExtra = 0;
	wincls.hInstance = GetModuleHandle(NULL);
	wincls.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wincls.hCursor = LoadCursor(NULL, IDC_ARROW);
	wincls.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
	wincls.lpszMenuName = NULL;
	wincls.lpszClassName = "wincls";
	// register the class
	if (!RegisterClass(&wincls)) {
		MessageBox(NULL, "Unable to create windows class", "Windows Error", MB_OK);

	// create the window
	if (!(hwnd = CreateWindowEx(0, "wincls", "Tree", WS_OVERLAPPEDWINDOW, 100, 100, width, height, HWND_DESKTOP, NULL, wincls.hInstance, NULL))) {
		MessageBox(NULL, "Unable to create window", "Windows Error", MB_OK);
	// get the dc
	hdc = GetDC(hwnd);

	// show the window
	ShowWindow(hwnd, true);


Window::~Window() {

void Window::show() {
	ShowWindow(hwnd, true);

void Window::hide() {
	ShowWindow(hwnd, false);

	Window win1("envio", 400, 300);
	bool done = false;
	while (!done) {

		if (win1.keys[VK_ESCAPE]) {
			done = true;

	return 0;

I have a inkling that I'm going about it entirely the wrong way. I'm getting an error back: "'typecast' : cannot convert from 'overloaded-function' to 'WNDPROC'." I tried to get rid of the wndProc function completley and tried to process the messages in the update() function directly, but I got a runtime error when the program tried to create the window handle. Any ideas? Thanks.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement