//after you compile and build this, change the file extension from .exe to .scr (it'll still run but Windows will recognize it as a screen saver program)
#include <windows.h>
#include <math.h>
#define BLACK 0

int xPos = -1, yPos = -1, NoErase, cxScreen, cyScreen, c = 0;
int x1 = 140, x2 = 300, Y1 = 400, y2 = 476, x1Erase, x2Erase, y1Erase, y2Erase;
int x1inc = 2, x2inc = 3, y1inc = 2, y2inc = 1;
int x1incErase = 2, x2incErase = 3, y1incErase = 2, y2incErase = 1;
int Buf[11] = {0,0,0,0,0,0,0,0,0,0};
DWORD fileSize, dwBytesRead, dwBytesWritten;
char Version[] = "1.1\nJune 28, 2008\nDoug Cox\njdmcox.com/";
char Filename[] = "MoireScreenSaver.dta";
BOOL fromtimer = FALSE;
HANDLE hFile;
COLORREF Colors[] = {0x0000FF, 0xFF00FF, 0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0xFFFFFF};
HWND hwnd;
HDC hdc;
HPEN hPen, hPen2;
PAINTSTRUCT ps;

void InitColor(void)
{
	if (c < 6) c++; else c = 0;
	switch (c)
	{
		case 0:
			Colors[c] = 0x0000FF;
			break;
		case 1:
			Colors[c] = 0xFF00FF;
			break;
		case 2:
			Colors[c] = 0xFF0000;
			break;
		case 3:
			Colors[c] = 0xFFFF00;
			break;
		case 4:
			Colors[c] = 0x00FF00;
			break;
		case 5:
			Colors[c] = 0x00FFFF;
			break;
		case 6:
			Colors[c] = 0xFFFFFF;
			break;
	}
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CREATE:
		ShowCursor(FALSE);
		hFile = CreateFile(Filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
		if (INVALID_HANDLE_VALUE != hFile)
		{
			ReadFile(hFile, Buf, 44, &dwBytesRead, NULL);
			CloseHandle(hFile);
		}
		return 0;

	case WM_SIZE:
		cxScreen = LOWORD(lParam);
		cyScreen = HIWORD(lParam);
		if (cxScreen != 0)//which it is the first time thru
		{//this only occurs once (the second time thru)
			NoErase = (int)sqrt(cxScreen*cyScreen);//counter to begin "erasing" lines
			if ((cxScreen == Buf[9]) && (cyScreen == Buf[10]))
			{//same resolution as before
				x1 = Buf[0];
				x2 = Buf[1];
				Y1 = Buf[2];
				y2 = Buf[3];
				x1inc = x1incErase = Buf[4];
				x2inc = x2incErase = Buf[5];
				y1inc = y1incErase = Buf[6];
				y2inc = y2incErase = Buf[7];
				c = Buf[8];
			}
			x1Erase = x1;
			x2Erase = x2;
			y1Erase = Y1;
			y2Erase = y2;
			fromtimer = TRUE;
			SetTimer(hwnd, 123, 1, NULL);//123 is an ID and 1 means send a WM_TIMER message every millisecond
//			SetTimer(hwnd, 456, 1, NULL);//ditto
//			SetTimer(hwnd, 789, 1, NULL);
//			SetTimer(hwnd, 012, 1, NULL);
//			SetTimer(hwnd, 345, 1, NULL);
//			SetTimer(hwnd, 678, 1, NULL);
		}
		return 0;
		
	case WM_TIMER:
		InvalidateRect(hwnd, NULL, FALSE);
		return 0;

	case WM_RBUTTONDOWN:
	case WM_LBUTTONDOWN:
	case WM_MOUSEMOVE:
		if ((xPos == -1) && (yPos = -1))
		{
			xPos = LOWORD(lParam);
			yPos = HIWORD(lParam);
		}
		else if ((xPos != LOWORD(lParam)) || (yPos != HIWORD(lParam)))
			DestroyWindow(hwnd);
		return 0;

	case WM_KEYDOWN:
//		if (wParam == VK_ESCAPE)
			DestroyWindow(hwnd);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		if (fromtimer)
		{
			hPen = CreatePen(PS_SOLID, 1, Colors[c]);//Colors[c]
			SelectObject(hdc, hPen);
			switch (c)
			{
				case 0:
					Colors[0] -= 0x000001;
					if (Colors[0] == 0x20)
//						Colors[0] = 0x0000FF;
						InitColor();
					break;
				case 1:
					Colors[1] -= 0x010001;
					if (Colors[1] == 0x200020)
//						Colors[1] = 0xFF00FF;
						InitColor();
					break;
				case 2:
					Colors[2] -= 0x010000;
					if (Colors[2] == 0x200000)
//						Colors[2] = 0xFF0000;
						InitColor();
					break;
				case 3:
					Colors[3] -= 0x010100;
					if (Colors[3] == 0x202000)
//						Colors[3] = 0xFFFF00;
						InitColor();
					break;
				case 4:
					Colors[4] -= 0x000100;
					if (Colors[4] == 0x002000)
//						Colors[4] = 0x00FF00;
						InitColor();
					break;
				case 5:
					Colors[5] -= 0x000101;
					if (Colors[5] == 0x002020)
//						Colors[5] = 0x00FFFF;
						InitColor();
					break;
				case 6:
					Colors[6] -= 0x010101;
					if (Colors[6] == 0x202020)
//						Colors[6] = 0xFFFFFF;
						InitColor();
					break;
			}
			MoveToEx(hdc, x1, Y1, NULL);
			LineTo(hdc, x2, y2);
			DeleteObject(hPen);
			x1 += x1inc;
			x2 += x2inc;
			Y1 += y1inc;
			y2 += y2inc;
			if ((x1 < 3) || (x1 > cxScreen - 3))
			{
				x1inc = -x1inc;
				InitColor();
			}
			if ((x2 < 3) || (x2 > cxScreen - 3))
			{
				x2inc = -x2inc;
				InitColor();
			}
			if ((Y1 < 3) || (Y1 > cyScreen - 3))
			{
				y1inc = -y1inc;
				InitColor();
			}
			if ((y2 < 3) || (y2 > cyScreen - 3))
			{
				y2inc = -y2inc;
				InitColor();
			}
			if (NoErase)
				NoErase--;
			else
			{
				hPen2 = CreatePen(PS_SOLID, 1, BLACK);
				SelectObject(hdc, hPen2);
				MoveToEx(hdc, x1Erase, y1Erase, NULL);
				LineTo(hdc, x2Erase, y2Erase);
				DeleteObject(hPen2);
				x1Erase += x1incErase;
				x2Erase += x2incErase;
				y1Erase += y1incErase;
				y2Erase += y2incErase;
				if ((x1Erase < 3) || (x1Erase > cxScreen - 3))
					x1incErase = -x1incErase;
				if ((x2Erase < 3) || (x2Erase > cxScreen - 3))
					x2incErase = -x2incErase;
				if ((y1Erase < 3) || (y1Erase > cyScreen - 3))
					y1incErase = -y1incErase;
				if ((y2Erase < 3) || (y2Erase > cyScreen - 3))
					y2incErase = -y2incErase;
			}
		}
		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		ShowCursor(TRUE);
//		KillTimer(hwnd, 678);
//		KillTimer(hwnd, 345);
//		KillTimer(hwnd, 012);
//		KillTimer(hwnd, 789);
//		KillTimer(hwnd, 456);
		KillTimer(hwnd, 123);
		Buf[0] = x1;
		Buf[1] = x2;
		Buf[2] = Y1;
		Buf[3] = y2;
		Buf[4] = x1inc;
		Buf[5] = x2inc;
		Buf[6] = y1inc;
		Buf[7] = y2inc;
		Buf[8] = c;
		Buf[9] = cxScreen;
		Buf[10] = cyScreen;
		hFile = CreateFile(Filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
		WriteFile(hFile, Buf, 44, &dwBytesWritten, NULL);
		CloseHandle(hFile);
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	char szAppName[] = "STATIC";
	MSG          msg;
	WNDCLASS     wndclass;

	wndclass.style         = CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
		return 0;

	hwnd = CreateWindow(szAppName, NULL,
		WS_POPUP,
		0, 0, 0, 0,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, SW_SHOWMAXIMIZED);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
