//Repaint using a virtual window


#include<windows.h>
#include<cstring>
#include<cstdio>

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);

char szWinName[] = "MyWin";

char str[255];

int X=0, Y=0;
int maxX, maxY;

HDC memdc;
HBITMAP hbit;
HBRUSH hbrush;

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
{
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wcl;

	wcl.cbSize = sizeof(WNDCLASSEX);

	wcl.hInstance = hThisInst;
	wcl.lpszClassName = szWinName;
	wcl.lpfnWndProc = WindowFunc;
	wcl.style = 0;

	wcl.hIcon = LoadIcon(hThisInst, "MyIcon");
	wcl.hIconSm = NULL;
	wcl.hCursor = LoadCursor(hThisInst, "MyCursor");

	wcl.lpszMenuName = NULL;
	wcl.cbClsExtra = 0;
	wcl.cbWndExtra = 0;

	wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

	if(!RegisterClassEx(&wcl)) return 0;

	hwnd = CreateWindow(szWinName, "Using a Virtual Window by Matt Gams **** SUCK IT!!! ****",
											WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
											CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hThisInst, NULL);

	ShowWindow(hwnd, nWinMode);
	UpdateWindow(hwnd);

	while(GetMessage(&msg, NULL, 0, 0) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);

	}//end while

	return msg.wParam;

}//end WinMain


LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;

	switch(message)
	{
		case WM_CREATE:
			maxX = GetSystemMetrics(SM_CXSCREEN);
			maxY = GetSystemMetrics(SM_CYSCREEN);

			hdc = GetDC(hwnd);
			memdc = CreateCompatibleDC(hdc);
			hbit = CreateCompatibleBitmap(hdc, maxX, maxY);
			SelectObject(memdc, hbit);
			hbrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
			SelectObject(memdc, hbrush);
			PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
			ReleaseDC(hwnd, hdc);
			break;

		case WM_CHAR:
			hdc = GetDC(hwnd);
			sprintf(str, "%c", (char) wParam);

			if( (char)wParam == '\r')
			{
				Y += 14;
				X = 0;

			}//end if

			else
			{
				TextOut(memdc, X, Y, str, strlen(str) );
				TextOut(hdc, X, Y, str, strlen(str) );
				X += 10;

			}//end else

			ReleaseDC(hwnd, hdc);
			break;

		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);

			/*
			BitBlt(hdc, 0, 0, maxX, maxY, memdc, 0, 0, SRCCOPY);
			EndPaint(hwnd, &ps);
			break;

			*/
			
			BitBlt(hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left,
						ps.rcPaint.bottom-ps.rcPaint.top, memdc, ps.rcPaint.left, ps.rcPaint.top,
						SRCCOPY);

			EndPaint(hwnd, &ps);
			break;

		case WM_DESTROY:
			DeleteDC(memdc);
			DeleteObject(hbit);
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hwnd, message, wParam, lParam);

		}//end switch

	return 0;

}//end WindowFunc
