#include using namespace std; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ HDC hdc; HPEN hpen; static int width; // 描画する線の幅 static int speed; // 描画する速度(ms) static int DeviceWidth = 0; // ディスプレイの幅(ピクセル) static int DeviceHeight = 0; // ディスプレイの高さ(ピクセル) static int curx = -1, cury = -1; // デバイスコンテキストのカレントポジション switch(msg){ case WM_DESTROY: PostQuitMessage(0); return 0; case WM_COMMAND: if(LOWORD(wp)==IDOK){ width= IsDlgButtonChecked(hwnd, 11)==BST_CHECKED ? 5 : IsDlgButtonChecked(hwnd, 12)==BST_CHECKED ? 10 : IsDlgButtonChecked(hwnd, 13)==BST_CHECKED ? 15 : IsDlgButtonChecked(hwnd, 14)==BST_CHECKED ? 20 : 0; if(!width) break; speed= IsDlgButtonChecked(hwnd, 15)==BST_CHECKED ? 1000 : IsDlgButtonChecked(hwnd, 16)==BST_CHECKED ? 500 : IsDlgButtonChecked(hwnd, 17)==BST_CHECKED ? 250 : IsDlgButtonChecked(hwnd, 18)==BST_CHECKED ? 125 : 0; if(!speed) break; ShowWindow(hwnd, SW_HIDE); SetTimer(hwnd, 1, speed, NULL); // speed(ms)ごとにメッセージポスト SetTimer(hwnd, 2, 60000, (TIMERPROC)PostQuitMessage); // 60秒後終了 } else if(LOWORD(wp)<15) CheckRadioButton(hwnd, 11, 14, LOWORD(wp)); else if(LOWORD(wp)<19) CheckRadioButton(hwnd, 15, 18, LOWORD(wp)); return 0; case WM_TIMER: hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); if(!DeviceWidth) DeviceWidth = GetDeviceCaps(hdc, HORZRES); if(!DeviceHeight) DeviceHeight = GetDeviceCaps(hdc, VERTRES); if(curx<0){ // いまだ初期状態ならば curx = (int)(DeviceWidth/2); cury = (int)(DeviceHeight/2); } hpen = CreatePen(PS_SOLID, width, RGB(rand()%256,rand()%256,rand()%256)); SelectObject(hdc, hpen); MoveToEx(hdc, curx, cury, NULL); curx = rand()%DeviceWidth; cury = rand()%DeviceHeight; LineTo(hdc, curx, cury); DeleteDC(hdc); DeleteObject(hpen); } return DefWindowProc(hwnd, msg, wp, lp); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { HWND hwnd; WNDCLASS wndc; MSG msg; wndc.style = CS_HREDRAW | CS_VREDRAW; wndc.lpfnWndProc = WndProc; wndc.cbClsExtra = 0; wndc.cbWndExtra = DLGWINDOWEXTRA; wndc.hInstance = hInstance; wndc.hIcon = LoadIcon(NULL, IDI_WINLOGO); wndc.hCursor = LoadCursor(NULL, IDC_ARROW); wndc.hbrBackground = (HBRUSH)CreateSolidBrush(GetSysColor(COLOR_MENU)); wndc.lpszMenuName = NULL; wndc.lpszClassName = TEXT("main"); if(!RegisterClass(&wndc)) return 0; hwnd = CreateDialog(hInstance, TEXT("main"), 0, NULL); if(!hwnd) return 0; while(GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg); return msg.wParam; }