Guest User

Untitled

a guest
Sep 21st, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <sstream>
  3. #include <winuser.h>
  4.  
  5. LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  6.  
  7. HBRUSH createSolidBrushWithColor(int r, int g, int b) {
  8.     return CreateSolidBrush(RGB(r, g, b));
  9. }
  10.  
  11. WNDCLASS initWNDCLASS(HINSTANCE hInstance, int nCmdShow, HBRUSH hbrBackground, char *lpszClassName, WNDPROC wndProc) {
  12.     WNDCLASS wndClass = { 0 };
  13.     wndClass.hInstance = hInstance;
  14.     wndClass.lpszClassName = TEXT(lpszClassName);
  15.     wndClass.lpfnWndProc = wndProc;
  16.     wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  17.     wndClass.hbrBackground = hbrBackground;
  18.     return wndClass;
  19. }
  20.  
  21. long q = 0;
  22.  
  23. struct UserInfo {
  24.     bool isLeft, isTop;
  25.     bool isSetted = false; 
  26. };
  27.  
  28. int CALLBACK WinMain(
  29.     _In_ HINSTANCE hInstance,
  30.     _In_ HINSTANCE hPrevInstance,
  31.     _In_ LPSTR lpCmdLine,
  32.     _In_ int nCmdShow
  33. ) {
  34.     WNDCLASS mainWndClass = initWNDCLASS(hInstance, nCmdShow, createSolidBrushWithColor(155, 89, 182), "KalyuzhinWin32_MainWindow", MainWindowProc);
  35.     if (!RegisterClass(&mainWndClass)) {
  36.         return 0;
  37.     }
  38.     int screenWeight = GetSystemMetrics(SM_CXSCREEN);
  39.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  40.     int mainWindowWeight = screenWeight / 2;
  41.     int mainWindowHeight = screenHeight / 2;
  42.  
  43.     HWND mainHWND = CreateWindow(
  44.         mainWndClass.lpszClassName,
  45.         TEXT("Main"),
  46.         WS_OVERLAPPEDWINDOW,
  47.         screenWeight / 2 - mainWindowWeight / 2,
  48.         screenHeight / 2 - mainWindowHeight / 2,
  49.         mainWindowWeight,
  50.         mainWindowHeight,
  51.         NULL,
  52.         NULL,
  53.         hInstance,
  54.         NULL);
  55.     UserInfo userInfo = UserInfo();
  56.     SetWindowLongPtr(mainHWND, GWLP_USERDATA, (LONG) &userInfo);
  57.  
  58.     ShowWindow(mainHWND, nCmdShow);
  59.     MSG msg;
  60.     while (GetMessage(&msg, NULL, 0, 0)) {
  61.         DispatchMessage(&msg);
  62.     }
  63.     return 0;
  64. }
  65.  
  66. void drawWindowsLogo(HDC* hdc, WORD windowHeight, WORD windowWeight, bool isLeft, bool isTop, bool isMouseOutside) {
  67.     RECT rect;
  68.     HBRUSH brush1, brush2, brush3, brush4, brushActive;
  69.     brush1 = createSolidBrushWithColor(231, 76, 60);
  70.     brush2 = createSolidBrushWithColor(41, 128, 185);
  71.     brush3 = createSolidBrushWithColor(46, 204, 113);
  72.     brush4 = createSolidBrushWithColor(241, 196, 15);
  73.     brushActive = createSolidBrushWithColor(255, 255, 255);
  74.     rect.left = 0;
  75.     rect.right = windowWeight / 2;
  76.     rect.top = 0;
  77.     rect.bottom = windowHeight / 2;
  78.     if (isLeft && isTop && !isMouseOutside) {
  79.         FillRect((*hdc), &rect, brushActive);
  80.     }
  81.     else {
  82.         FillRect((*hdc), &rect, brush1);
  83.     }
  84.     rect.left = 0;
  85.     rect.right = windowWeight / 2;
  86.     rect.top = windowHeight / 2;
  87.     rect.bottom = windowHeight;
  88.     if (isLeft && !isTop && !isMouseOutside) {
  89.         FillRect((*hdc), &rect, brushActive);
  90.     }
  91.     else {
  92.         FillRect((*hdc), &rect, brush2);
  93.     }
  94.     rect.left = windowWeight / 2;
  95.     rect.right = windowWeight;
  96.     rect.top = 0;
  97.     rect.bottom = windowHeight / 2;
  98.     if (!isLeft && isTop && !isMouseOutside) {
  99.         FillRect((*hdc), &rect, brushActive);
  100.     }
  101.     else {
  102.         FillRect((*hdc), &rect, brush3);
  103.     }
  104.     rect.left = windowWeight / 2;
  105.     rect.right = windowWeight;
  106.     rect.top = windowHeight / 2;
  107.     rect.bottom = windowHeight;
  108.     if (!isLeft && !isTop && !isMouseOutside) {
  109.         FillRect((*hdc), &rect, brushActive);
  110.     }
  111.     else {
  112.         FillRect((*hdc), &rect, brush4);
  113.     }
  114.     DeleteObject(brush1);
  115.     DeleteObject(brush2);
  116.     DeleteObject(brush3);
  117.     DeleteObject(brush4);
  118.     DeleteObject(brushActive);
  119. }
  120.  
  121. LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  122. {
  123.     switch (msg) {
  124.     case WM_DESTROY:
  125.         PostQuitMessage(0);
  126.         break;
  127.     case WM_ERASEBKGND:
  128.         return (LRESULT)1;
  129. //  case WM_SIZE:
  130. //      InvalidateRect(hwnd, 0, true);
  131. //      return 0;
  132.     case WM_PAINT: {
  133.         q++;
  134.         std::stringstream stream;
  135.         stream << q;
  136.         SetWindowText(hwnd, stream.str().c_str());
  137.         PAINTSTRUCT ps;
  138.         POINT pt;
  139.         RECT rc;
  140.         RECT windowRect;
  141.         bool isLeft, isTop;
  142.         GetClientRect(hwnd, &rc);
  143.         GetCursorPos(&pt);
  144.         WORD windowHeight = rc.bottom - rc.top;
  145.         WORD windowWeight = rc.right - rc.left;
  146.         GetWindowRect(hwnd, &windowRect);
  147.         ScreenToClient(hwnd, &pt);
  148.         bool isMouseOutside = !PtInRect(&rc, pt);
  149.  
  150.         isLeft = pt.x < (windowWeight / 2);
  151.         isTop = pt.y < (windowHeight / 2);
  152.  
  153.         BeginPaint(hwnd, &ps);
  154.         HDC hdcMem;
  155.         HBITMAP hbmMem, hbmOld;
  156.         hdcMem = CreateCompatibleDC(ps.hdc); //  создаем совместимый контекст устройства
  157.         hbmMem = CreateCompatibleBitmap(ps.hdc, windowWeight, windowHeight); // создаем совместимую карту (изображение)
  158.         hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
  159.         drawWindowsLogo(&hdcMem, rc.bottom - rc.top, rc.right - rc.left, isLeft, isTop, isMouseOutside);
  160.         BitBlt(ps.hdc, 0, 0, windowWeight, windowHeight, hdcMem, 0, 0, SRCCOPY);
  161.         SelectObject(hdcMem, hbmOld);
  162.         DeleteObject(hbmMem);
  163.         DeleteDC(hdcMem);
  164.         EndPaint(hwnd, &ps);
  165.         return 0;
  166.     }
  167.     case WM_MOUSELEAVE: {
  168.         LONG_PTR lpUserData = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  169.         UserInfo* pUserInfo = (UserInfo*)lpUserData;
  170.         if (pUserInfo->isSetted) {
  171.             pUserInfo->isSetted = false;
  172.             InvalidateRect(hwnd, 0, true);
  173.         }
  174.         return 0;
  175.     }
  176.     case WM_MOUSEMOVE: {
  177.         LONG_PTR lpUserData = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  178.         UserInfo* pUserInfo = (UserInfo*)lpUserData;
  179.         POINT pt;
  180.         RECT rc;
  181.         RECT windowRect;
  182.         bool isLeft, isTop;
  183.  
  184.         GetClientRect(hwnd, &rc);
  185.         GetCursorPos(&pt);
  186.         WORD windowHeight = rc.bottom - rc.top;
  187.         WORD windowWeight = rc.right - rc.left;
  188.         GetWindowRect(hwnd, &windowRect);
  189.         ScreenToClient(hwnd, &pt);
  190.         bool isMouseOutside = !PtInRect(&rc, pt);
  191.        
  192.         isLeft = pt.x < (windowWeight / 2);
  193.         isTop = pt.y < (windowHeight / 2);
  194.  
  195.         if (!pUserInfo->isSetted) {
  196.             InvalidateRect(hwnd, 0, true);
  197.         }
  198.         if (pUserInfo->isSetted && (pUserInfo->isLeft != isLeft || pUserInfo->isTop != isTop)) {
  199.             InvalidateRect(hwnd, 0, true);
  200.         }
  201.         pUserInfo->isLeft = isLeft;
  202.         pUserInfo->isTop = isTop;
  203.         pUserInfo->isSetted = true;
  204.         return 0;
  205.     }
  206.  
  207.     default:
  208.         TRACKMOUSEEVENT tme;
  209.         tme.cbSize = sizeof(TRACKMOUSEEVENT);
  210.         tme.dwFlags = TME_LEAVE;
  211.         tme.hwndTrack = hwnd;
  212.  
  213.         TrackMouseEvent(&tme);
  214.         return DefWindowProc(hwnd, msg, wParam, lParam);
  215.     }
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment