//----------------------------------------------------------------------------
LRESULT CALLBACK MsWindowEventHandler (HWND handle, UINT message,
    WPARAM wParam, LPARAM lParam)
{
    WindowApplication* theApp =
        (WindowApplication*)Application::TheApplication;

    if (!theApp || !theApp->GetWindowID())
    {
        return DefWindowProc(handle, message, wParam, lParam);
    }

    switch (message) 
    {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(handle, &ps);
            theApp->OnDisplay();
            EndPaint(handle, &ps);
            return 0;
        }
        case WM_ERASEBKGND:
        {
            // This tells Windows not to erase the background (and that the
            // application is doing so).
            return 1;
        }
        case WM_MOVE:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMove(xPos, yPos);
            return 0;
        }
        case WM_SIZE:
        {
            int width = (int)(LOWORD(lParam));
            int height = (int)(HIWORD(lParam));
            theApp->OnResize(width, height);
            return 0;
        }
        case WM_CHAR:
        {
            unsigned char key = (unsigned char)(char)wParam;

            // Quit the application if the KEY_TERMINATE key is pressed.
            if (key == theApp->KEY_TERMINATE)
            {
                PostQuitMessage(0);
                return 0;
            }

            // Get the cursor position in client coordinates.
            POINT point;
            GetCursorPos(&point);
            ScreenToClient(handle, &point);
            int xPos = (int)point.x;
            int yPos = (int)point.y;

            theApp->OnKeyDown(key, xPos, yPos);
            return 0;
        }
        case WM_KEYDOWN:
        {
            int virtKey = (int)wParam;

            // Get cursor position client coordinates.
            POINT point;
            GetCursorPos(&point);
            ScreenToClient(handle, &point);
            int xPos = (int)point.x;
            int yPos = (int)point.y;

            if ((VK_F1 <= virtKey && virtKey <= VK_F12)
            ||  (VK_PRIOR <= virtKey && virtKey <= VK_DOWN)
            ||  (virtKey == VK_INSERT) || (virtKey == VK_DELETE)
            ||  (virtKey == VK_SHIFT) || (virtKey == VK_CONTROL))
            {
                theApp->OnSpecialKeyDown(virtKey, xPos, yPos);
            }
            return 0;
        }
        case WM_KEYUP:
        {
            int virtKey = (int)wParam;

            // get the cursor position in client coordinates
            POINT point;
            GetCursorPos(&point);
            ScreenToClient(handle, &point);
            int xPos = (int)point.x;
            int yPos = (int)point.y;

            if ((VK_F1 <= virtKey && virtKey <= VK_F12)
            ||  (VK_PRIOR <= virtKey && virtKey <= VK_DOWN)
            ||  (virtKey == VK_INSERT) || (virtKey == VK_DELETE)
            ||  (virtKey == VK_SHIFT) || (virtKey == VK_CONTROL))
            {
                theApp->OnSpecialKeyUp(virtKey, xPos, yPos);
            }
            else
            {
                theApp->OnKeyUp((unsigned char)virtKey, xPos, yPos);
            }
            return 0;
        }
        case WM_LBUTTONDOWN:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_LEFT_BUTTON,
                WindowApplication::MOUSE_DOWN, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_LBUTTONUP:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_LEFT_BUTTON,
                WindowApplication::MOUSE_UP, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_MBUTTONDOWN:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_MIDDLE_BUTTON,
                WindowApplication::MOUSE_DOWN, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_MBUTTONUP:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_MIDDLE_BUTTON,
                WindowApplication::MOUSE_UP, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_RBUTTONDOWN:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_RIGHT_BUTTON,
                WindowApplication::MOUSE_DOWN, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_RBUTTONUP:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            theApp->OnMouseClick(WindowApplication::MOUSE_RIGHT_BUTTON,
                WindowApplication::MOUSE_UP, xPos, yPos, PtrToUint(wParam));
            return 0;
        }
        case WM_MOUSEMOVE:
        {
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));

            int button = -1;
            if (wParam & MK_LBUTTON)
            {
                button = WindowApplication::MOUSE_LEFT_BUTTON;
            }
            else if (wParam & MK_MBUTTON)
            {
                button = WindowApplication::MOUSE_MIDDLE_BUTTON;
            }
            else if (wParam & MK_RBUTTON)
            {
                button = WindowApplication::MOUSE_RIGHT_BUTTON;
            }

            if (button >= 0)
            {
                theApp->OnMotion(button, xPos, yPos, PtrToUint(wParam));
            }
            else
            {
                theApp->OnPassiveMotion(xPos, yPos);
            }

            return 0;
        }
        case WM_MOUSEWHEEL:
        {
            short sWParam = (short)(HIWORD(wParam));
            int delta = ((int)sWParam)/WHEEL_DELTA;
            int xPos = (int)(LOWORD(lParam));
            int yPos = (int)(HIWORD(lParam));
            unsigned int modifiers = (unsigned int)(LOWORD(wParam));
            theApp->OnMouseWheel(delta, xPos, yPos, modifiers);
            return 0;
        }
        case WM_DESTROY:
        {
            // The DestroyWindow call when recreating the window for
            // multisampling causes the application to terminate.  It is
            // not clear why the same problem did not occur in WM4.
            if (!gsIgnoreWindowDestroy)
            {
                PostQuitMessage(0);
            }
            gsIgnoreWindowDestroy = false;
            return 0;
        }
    }

    return DefWindowProc(handle, message, wParam, lParam);
}