#include <tchar.h>
#include <Windows.h>

const TCHAR szWndClass[] = _T("Program343class");

LRESULT CALLBACK WndProc(
	HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	PSTR lpCmdLine,
	int nCmdShow) {
	HWND hWnd;
	WNDCLASS wc;
	MSG msg;
	BOOL bRet;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbWndExtra = 0;
	wc.cbClsExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = szWndClass;

	if (!RegisterClass(&wc)) { return -1; }

	hWnd = CreateWindow(
		szWndClass,
		_T("Program343のウィンドウ"),
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (hWnd == NULL) { return -1; }

	while (bRet = GetMessage(&msg, NULL, 0, 0)) {
		if (bRet == -1) { return FALSE; }
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(
	HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rctDimension;

	static BOOL blRight = TRUE;
	static int x = 0;
	static BOOL blBottom = TRUE;
	static int y = 0;
	int dx = 5;

	switch (msg) {
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CREATE:
		SetTimer(hWnd, 1, 1, NULL);
		break;
	case WM_TIMER:
		GetClientRect(hWnd, &rctDimension);
		if (x + 50 >= rctDimension.right) { blRight = FALSE; }
		else if (x <= 0) { blRight = TRUE; }

		if (y + 50 >= rctDimension.bottom) { blBottom = FALSE; }
		else if (y <= 0) { blBottom = TRUE; }

		if (blRight) { x += dx; }
		else { x -= dx; }

		if (blBottom) { y += dx; }
		else { y -= dx; }

		InvalidateRect(hWnd, NULL, TRUE);
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		SelectObject(hdc, GetStockObject(LTGRAY_BRUSH));
		Ellipse(hdc, x, 100, x + 50, 150);
		EndPaint(hWnd, &ps);
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}
