Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <sstream>
- #include <winuser.h>
- LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- HBRUSH createSolidBrushWithColor(int r, int g, int b) {
- return CreateSolidBrush(RGB(r, g, b));
- }
- WNDCLASS initWNDCLASS(HINSTANCE hInstance, int nCmdShow, HBRUSH hbrBackground, char *lpszClassName, WNDPROC wndProc) {
- WNDCLASS wndClass = { 0 };
- wndClass.hInstance = hInstance;
- wndClass.lpszClassName = TEXT(lpszClassName);
- wndClass.lpfnWndProc = wndProc;
- wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndClass.hbrBackground = hbrBackground;
- return wndClass;
- }
- long q = 0;
- struct UserInfo {
- bool isLeft, isTop;
- bool isSetted = false;
- };
- int CALLBACK WinMain(
- _In_ HINSTANCE hInstance,
- _In_ HINSTANCE hPrevInstance,
- _In_ LPSTR lpCmdLine,
- _In_ int nCmdShow
- ) {
- WNDCLASS mainWndClass = initWNDCLASS(hInstance, nCmdShow, createSolidBrushWithColor(155, 89, 182), "KalyuzhinWin32_MainWindow", MainWindowProc);
- if (!RegisterClass(&mainWndClass)) {
- return 0;
- }
- int screenWeight = GetSystemMetrics(SM_CXSCREEN);
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
- int mainWindowWeight = screenWeight / 2;
- int mainWindowHeight = screenHeight / 2;
- HWND mainHWND = CreateWindow(
- mainWndClass.lpszClassName,
- TEXT("Main"),
- WS_OVERLAPPEDWINDOW,
- screenWeight / 2 - mainWindowWeight / 2,
- screenHeight / 2 - mainWindowHeight / 2,
- mainWindowWeight,
- mainWindowHeight,
- NULL,
- NULL,
- hInstance,
- NULL);
- UserInfo userInfo = UserInfo();
- SetWindowLongPtr(mainHWND, GWLP_USERDATA, (LONG) &userInfo);
- ShowWindow(mainHWND, nCmdShow);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- DispatchMessage(&msg);
- }
- return 0;
- }
- void drawWindowsLogo(HDC* hdc, WORD windowHeight, WORD windowWeight, bool isLeft, bool isTop, bool isMouseOutside) {
- RECT rect;
- HBRUSH brush1, brush2, brush3, brush4, brushActive;
- brush1 = createSolidBrushWithColor(231, 76, 60);
- brush2 = createSolidBrushWithColor(41, 128, 185);
- brush3 = createSolidBrushWithColor(46, 204, 113);
- brush4 = createSolidBrushWithColor(241, 196, 15);
- brushActive = createSolidBrushWithColor(255, 255, 255);
- rect.left = 0;
- rect.right = windowWeight / 2;
- rect.top = 0;
- rect.bottom = windowHeight / 2;
- if (isLeft && isTop && !isMouseOutside) {
- FillRect((*hdc), &rect, brushActive);
- }
- else {
- FillRect((*hdc), &rect, brush1);
- }
- rect.left = 0;
- rect.right = windowWeight / 2;
- rect.top = windowHeight / 2;
- rect.bottom = windowHeight;
- if (isLeft && !isTop && !isMouseOutside) {
- FillRect((*hdc), &rect, brushActive);
- }
- else {
- FillRect((*hdc), &rect, brush2);
- }
- rect.left = windowWeight / 2;
- rect.right = windowWeight;
- rect.top = 0;
- rect.bottom = windowHeight / 2;
- if (!isLeft && isTop && !isMouseOutside) {
- FillRect((*hdc), &rect, brushActive);
- }
- else {
- FillRect((*hdc), &rect, brush3);
- }
- rect.left = windowWeight / 2;
- rect.right = windowWeight;
- rect.top = windowHeight / 2;
- rect.bottom = windowHeight;
- if (!isLeft && !isTop && !isMouseOutside) {
- FillRect((*hdc), &rect, brushActive);
- }
- else {
- FillRect((*hdc), &rect, brush4);
- }
- DeleteObject(brush1);
- DeleteObject(brush2);
- DeleteObject(brush3);
- DeleteObject(brush4);
- DeleteObject(brushActive);
- }
- LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg) {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_ERASEBKGND:
- return (LRESULT)1;
- // case WM_SIZE:
- // InvalidateRect(hwnd, 0, true);
- // return 0;
- case WM_PAINT: {
- q++;
- std::stringstream stream;
- stream << q;
- SetWindowText(hwnd, stream.str().c_str());
- PAINTSTRUCT ps;
- POINT pt;
- RECT rc;
- RECT windowRect;
- bool isLeft, isTop;
- GetClientRect(hwnd, &rc);
- GetCursorPos(&pt);
- WORD windowHeight = rc.bottom - rc.top;
- WORD windowWeight = rc.right - rc.left;
- GetWindowRect(hwnd, &windowRect);
- ScreenToClient(hwnd, &pt);
- bool isMouseOutside = !PtInRect(&rc, pt);
- isLeft = pt.x < (windowWeight / 2);
- isTop = pt.y < (windowHeight / 2);
- BeginPaint(hwnd, &ps);
- HDC hdcMem;
- HBITMAP hbmMem, hbmOld;
- hdcMem = CreateCompatibleDC(ps.hdc); // создаем совместимый контекст устройства
- hbmMem = CreateCompatibleBitmap(ps.hdc, windowWeight, windowHeight); // создаем совместимую карту (изображение)
- hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
- drawWindowsLogo(&hdcMem, rc.bottom - rc.top, rc.right - rc.left, isLeft, isTop, isMouseOutside);
- BitBlt(ps.hdc, 0, 0, windowWeight, windowHeight, hdcMem, 0, 0, SRCCOPY);
- SelectObject(hdcMem, hbmOld);
- DeleteObject(hbmMem);
- DeleteDC(hdcMem);
- EndPaint(hwnd, &ps);
- return 0;
- }
- case WM_MOUSELEAVE: {
- LONG_PTR lpUserData = GetWindowLongPtr(hwnd, GWLP_USERDATA);
- UserInfo* pUserInfo = (UserInfo*)lpUserData;
- if (pUserInfo->isSetted) {
- pUserInfo->isSetted = false;
- InvalidateRect(hwnd, 0, true);
- }
- return 0;
- }
- case WM_MOUSEMOVE: {
- LONG_PTR lpUserData = GetWindowLongPtr(hwnd, GWLP_USERDATA);
- UserInfo* pUserInfo = (UserInfo*)lpUserData;
- POINT pt;
- RECT rc;
- RECT windowRect;
- bool isLeft, isTop;
- GetClientRect(hwnd, &rc);
- GetCursorPos(&pt);
- WORD windowHeight = rc.bottom - rc.top;
- WORD windowWeight = rc.right - rc.left;
- GetWindowRect(hwnd, &windowRect);
- ScreenToClient(hwnd, &pt);
- bool isMouseOutside = !PtInRect(&rc, pt);
- isLeft = pt.x < (windowWeight / 2);
- isTop = pt.y < (windowHeight / 2);
- if (!pUserInfo->isSetted) {
- InvalidateRect(hwnd, 0, true);
- }
- if (pUserInfo->isSetted && (pUserInfo->isLeft != isLeft || pUserInfo->isTop != isTop)) {
- InvalidateRect(hwnd, 0, true);
- }
- pUserInfo->isLeft = isLeft;
- pUserInfo->isTop = isTop;
- pUserInfo->isSetted = true;
- return 0;
- }
- default:
- TRACKMOUSEEVENT tme;
- tme.cbSize = sizeof(TRACKMOUSEEVENT);
- tme.dwFlags = TME_LEAVE;
- tme.hwndTrack = hwnd;
- TrackMouseEvent(&tme);
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment