peterzig

[PIU] Click Box

Nov 19th, 2016
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <windows.h>
  2. #define DIVISIONS 5
  3.  
  4. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  5.  
  6. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  7. {
  8.     static TCHAR szAppName[] = TEXT("Click Check");
  9.     HWND hwnd;
  10.     MSG msg;
  11.     WNDCLASS wndclass;
  12.  
  13.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  14.     wndclass.lpfnWndProc = WndProc;
  15.     wndclass.cbClsExtra = 0;
  16.     wndclass.cbWndExtra = 0;
  17.     wndclass.hInstance = hInstance;
  18.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  19.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  20.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  21.     wndclass.lpszMenuName = NULL;
  22.     wndclass.lpszClassName = szAppName;
  23.  
  24.     if (!RegisterClass(&wndclass))
  25.     {
  26.         MessageBox(NULL, TEXT("Nie udaΕ‚o siΔ™ zarejestrowaΔ‡ klasy okna!"), szAppName, MB_ICONERROR);
  27.         return 0;
  28.     }
  29.  
  30.     hwnd = CreateWindow(szAppName, TEXT("Click Box"),
  31.         WS_OVERLAPPEDWINDOW,
  32.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  33.         NULL, NULL, hInstance, NULL);
  34.  
  35.     ShowWindow(hwnd, iCmdShow);
  36.     UpdateWindow(hwnd);
  37.  
  38.     while (GetMessage(&msg, NULL, 0, 0))
  39.     {
  40.         TranslateMessage(&msg);
  41.         DispatchMessage(&msg);
  42.     }
  43.     return msg.wParam;
  44. }
  45.  
  46. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  47. {
  48.     static BOOL fState[DIVISIONS][DIVISIONS];
  49.     static int cxBlock, cyBlock;
  50.     HDC hdc;
  51.     int x, y;
  52.     PAINTSTRUCT ps;
  53.     RECT rect;
  54.  
  55.     switch (msg)
  56.     {
  57.     case WM_SIZE:
  58.         cxBlock = LOWORD(lParam) / DIVISIONS;
  59.         cyBlock = HIWORD(lParam) / DIVISIONS;
  60.         return 0;
  61.  
  62.     case WM_LBUTTONDOWN:
  63.         x = LOWORD(lParam) / cxBlock;
  64.         y = HIWORD(lParam) / cyBlock;
  65.  
  66.         if (x < DIVISIONS && y < DIVISIONS)
  67.         {
  68.             fState[x][y] ^= 1;
  69.  
  70.             rect.left = x * cxBlock;
  71.             rect.top = y * cyBlock;
  72.             rect.right = (x + 1) * cxBlock;
  73.             rect.bottom = (y + 1) * cyBlock;
  74.  
  75.             InvalidateRect(hwnd, &rect, FALSE);
  76.         }
  77.         else
  78.             MessageBeep(0);
  79.         return 0;
  80.  
  81.     case WM_PAINT:
  82.         hdc = BeginPaint(hwnd, &ps);
  83.  
  84.         for (x = 0; x < DIVISIONS; x++)
  85.             for (y = 0; y < DIVISIONS; y++)
  86.             {
  87.                 Rectangle(hdc, x * cxBlock, y * cyBlock, (x + 1) * cxBlock, (y + 1) * cyBlock);
  88.  
  89.                 if (fState[x][y])
  90.                 {
  91.                     MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL);
  92.                     LineTo(hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
  93.                     MoveToEx(hdc, x * cxBlock, (y + 1) * cyBlock, NULL);
  94.                     LineTo(hdc, (x + 1) * cxBlock, y * cyBlock);
  95.                 }
  96.             }
  97.         EndPaint(hwnd, &ps);
  98.         return 0;
  99.  
  100.     case WM_DESTROY:
  101.         PostQuitMessage(0);
  102.         return 0;
  103.     }
  104.     return DefWindowProc(hwnd, msg, wParam, lParam);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment