(*
Project: Random Colors Squares
Original C++ Source: http://www.up.poznan.pl/~rjk/pages/ozdgk/3_4.html
Ported to Delphi by: Sn!per X ^ AT4RE
Code Type: Delphi / WinAPI
Release Date: 22-11-2015
*)
program RandomColorsSquares;
uses Windows, Messages;
const
g_strKlasaOkna = 'od0dogk_Window';
progname = 'Random Colors Squares';
var g_hdcOkno: HDC;
Function iRandom(nMin, nMax: Integer): Integer;
begin
Result := Random( (nMax - nMin + 1) + nMin);
End;
Function WindowEventProc(hWnd,uMsg:Longint; wParam : WPARAM; lParam: LPARAM):Longint; stdcall;
var rcObszarKlienta, rcProstokat: TRECT;
Begin
Result := 0;
Case uMsg of
WM_TIMER:
begin
// pobieramy obszar klienta
GetClientRect (hWnd, rcObszarKlienta);
// generujemy wspَlrzedne nowego prostokata;
// prawa i dolna krawedz dobieramy tak,
// aby zawsze byla polozona miedzy lewa/gَrna krawedzia
// prostok. i prawa/dolna krawedzia obszaru klienta okna
rcProstokat.left := iRandom(0, rcObszarKlienta.right);
rcProstokat.right := iRandom(rcProstokat.left,
rcObszarKlienta.right);
rcProstokat.top := iRandom(0, rcObszarKlienta.bottom);
rcProstokat.bottom := iRandom(rcProstokat.top,
rcObszarKlienta.bottom);
// ustawiamy losowy kolor pedzla
SetDCBrushColor (g_hdcOkno,
RGB(iRandom(0, 255),
iRandom(0, 255),
iRandom(0, 255)));
// rysujemy prostokat
Rectangle (g_hdcOkno, rcProstokat.left, rcProstokat.top,
rcProstokat.right, rcProstokat.bottom);
Result := 0; Exit;
end;
WM_DESTROY:
Begin
// konczymy program
PostQuitMessage (0);
Result := 0;
end;
End;
Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
end;
var WinClass: WNDCLASSEX;
iHwnd : HWND;
MSG: TMSG;
begin
ZeroMemory (@WinClass, sizeof(WNDCLASSEX));
WinClass.cbSize := sizeof(WNDCLASSEX);
WinClass.hInstance := hInstance;
WinClass.lpfnWndProc := @WindowEventProc;
WinClass.lpszClassName := g_strKlasaOkna;
WinClass.hCursor := LoadCursor(0, IDC_ARROW);
WinClass.hIcon := LoadIcon(0, IDI_APPLICATION);
WinClass.hbrBackground := GetStockObject(WHITE_BRUSH);
WinClass.style := CS_OWNDC // wlasny kontekst urzadzenia okna
or CS_HREDRAW or CS_VREDRAW;
// rejestrujemy klase okna
RegisterClassEx(WinClass);
iHwnd:= CreateWindow(
g_strKlasaOkna,
progname,
WS_SYSMENU or WS_CAPTION or WS_BORDER or WS_OVERLAPPED or WS_VISIBLE or WS_MINIMIZEBOX,
CW_USEDEFAULT,
0,
320+2,
200+16+2,
0,
0,
hInstance,
0);
ShowWindow(iHwnd,SW_SHOW);
UpdateWindow(iHwnd);
g_hdcOkno := GetDC(iHwnd);
// ustawiamy mu elastyczny pedzel
DeleteObject(SelectObject(g_hdcOkno, GetStockObject(DC_BRUSH)));
SetTimer (iHwnd, 1, 250, nil); //* milisekund */
while (GetMessage(MSG, 0, 0, 0)) do
Begin
TranslateMessage(MSG);
DispatchMessage(MSG);
end;
end.