1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include "Base.h"
namespace WindowsGUI
{
Base::Base()
{
}
Base::Base(char *classname, char *windowname, int menuid, RECT *rect, HINSTANCE hinstance)
{
className = classname;
windowName = windowname;
pRect = rect;
hInstance = hinstance;
dwStyles = WS_VISIBLE;// | WS_POPUP;
dwExStyles = 0;
hMenu = 0;
menuName = MAKEINTRESOURCE(menuid);
}
Base::~Base()
{
if (g_hwndBase) DestroyWindow(g_hwndBase);
g_hwndBase = NULL;
}
BOOL Base::Register()
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
// wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; //used for painting the main window.
wc.lpfnWndProc = msgRouter;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
// wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszMenuName = menuName;
wc.lpszClassName = className;
wc.hInstance = hInstance;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
return true;
}
BOOL Base::Create()
{
//Last parameter passed is (void *)this. We are taking the this pointer (which points back at the object itself) and
//cast it to type void *. Windows takes that pointer and passes it to the message handler for the window,
//along with the message WM_NCCREATE, in the lParam value. This means that we can handle the WM_NCCREATE message in our
//static message handler and quickly grab the void * that points to the WrapperClass object.
g_hwndBase = CreateWindowEx(
dwExStyles,
className,
windowName,
dwStyles,
pRect->left,
pRect->top,
pRect->right,
pRect->bottom,
g_hwndParent,
// NULL,
hMenu,
hInstance,
(void *)this);
return (g_hwndBase != NULL);
}
BOOL Base::ShowWindow(int cmd)
{
::ShowWindow(g_hwndBase, cmd);
::UpdateWindow(g_hwndBase);
return true;
}
WPARAM Base::MessagePump()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
delete this;
return (msg.wParam);
}
LRESULT CALLBACK Base::wndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hwnd, msg, w, l);
}
return false;
}
//Default windows message handler. The message processor looks like this:
LRESULT CALLBACK Base::msgRouter(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
Base *pWnd = 0;
//If the window is being created, it calls SetWindowLong on the window to store the pointer to the window.
if (msg == WM_NCCREATE) SetWindowLong(hwnd, GWL_USERDATA, (long)((LPCREATESTRUCT(l))->lpCreateParams));
//Get the pointer to the window and calls that instance's wndproc
pWnd = (Base *)GetWindowLongPtr(hwnd, GWL_USERDATA);
return LRESULT (pWnd) ? pWnd->wndProc(hwnd, msg, w, l) : DefWindowProc(hwnd, msg, w, l);
}
}
|