1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <chrono>
#include <random>
#include <windows.h>
#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
int random( int range ) // Return a random number in [0, range-1]
{
static std::ranlux48 prng( std::chrono::system_clock::now().time_since_epoch().count() );
std::uniform_int_distribution <unsigned> dist( 0, range - 1 );
return dist( prng );
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
constexpr WORD IDC_STATIC_TEXT = 101;
switch (msg)
{
case WM_CREATE:
{
RECT rect;
GetClientRect( hwnd, &rect );
HWND htext = CreateWindowExA(
0,
"Static",
"I love you",
WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE,
0, 0, rect.right, rect.bottom,
hwnd,
(HMENU)IDC_STATIC_TEXT,
NULL,
NULL
);
HFONT hfont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );
SendMessage( htext, WM_SETFONT, (WPARAM)hfont, MAKELPARAM( FALSE, 0 ) );
break;
}
case WM_SIZE:
{
RECT rect;
GetClientRect( hwnd, &rect );
HWND htext = GetDlgItem( hwnd, IDC_STATIC_TEXT );
SetWindowPos( htext, NULL, 0, 0, rect.right, rect.bottom, SWP_NOZORDER );
InvalidateRect( hwnd, NULL, FALSE );
break;
}
case WM_DESTROY:
{
PostQuitMessage( 0 );
break;
}
default:
return DefWindowProc( hwnd, msg, wparam, lparam );
}
return 0;
}
int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nCmdShow )
{
const char* szWindowClassName = "ILU";
const char* szWindowTitleText = ":O)";
WNDCLASSEXA wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = &WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = (HICON) LoadImageA( NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );
wc.hCursor = (HCURSOR)LoadImageA( NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWindowClassName;
wc.hIconSm = (HICON) LoadImageA( NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );
if (!RegisterClassExA( &wc ))
{
MessageBoxA( NULL, "Fatal error: Window Registration Failed!", szWindowClassName, MB_ICONEXCLAMATION | MB_OK );
return 1;
}
UINT style = WS_OVERLAPPEDWINDOW;
UINT ex_style = WS_EX_CLIENTEDGE;
RECT rect { 0, 0, 400, 300 };
AdjustWindowRectEx( &rect, style, 0, ex_style );
RECT workarea;
SystemParametersInfo( SPI_GETWORKAREA, 0, &workarea, 0 );
for (int n = 0; n < 10; n++)
{
HWND hwnd = CreateWindowExA(
ex_style,
szWindowClassName,
szWindowTitleText,
style,
workarea.left + random( workarea.right - workarea.left - rect.right + rect.left ), // x
workarea.top + random( workarea.bottom - workarea.top - rect.bottom + rect.top ), // y
rect.right - rect.left, // width
rect.bottom - rect.top, // height
NULL,
NULL,
hinstance,
NULL
);
if (!hwnd)
{
MessageBoxA( NULL, "Fatal error: Window Creation Failed!", szWindowClassName, MB_ICONEXCLAMATION | MB_OK );
return 2;
}
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
}
MSG msg;
while (GetMessage( &msg, NULL, 0, 0 ) > 0)
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int)msg.wParam;
}
|