Advertisement
Guest User

Code: DWM Window Frame Conflict

a guest
Apr 21st, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <memory>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <istream>
  9. #include <ostream>
  10. #include <unordered_map>
  11. #include <list>
  12. #include <array>
  13. #include <utility>
  14. #include <type_traits>
  15.  
  16. #include <dwmapi.h>
  17. #include <Uxtheme.h>
  18. #include <vssym32.h>
  19. #include <windowsx.h>
  20.  
  21. #pragma comment(lib, "uxtheme.lib")
  22. #pragma comment(lib, "dwmapi.lib")
  23.  
  24. #include <defs.h>
  25.  
  26. #define FRAMEWIDTH      8
  27. #define CAPTIONHEIGHT   27
  28.  
  29. #define RECTRIGHT(v)  v.right
  30. #define RECTBOTTOM(v) v.bottom
  31. #define RECTLEFT(v)   v.left
  32. #define RECTTOP(v)    v.top
  33. #define RECTSIZE1(v)  (RECTRIGHT(v) - RECTLEFT(v))
  34. #define RECTSIZE2(v)  (RECTBOTTOM(v) - RECTTOP(v))
  35.  
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40.  
  41.     HINSTANCE hDesktopApp;
  42.     TCHAR lpszTitle[] = TEXT("wordproc");
  43.     TCHAR lpszWindowClass[] = TEXT("DesktopApp");
  44.  
  45.     VOID NewDesktopApp(HINSTANCE hInstance);
  46.  
  47. #define SDK_HGDI_SOLID_BRUSH_SIZE   8
  48. #define SDK_HGDI_PEN_SIZE           8
  49.  
  50.     HBRUSH  hFrameBrushPtr[SDK_HGDI_SOLID_BRUSH_SIZE];
  51.     HPEN    hFramePenPtr[SDK_HGDI_PEN_SIZE];
  52.  
  53. #define HWND_HT_FRAME_BA0   HTTOPLEFT
  54. #define HWND_HT_FRAME_BA1   HTLEFT
  55. #define HWND_HT_FRAME_BA2   HTBOTTOMLEFT        
  56. #define HWND_HT_FRAME_BA3   HTBOTTOM        
  57. #define HWND_HT_FRAME_BA4   HTBOTTOMRIGHT  
  58. #define HWND_HT_FRAME_BA5   HTRIGHT    
  59. #define HWND_HT_FRAME_BA6   HTTOPRIGHT  
  60. #define HWND_HT_FRAME_BA7   HTTOP
  61. #define HWND_HT_NULL        HTNOWHERE
  62. #define HWND_HT_FRAME_BA8   HWND_HT_NULL
  63. #define HWND_HT_CAPTION     HTCAPTION
  64. #define HWND_HT_CLIENT      HTCLIENT
  65. #define HWND_HT_MAXBUTTON   HTMINBUTTON
  66. #define HWND_HT_MAXBUTTON   HTMAXBUTTON
  67. #define HWND_HT_CLOSE       HTCLOSE
  68.     static
  69.         LRESULT WINAPI PaintNonClientArea(HWND hWnd, HRGN hNCregion)
  70.     {
  71.         LONG_PTR    lRet = (LONG_PTR)0;
  72.         RECT        rNCRect;
  73.         PAINTSTRUCT ps;
  74.         HDC         hDC;
  75.         HDC         hMemDC;             // <- might go unused, but here in case needed
  76.         HANDLE      oldBrush;
  77.         HANDLE      oldPen;
  78.  
  79.         hDC = BeginPaint(hWnd, &ps);
  80.         FillRect(hDC, &ps.rcPaint, reinterpret_cast<HBRUSH>(hFrameBrushPtr[0]));
  81.  
  82.         BOOL bRet;
  83.         bRet = EndPaint(hWnd, &ps);
  84.         if (bRet == FALSE)
  85.         {
  86.             // error.
  87.         }
  88.  
  89.         return (LONG_PTR)1;
  90.     }
  91.  
  92.     static
  93.         LRESULT SetHWNDInfoForDWM(HWND hWnd)
  94.     {
  95.         const MARGINS margins =
  96.         {
  97.             8,
  98.             8,
  99.             28,
  100.             8
  101.         };
  102.  
  103.         HRESULT hr = DwmExtendFrameIntoClientArea(hWnd, &margins);
  104.         if (!SUCCEEDED(hr))
  105.         {
  106.             return (LONG_PTR)0;
  107.         }
  108.  
  109.         DWMNCRENDERINGPOLICY ncrp = DWMNCRP_ENABLED;
  110.         hr = DwmSetWindowAttribute(hWnd, DWMWA_NCRENDERING_POLICY, (void*)&ncrp, sizeof(DWMNCRENDERINGPOLICY));
  111.         if (!SUCCEEDED(hr))
  112.         {
  113.             return (LONG_PTR)0;
  114.         }
  115.  
  116.         BOOL bDrawNC = TRUE;
  117.         hr = DwmSetWindowAttribute(hWnd, DWMWA_ALLOW_NCPAINT, (void*)&bDrawNC, sizeof(BOOL));
  118.         if (!SUCCEEDED(hr))
  119.         {
  120.             return (LONG_PTR)0;
  121.         }
  122.  
  123.         return (LONG_PTR)1;
  124.     }
  125.  
  126.     static
  127.         LRESULT HitTest(HWND hWnd, WPARAM wParam, LPARAM lParam)
  128.     {
  129.         bool fOnResizeBorder = false;
  130.         BOOL bRet;
  131.         LONG_PTR lRet = (LONG_PTR)0;
  132.  
  133.         RECT rcWindow =
  134.         {
  135.             0,
  136.             0,
  137.             0,
  138.             0
  139.         };
  140.         bRet = GetWindowRect(hWnd, &rcWindow);
  141.         if (bRet == FALSE)
  142.         {
  143.         }
  144.  
  145.         DWORD dwExStyle = (DWORD)(WS_EX_LAYERED);
  146.         DWORD dwStyle = ((DWORD)(WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_DLGFRAME | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS));
  147.  
  148.         RECT rcNCregion =
  149.         {
  150.             0,
  151.             0,
  152.             0,
  153.             0
  154.         };
  155.         bRet = AdjustWindowRectEx(&rcNCregion, dwStyle & ~WS_CAPTION, FALSE, dwExStyle);
  156.         if (bRet == FALSE)
  157.         {
  158.         }
  159.  
  160.         INT dwRow = 1;
  161.         INT dwCol = 1;
  162.  
  163.         POINT pt;
  164.         pt.x = LOWORD(lParam);
  165.         pt.y = HIWORD(lParam);
  166.  
  167.         if (pt.y >= rcWindow.top && pt.y < rcWindow.top + CAPTIONHEIGHT)
  168.         {
  169.             if (pt.y < (rcWindow.top - rcNCregion.top))
  170.             {
  171.                 dwRow = 0;
  172.             }
  173.             else
  174.             {
  175.                 return HTCAPTION;
  176.             }
  177.         }
  178.  
  179.         if (pt.y < rcWindow.bottom && pt.y >= rcWindow.bottom - FRAMEWIDTH)
  180.         {
  181.             dwRow = 2;
  182.         }
  183.  
  184.         if (pt.x >= rcWindow.left && pt.x < rcWindow.left + FRAMEWIDTH)
  185.         {
  186.             dwCol = 0;
  187.         }
  188.  
  189.         if (pt.x < rcWindow.right && pt.x >= rcWindow.right - FRAMEWIDTH)
  190.         {
  191.             dwCol = 2;
  192.         }
  193.  
  194.         static LONG_PTR larrHitTest[3][3] =
  195.         {
  196.             { HWND_HT_FRAME_BA0, HWND_HT_FRAME_BA7, HWND_HT_FRAME_BA6 },
  197.             { HWND_HT_FRAME_BA1, HWND_HT_FRAME_BA8, HWND_HT_FRAME_BA5 },
  198.             { HWND_HT_FRAME_BA2, HWND_HT_FRAME_BA3, HWND_HT_FRAME_BA4 }
  199.         };
  200.  
  201.         return larrHitTest[dwRow][dwCol];
  202.     }
  203.  
  204.     static
  205.         LRESULT WINAPI RecalcNonClientArea(LPARAM lParam)
  206.     {
  207.         LONG_PTR            lRet = (LONG_PTR)0;
  208.         NCCALCSIZE_PARAMS* lpParams = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
  209.  
  210.         RECTLEFT(lpParams->rgrc[0]) = RECTLEFT(lpParams->rgrc[0]) + 0;
  211.         RECTTOP(lpParams->rgrc[0]) = RECTTOP(lpParams->rgrc[0]) + 0;
  212.         RECTBOTTOM(lpParams->rgrc[0]) = RECTBOTTOM(lpParams->rgrc[0]) - 0;
  213.         RECTRIGHT(lpParams->rgrc[0]) = RECTRIGHT(lpParams->rgrc[0]) - 0;
  214.  
  215.         lRet = ((LONG_PTR)(WVR_VALIDRECTS));
  216.  
  217.         return lRet;
  218.     }
  219.  
  220.     BOOL WINAPI InitializeResources(VOID)
  221.     {
  222.         BOOL bRet = FALSE;
  223.  
  224.         // initialize solid-color paint brushes
  225.         hFrameBrushPtr[0] = reinterpret_cast<HBRUSH>(CreateSolidBrush(RGB(52, 52, 52)));  // color: Non-client Window Frame Border
  226.         hFrameBrushPtr[1] = reinterpret_cast<HBRUSH>(CreateSolidBrush(RGB(203, 94, 72)));  // color: HTCLOSE color
  227.         hFrameBrushPtr[2] = reinterpret_cast<HBRUSH>(CreateSolidBrush(RGB(68, 68, 68)));  // color: HTMINBUTTON and HTMAXBUTTON
  228.  
  229.         // initialize solid-color pens
  230.         hFramePenPtr[0] = reinterpret_cast<HPEN>(CreatePen(PS_SOLID, 1, RGB(47, 47, 47)));
  231.         hFramePenPtr[1] = reinterpret_cast<HPEN>(CreatePen(PS_SOLID, 1, RGB(286, 186, 186)));
  232.         hFramePenPtr[2] = reinterpret_cast<HPEN>(CreatePen(PS_SOLID, 1, RGB(145, 145, 145)));
  233.         hFramePenPtr[3] = reinterpret_cast<HPEN>(CreatePen(PS_SOLID, 1, RGB(42, 42, 42)));
  234.  
  235.         return bRet;
  236.     }
  237.  
  238.     LRESULT CALLBACK DwmWndProc(bool* pfCallDWP, HWND message, UINT uMsg, WPARAM wParam, LPARAM lParam);
  239.     LRESULT CALLBACK AppWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  240.  
  241.     LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  242.     {
  243.         LONG_PTR lRet = (LONG_PTR)0;
  244.         bool fCallDWP = true;
  245.         BOOL fDwmEnabled = FALSE;
  246.  
  247.         HRESULT hr = DwmIsCompositionEnabled(&fDwmEnabled);
  248.         if (!SUCCEEDED(hr))
  249.         {
  250.             MessageBox
  251.             (
  252.                 nullptr,
  253.                 __TEXT
  254.                 (
  255.  
  256.                     "Call to function \"HRESULT DwmIsCompositionEnabled\" returned an unexpected error."
  257.                     "\n\n"
  258.                     "Resolve: The Desktop Window Manager (DWM) is required for this application. Please ensure that composition is enabled on your operating"
  259.                     "system, and try again."
  260.                 ),
  261.                 __TEXT
  262.                 (
  263.                     "Desktop Window Manager"
  264.                 ),
  265.                 MB_OK
  266.             );
  267.             abort();
  268.         }
  269.         else
  270.         {
  271.             lRet = DwmWndProc(&fCallDWP, hWnd, message, wParam, lParam);
  272.         }
  273.  
  274.         if (fCallDWP)
  275.         {
  276.             lRet = AppWndProc(hWnd, message, wParam, lParam);
  277.         }
  278.  
  279.         return lRet;
  280.     }
  281.  
  282.     LRESULT CALLBACK DwmWndProc(bool* pfCallDWP, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  283.     {
  284.         bool fCallDWP = true;
  285.         LONG_PTR lRet = (LONG_PTR)0;
  286.  
  287.         /*++
  288.  
  289.  
  290.  
  291.         --*/
  292.         if (message == WM_DESTROY)
  293.         {
  294.             PostQuitMessage(EXIT_SUCCESS);
  295.             fCallDWP = true;
  296.         }
  297.  
  298.         /*++
  299.  
  300.  
  301.  
  302.         --*/
  303.         if (message == WM_CLOSE)
  304.         {
  305.             unsigned int i;
  306.             for (i = 0; i < (unsigned int)SDK_HGDI_SOLID_BRUSH_SIZE; i++)
  307.             {
  308.                 DeleteBrush(hFrameBrushPtr[(unsigned int)i]);
  309.                 DeletePen(hFramePenPtr[(unsigned int)i]);
  310.             }
  311.  
  312.             DestroyWindow(hWnd);
  313.             fCallDWP = true;
  314.         }
  315.  
  316.         /*++
  317.  
  318.  
  319.  
  320.         --*/
  321.         if ((message == WM_NCHITTEST) && (lRet == 0))
  322.         {
  323.             lRet = (LONG_PTR)HitTest(hWnd, wParam, lParam);
  324.             if (lRet == HTNOWHERE)
  325.             {
  326.                 fCallDWP = true;
  327.             }
  328.             else
  329.             {
  330.                 fCallDWP = false;
  331.             }
  332.         }
  333.  
  334.         /*++
  335.  
  336.  
  337.  
  338.         --*/
  339.         if ((message == WM_NCCALCSIZE) && (wParam == TRUE))
  340.         {
  341.             lRet = (LONG_PTR)RecalcNonClientArea(lParam);
  342.             fCallDWP = false;
  343.         }
  344.  
  345.         /*++
  346.  
  347.  
  348.  
  349.         --*/
  350.         if (message == WM_CREATE)
  351.         {
  352.             lRet = SetHWNDInfoForDWM(hWnd);
  353.  
  354.             BOOL bRet;
  355.             bRet = InitializeResources();
  356.             if (bRet == FALSE)
  357.             {
  358.             }
  359.  
  360.             fCallDWP = true;
  361.         }
  362.  
  363.         /*++
  364.  
  365.  
  366.  
  367.         --*/
  368.         if (message == WM_ACTIVATE)
  369.         {
  370.             SetWindowPos(hWnd, nullptr, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
  371.             fCallDWP = true;
  372.         }
  373.  
  374.         /*++
  375.  
  376.  
  377.  
  378.         --*/
  379.         if (message == WM_PAINT)
  380.         {
  381.             lRet = (LONG_PTR)PaintNonClientArea(hWnd, (HRGN)wParam);
  382.             fCallDWP = true;
  383.         }
  384.  
  385.         *pfCallDWP = fCallDWP;
  386.  
  387.         return lRet;
  388.     }
  389.  
  390.     LRESULT CALLBACK AppWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  391.     {
  392.         LONG_PTR lRet = (LONG_PTR)0;
  393.  
  394.         switch (message)
  395.         {
  396.         case WM_CREATE:
  397.         {
  398.             break;
  399.         }
  400.         case WM_COMMAND:
  401.         {
  402.             break;
  403.         }
  404.         case WM_PAINT:
  405.         {
  406.             break;
  407.         }
  408.         default:
  409.             return DefWindowProc(hWnd, message, wParam, lParam);
  410.         }
  411.  
  412.         return lRet;
  413.     }
  414.  
  415.     extern
  416.         INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
  417.     {
  418.         try
  419.         {
  420.             NewDesktopApp(hInstance);
  421.         }
  422.         catch (std::exception& const e)
  423.         {
  424.             abort();
  425.         }
  426.  
  427.         static MSG msg;
  428.  
  429.         BOOL bRet;
  430.         while ((bRet = GetMessage(&msg, nullptr, 0, 0)) == TRUE)
  431.         {
  432.             if (bRet == FALSE)
  433.             {
  434.                 // error.
  435.             }
  436.             else
  437.             {
  438.                 TranslateMessage
  439.                 (
  440.                     &msg
  441.                 );
  442.                 DispatchMessage
  443.                 (
  444.                     &msg
  445.                 );
  446.             }
  447.         }
  448.  
  449.         return (INT)msg.wParam;
  450.     }
  451.  
  452.     static
  453.         ATOM MyRegisterClass(HINSTANCE hInstance)
  454.     {
  455.         WNDCLASSEX wcex;
  456.         ZeroMemory(&wcex, sizeof(WNDCLASSEX));
  457.  
  458.         wcex.cbSize = sizeof(WNDCLASSEX);
  459.         wcex.hbrBackground = NULL;
  460.         wcex.cbClsExtra = 0;
  461.         wcex.cbWndExtra = 0;
  462.         wcex.style = 0;
  463.         wcex.hInstance = hInstance;
  464.         wcex.lpfnWndProc = WndProc;
  465.         wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  466.         wcex.hCursor = LoadCursor(wcex.hInstance, IDC_ARROW);
  467.         wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  468.         wcex.lpszClassName = lpszWindowClass;
  469.         wcex.lpszMenuName = NULL;
  470.  
  471.         return RegisterClassEx(&wcex);
  472.     }
  473.  
  474.     static
  475.         VOID NewDesktopApp(HINSTANCE hInstance)
  476.     {
  477.         hDesktopApp = hInstance;
  478.         if (MyRegisterClass(hDesktopApp) == (ATOM)0)
  479.         {
  480.             MessageBox(nullptr, __TEXT("Failed to register window class \"DesktopApp\"!"), __TEXT("wordproc"), MB_OK);
  481.             throw std::exception
  482.             (
  483.             );
  484.         }
  485.  
  486.         DWORD dwExStyle = (DWORD)(WS_EX_LAYERED);
  487.         DWORD dwStyle = ((DWORD)(WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_DLGFRAME | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS));
  488.         RECT r =
  489.         {
  490.             0,
  491.             0,
  492.             1480,
  493.             680
  494.         };
  495.         AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
  496.         HWND hWnd = CreateWindowEx
  497.         (
  498.             dwExStyle,
  499.             lpszWindowClass,
  500.             lpszTitle,
  501.             dwStyle,
  502.             CW_USEDEFAULT,
  503.             CW_USEDEFAULT,
  504.             CW_USEDEFAULT,
  505.             CW_USEDEFAULT,
  506.             //((INT)RECTSIZE1(r)),
  507.             //((INT)RECTSIZE2(r)),
  508.             nullptr,
  509.             nullptr,
  510.             hDesktopApp,
  511.             nullptr
  512.         );
  513.         if (!hWnd)
  514.         {
  515.             MessageBox(nullptr, __TEXT("Failed to create window of class type \"DesktopApp\"!"), __TEXT("wordproc"), MB_OK);
  516.             throw std::exception
  517.             (
  518.             );
  519.         }
  520.  
  521.         SetLayeredWindowAttributes(hWnd, NULL, 255, LWA_ALPHA);
  522.         //UpdateLayeredWindow()
  523.  
  524.         ShowWindow(hWnd, SW_NORMAL);
  525.         UpdateWindow(hWnd);
  526.     }
  527.  
  528. #ifdef __cplusplus
  529. }
  530. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement