#include <windows.h>
class Window {
public:
Window(char* title, int width, int height);
~Window();
void show();
void hide();
void update();
LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
bool keys[256];
private:
HWND hwnd;
HDC hdc;
};
void Window::update() {
MSG msg;
if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
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:
PostQuitMessage(0);
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);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT) {
Window win1("envio", 400, 300);
win1.show();
bool done = false;
while (!done) {
win1.update();
if (win1.keys[VK_ESCAPE]) {
done = true;
}
}
return 0;
}
encapsulating windows
Basically, I want to encapsulate the windows functions into an easy to use class.
Here's the code I have so far.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement