/* Modeless.c */
#include "windows.h"

HINSTANCE hinstance;
HWND hwndmain;
WNDPROC buttonproc;	// Original button control proc

static void apppaint(HDC hdc, HWND hwnd, LPPAINTSTRUCT lpps){
	HBRUSH hbr= CreateSolidBrush(RGB(255,204,153));
	FillRect(hdc, &(lpps->rcPaint), hbr);
	DeleteObject(hbr);
}
BOOL CALLBACK About(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam){
	switch (message){
	case WM_INITDIALOG:
		return TRUE;
	case WM_COMMAND:
		switch(LOWORD(wParam)){
		case IDOK:
		case IDCANCEL:
			EndDialog(hdlg, TRUE);
			return TRUE;
		} break;
	}
	return FALSE;
}
long CALLBACK TinyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT ps;
	HDC hdc;
	static POINT ppt= {40,70};

	if(message==WM_PAINT){
		char buf[32];
		HBRUSH hbr= CreateSolidBrush(RGB(255,150,0));
		hdc= BeginPaint(hwnd, &ps);
		FillRect(hdc, &ps.rcPaint, hbr);

		GetWindowText(hwnd, buf, sizeof(buf));
		SetBkMode(hdc, TRANSPARENT);
		TextOut(hdc, 30, 40, buf, strlen(buf));

		BitBlt(hdc, ppt.x-8, ppt.y-8, 17, 17, hdc, 0, 0, BLACKNESS);
		EndPaint(hwnd, &ps);
		return TRUE;
	}
	if(message==WM_LBUTTONDOWN){
		ppt.x= LOWORD(lParam);
		ppt.y= HIWORD(lParam);
		InvalidateRect(hwnd, 0, 0);
		UpdateWindow(hwnd);
		return TRUE;
	}
	return buttonproc(hwnd, message, wParam, lParam);
}
BOOL FAR PASCAL MainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT ps;
	HDC hdc;
	HWND hw;
	int id;

	switch (message){
	case WM_INITDIALOG:
		if(hw= GetDlgItem(hwnd
, 101)){
			buttonproc= (WNDPROC)GetWindowLong(hw, GWL_WNDPROC);
			SetWindowLong(hw, GWL_WNDPROC, (long)TinyProc);
		}
		return TRUE;
	case WM_PAINT:
		hdc= BeginPaint(hwnd, &ps);
		apppaint(hdc, hwnd, &ps);
		EndPaint(hwnd, &ps);
		return TRUE;
	case WM_COMMAND:
		switch(id= LOWORD(wParam)){
		case 101:
			DialogBox(hinstance, "AboutBox", hwnd, About);
			return TRUE;
		case 100:
			SendMessage(hwnd, WM_CLOSE, 0, 0L);
			return TRUE;
		} break;
	case WM_CLOSE:
		ShowWindow(hwnd, SW_HIDE);
		DestroyWindow(hwnd);
		return TRUE;
	case WM_DESTROY:
		if(buttonproc)
			SetWindowLong(GetDlgItem(hwnd, 101), GWL_WNDPROC, (long)buttonproc);
		PostQuitMessage(0);
		return 0;
	}
	return 0;
}
BOOL InitInstance(HANDLE hInstance, int nCmdShow){
	hinstance= hInstance;
	hwndmain= CreateDialog(hinstance, "BOARD", 0, (DLGPROC)MainProc);
	if(!hwndmain)
		return FALSE;
	ShowWindow(hwndmain, nCmdShow);
	UpdateWindow(hwndmain);
	return TRUE;
}
int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
	MSG msg;

	if(!InitInstance(hInstance, nCmdShow))
		return FALSE;
	for(;;){
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			if(msg.message == WM_QUIT)
				break;
			if(IsDialogMessage(hwndmain, &msg))
				continue;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}else
			WaitMessage();
	}
	return msg.wParam;
}

