/////////////////////////////////////////////////////////////////////////// // WindowClass.cpp - implement experimental window class // // // // Jim Fawcett, CSE775 - Distributed Objects, Summer 2010 // /////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "WindowClass.h" #define MAX_LOADSTRING 100 // Global Variables: Window* self; // will hold pointer to the application, used by // static MsgProc function to access class member data TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Forward declarations of functions included in this code module: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); //---< constructor initializes a handle to the running instance >---------- Window::Window(HINSTANCE hInstance) : hInst(hInstance) { } //----< start the application >-------------------------------------------- // // - This is a blocking call // - we should eventially run the message loop on a child thread // and return quickly // int Window::start(LPTSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInst, IDC_WINDOWCLASS, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInst); // Perform application initialization: if (!InitInstance (hInst, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_WINDOWCLASS)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return static_cast(msg.wParam); } //----< register the window type and styles >------------------------------ ATOM Window::MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWCLASS)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINDOWCLASS); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } //----< create and show the window >--------------------------------------- BOOL Window::InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } //----< register a derived class event handler >--------------------------- void Window::registerEventHandler(UINT msg, void (Window::*eh)()) { MsgMap[msg] = eh; } //----< Message processing >----------------------------------------------- // // - Uses registered message handlers if found for message // - Otherwise does default message processing // LRESULT Window::MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; // if message is registered, invoke its registered handler std::map::iterator iter; iter = self->MsgMap.find(message); if(iter != self->MsgMap.end()) { void (Window::*mPtr)(); mPtr = iter->second; (self->*mPtr)(); return 0; } // default message processing switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(self->hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } //---< Windows calls this and it delegates to our message handling >------- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { return Window::MsgProc(hWnd, message, wParam, lParam); } //----< Message handler for about box >------------------------------------ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } /////////////////////////////////////////////////////////////////////////// // DWindow class // // - Derives from Window class to provide application specific details // // - Eventually this should be given its own file /////////////////////////////////////////////////////////////////////////// class DWindow : public Window { public: DWindow(HINSTANCE hInstance) : Window(hInstance) {} void create(); void lbutton(); void paint(); }; //----< demonstrate button handling >-------------------------------------- void DWindow::lbutton() { MessageBox(NULL,_T("been clicked"), _T("Button Event"), MB_OK); } //----< define window font when window is created >------------------------ void DWindow::create() { HDC hdc = GetDC(NULL); long lfHeight = -MulDiv(24, GetDeviceCaps(hdc, LOGPIXELSY), 72); ReleaseDC(NULL, hdc); hFont = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, L"Tahoma"); } //----< paint text on window >--------------------------------------------- void DWindow::paint() { hdc = BeginPaint(hWnd, &ps); static HGDIOBJ hOld = NULL; static wchar_t* msg = L"Hello CSE775 - Distributed Objects"; hOld = SelectObject(hdc,hFont); GetClientRect(hWnd,&rect); DrawText(hdc,msg,wcslen(msg),&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE); SelectObject(hdc,hOld); hdc = BeginPaint(hWnd, &ps); } //----< entry point for application >-------------------------------------- int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DWindow win(hInstance); // initialize global pointer to the Window object self = &win; /* would like to register event handlers like this: win.registerEventHandler(WM_LBUTTONDOWN, &Window::lbutton); win.registerEventHandler(WM_LBUTTONDOWN, &Window::create); win.registerEventHandler(WM_LBUTTONDOWN, &Window::paint); That doesn't work because we want to define the event handlers in the application specific derived class, e.g., DWindow. So, for now, we do the hack you see below. */ // register event handlers void (Window::*winPtr)(); winPtr = reinterpret_cast(&DWindow::lbutton); win.registerEventHandler(WM_LBUTTONDOWN, winPtr); winPtr = reinterpret_cast(&DWindow::paint); win.registerEventHandler(WM_PAINT, winPtr); winPtr = reinterpret_cast(&DWindow::create); win.registerEventHandler(WM_CREATE, winPtr); // run the application int result = win.start(lpCmdLine, nCmdShow); return result; }