// Оконная процедура. Обработка сообщений, приходящих в окно.
LRESULT COverlappedWindow::windowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case WM_NCCREATE:
    {
        COverlappedWindow* window = (COverlappedWindow*)((CREATESTRUCT*)lParam)->lpCreateParams;
        SetLastError(0);
        SetWindowLongPtr(handle, GWLP_USERDATA, (LONG)window);
        if( GetLastError() != 0 ) {
            return GetLastError();
        }
        window->OnNCCreate(handle);
        return DefWindowProc(handle, message, wParam, lParam);
    }
    case WM_CREATE:
    {
        COverlappedWindow* window = (COverlappedWindow*)GetWindowLongPtr(handle, GWLP_USERDATA);
        window->OnCreate();
        return DefWindowProc(handle, message, wParam, lParam);
    }
    case WM_SIZE:
    {
        COverlappedWindow* window = (COverlappedWindow*)GetWindowLongPtr(handle, GWLP_USERDATA);
        window->OnSize();
        return DefWindowProc(handle, message, wParam, lParam);
    }
    case WM_DESTROY:
    {
        COverlappedWindow* window = (COverlappedWindow*)GetWindowLongPtr(handle, GWLP_USERDATA);
        window->OnDestroy();
        return 0;
    }
    default:
        return DefWindowProc(handle, message, wParam, lParam);
    }
}
LRESULT COverlappedWindow::windowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch( message ) {
		case WM_NCCREATE:
		{
			CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(pCreate->lpCreateParams);
			SetWindowLongPtr(handle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(windowPtr));
			windowPtr->OnNCCreate(handle);
			return true;
		}
		case WM_CREATE:
		{
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
			windowPtr->OnCreate();
			break;
		}
		case WM_SIZE:
		{
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
			windowPtr->OnSize();
			break;
		}
		case WM_CTLCOLOREDIT:
		{
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
			HDC hdc = reinterpret_cast<HDC>(wParam);
			return windowPtr->OnControlColorEdit(hdc);
		}
		case WM_COMMAND:
		{
			if( HIWORD(wParam) == EN_CHANGE ) {
				COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
				windowPtr->OnTextChange();
				break;
			}
			switch( LOWORD(wParam) ) {
				case ID_FILE_SAVE:
				{
					COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
					windowPtr->saveFile();
					break;
				}
				case ID_FILE_EXIT:
				{
					COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
					windowPtr->OnClose();
					break;
				}
				case ID_VIEW_SETTINGS:
				{
					COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
					windowPtr->showSettings();
					break;
				}
				case ID_KILL:
				{
					COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
					windowPtr->OnDestroy();
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
			windowPtr->OnClose();
			break;
		}
		case WM_DESTROY:
		{
			COverlappedWindow* windowPtr = reinterpret_cast<COverlappedWindow*>(GetWindowLongPtr(handle, GWLP_USERDATA));
			windowPtr->OnDestroy();
			break;
		}
		default:
			return DefWindowProc(handle, message, wParam, lParam);
	}
	return false;
}