#include "StdAfx.h" #include "UIShadow.h" #include "math.h" #include "crtdbg.h" namespace DuiLib { const TCHAR *strWndClassName = _T("PerryShadowWnd"); bool CShadowUI::s_bHasInit = FALSE; CShadowUI::CShadowUI(void) : m_hWnd((HWND)NULL) , m_OriParentProc(NULL) , m_Status(0) , m_nDarkness(150) , m_nSharpness(5) , m_nSize(0) , m_nxOffset(0) , m_nyOffset(0) , m_Color(RGB(0, 0, 0)) , m_WndSize(0) , m_bUpdate(false) , m_bIsImageMode(false) , m_bIsShowShadow(false) , m_bIsDisableShadow(false) { ::ZeroMemory(&m_rcShadowCorner, sizeof(RECT)); } CShadowUI::~CShadowUI(void) { } bool CShadowUI::Initialize(HINSTANCE hInstance) { if (s_bHasInit) return false; // Register window class for shadow window WNDCLASSEX wcex; memset(&wcex, 0, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = DefWindowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = strWndClassName; wcex.hIconSm = NULL; RegisterClassEx(&wcex); s_bHasInit = true; return true; } void CShadowUI::Create(CPaintManagerUI* pPaintManager) { if(!m_bIsShowShadow) return; // Already initialized _ASSERT(CPaintManagerUI::GetInstance() != INVALID_HANDLE_VALUE); _ASSERT(pPaintManager != NULL); m_pManager = pPaintManager; HWND hParentWnd = m_pManager->GetPaintWindow(); // Add parent window - shadow pair to the map _ASSERT(GetShadowMap().find(hParentWnd) == GetShadowMap().end()); // Only one shadow for each window GetShadowMap()[hParentWnd] = this; // Determine the initial show state of shadow according to parent window's state LONG lParentStyle = GetWindowLongPtr(hParentWnd, GWL_STYLE); // Create the shadow window LONG styleValue = lParentStyle & WS_CAPTION; m_hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, strWndClassName, NULL, /*WS_VISIBLE | */styleValue | WS_POPUPWINDOW, CW_USEDEFAULT, 0, 0, 0, hParentWnd, NULL, CPaintManagerUI::GetInstance(), NULL); if(!(WS_VISIBLE & lParentStyle)) // Parent invisible m_Status = SS_ENABLED; else if((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow m_Status = SS_ENABLED | SS_PARENTVISIBLE; else // Show the shadow { m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE; ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE); Update(hParentWnd); } // Replace the original WndProc of parent window to steal messages m_OriParentProc = GetWindowLongPtr(hParentWnd, GWLP_WNDPROC); #pragma warning(disable: 4311) // temporrarily disable the type_cast warning in Win32 SetWindowLongPtr(hParentWnd, GWLP_WNDPROC, (LONG_PTR)ParentProc); #pragma warning(default: 4311) } std::map& CShadowUI::GetShadowMap() { static std::map s_Shadowmap; return s_Shadowmap; } LRESULT CALLBACK CShadowUI::ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { _ASSERT(GetShadowMap().find(hwnd) != GetShadowMap().end()); // Shadow must have been attached CShadowUI *pThis = GetShadowMap()[hwnd]; if (pThis->IsDisableShadow()) { return CallWindowProc((WNDPROC)pThis->m_OriParentProc, hwnd, uMsg, wParam, lParam); } switch(uMsg) { case WM_ACTIVATEAPP: { if(!IsWindowEnabled(hwnd)) break; ::SetWindowPos(pThis->m_hWnd, hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW); break; } case WM_NCACTIVATE: { if(!IsWindowEnabled(hwnd)) break; ::SetWindowPos(pThis->m_hWnd, hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW); break; } case WM_WINDOWPOSCHANGED: { RECT WndRect; LPWINDOWPOS pWndPos; pWndPos = (LPWINDOWPOS)lParam; GetWindowRect(hwnd, &WndRect); if (pThis->m_bIsImageMode) { SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left - pThis->m_nSize, WndRect.top - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } else { SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left + pThis->m_nxOffset - pThis->m_nSize, WndRect.top + pThis->m_nyOffset - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } if (pWndPos->flags & SWP_SHOWWINDOW) { if (pThis->m_Status & SS_ENABLED && !(pThis->m_Status & SS_PARENTVISIBLE)) { pThis->m_bUpdate = true; ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE); pThis->m_Status |= SS_VISABLE | SS_PARENTVISIBLE; } } else if (pWndPos->flags & SWP_HIDEWINDOW) { if (pThis->m_Status & SS_ENABLED) { ::ShowWindow(pThis->m_hWnd, SW_HIDE); pThis->m_Status &= ~(SS_VISABLE | SS_PARENTVISIBLE); } } break; } case WM_MOVE: if(pThis->m_Status & SS_VISABLE) { RECT WndRect; GetWindowRect(hwnd, &WndRect); if (pThis->m_bIsImageMode) { SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left - pThis->m_nSize, WndRect.top - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } else { SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left + pThis->m_nxOffset - pThis->m_nSize, WndRect.top + pThis->m_nyOffset - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } } break; case WM_SIZE: if(pThis->m_Status & SS_ENABLED) { if(SIZE_MAXIMIZED == wParam || SIZE_MINIMIZED == wParam) { ::ShowWindow(pThis->m_hWnd, SW_HIDE); pThis->m_Status &= ~SS_VISABLE; } // Parent maybe resized even if invisible else if(pThis->m_Status & SS_PARENTVISIBLE) { // Awful! It seems that if the window size was not decreased // the window region would never be updated until WM_PAINT was sent. // So do not Update() until next WM_PAINT is received in this case if(LOWORD(lParam) > LOWORD(pThis->m_WndSize) || HIWORD(lParam) > HIWORD(pThis->m_WndSize)) pThis->m_bUpdate = true; else pThis->Update(hwnd); if(!(pThis->m_Status & SS_VISABLE)) { ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE); pThis->m_Status |= SS_VISABLE; } } pThis->m_WndSize = lParam; } break; case WM_PAINT: { if(pThis->m_bUpdate) { pThis->Update(hwnd); pThis->m_bUpdate = false; } break; } // In some cases of sizing, the up-right corner of the parent window region would not be properly updated // Update() again when sizing is finished case WM_EXITSIZEMOVE: if(pThis->m_Status & SS_VISABLE) { pThis->Update(hwnd); } break; case WM_SHOWWINDOW: if(pThis->m_Status & SS_ENABLED) { // the window is being hidden if(!wParam) { ::ShowWindow(pThis->m_hWnd, SW_HIDE); pThis->m_Status &= ~(SS_VISABLE | SS_PARENTVISIBLE); } else if(!(pThis->m_Status & SS_PARENTVISIBLE)) { //pThis->Update(hwnd); pThis->m_bUpdate = true; ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE); pThis->m_Status |= SS_VISABLE | SS_PARENTVISIBLE; } } break; case WM_DESTROY: DestroyWindow(pThis->m_hWnd); // Destroy the shadow break; case WM_NCDESTROY: GetShadowMap().erase(hwnd); // Remove this window and shadow from the map break; } return CallWindowProc((WNDPROC)pThis->m_OriParentProc, hwnd, uMsg, wParam, lParam); } //Formats GetLastError() value. void GetLastErrorMessage() { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR)&lpMsgBuf, 0, NULL ); // Display the string. //MessageBox(NULL, (const wchar_t*)lpMsgBuf, L"GetLastError", MB_OK | MB_ICONINFORMATION); // Free the buffer. LocalFree(lpMsgBuf); } void CShadowUI::Update(HWND hParent) { if(!m_bIsShowShadow || !(m_Status & SS_VISABLE)) return; RECT WndRect; GetWindowRect(hParent, &WndRect); int nShadWndWid; int nShadWndHei; if (m_bIsImageMode) { if(m_sShadowImage.IsEmpty()) return; nShadWndWid = WndRect.right - WndRect.left + m_nSize * 2; nShadWndHei = WndRect.bottom - WndRect.top + m_nSize * 2; } else { if (m_nSize == 0) return; nShadWndWid = WndRect.right - WndRect.left + m_nSize * 2; nShadWndHei = WndRect.bottom - WndRect.top + m_nSize * 2; } // Create the alpha blending bitmap BITMAPINFO bmi; // bitmap header ZeroMemory(&bmi, sizeof(BITMAPINFO)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = nShadWndWid; bmi.bmiHeader.biHeight = nShadWndHei; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; // four 8-bit components bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biSizeImage = nShadWndWid * nShadWndHei * 4; BYTE *pvBits; // pointer to DIB section HBITMAP hbitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pvBits, NULL, 0); if (hbitmap == NULL) { GetLastErrorMessage(); } HDC hMemDC = CreateCompatibleDC(NULL); if (hMemDC == NULL) { GetLastErrorMessage(); } HBITMAP hOriBmp = (HBITMAP)SelectObject(hMemDC, hbitmap); if (GetLastError()!=0) { GetLastErrorMessage(); } if (m_bIsImageMode) { RECT rcPaint = {0, 0, nShadWndWid, nShadWndHei}; const TDrawInfo* pDrawInfo = m_pManager->GetDrawInfo(m_sShadowImage, _T("")); if(pDrawInfo != NULL) { const TImageInfo* data = m_pManager->GetImageEx(pDrawInfo->sImageName, pDrawInfo->sResType, pDrawInfo->dwMask, pDrawInfo->bHSL, pDrawInfo->bGdiplus); if( !data ) return; RECT rcBmpPart = {0}; rcBmpPart.right = data->nX; rcBmpPart.bottom = data->nY; RECT corner = m_rcShadowCorner; CRenderEngine::DrawImage(hMemDC, data->hBitmap, rcPaint, rcPaint, rcBmpPart, corner, data->bAlpha, 0xFF, true, false, false); } } else { ZeroMemory(pvBits, bmi.bmiHeader.biSizeImage); MakeShadow((UINT32 *)pvBits, hParent, &WndRect); } POINT ptDst; if (m_bIsImageMode) { ptDst.x = WndRect.left - m_nSize; ptDst.y = WndRect.top - m_nSize; } else { ptDst.x = WndRect.left + m_nxOffset - m_nSize; ptDst.y = WndRect.top + m_nyOffset - m_nSize; } POINT ptSrc = {0, 0}; SIZE WndSize = {nShadWndWid, nShadWndHei}; BLENDFUNCTION blendPixelFunction= { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; MoveWindow(m_hWnd, ptDst.x, ptDst.y, nShadWndWid, nShadWndHei, FALSE); BOOL bRet= ::UpdateLayeredWindow(m_hWnd, NULL, &ptDst, &WndSize, hMemDC, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA); _ASSERT(bRet); // something was wrong.... // Delete used resources SelectObject(hMemDC, hOriBmp); DeleteObject(hbitmap); DeleteDC(hMemDC); } // --------------------------------------------------------------------------- // [2026-09-25] R25 - anti-aliased inner hole for the shadow. // // Symptom: on a layered anti-aliased rounded window the top corners showed a // dark line, and the bottom corners showed a broken / too-thin shadow. // // Root cause: the hole punched into the shadow bitmap was cut with a 1-BIT // silhouette (PtInRegion over hParentRgn), while the window content itself is // cut with an 8x8 supersampled alpha mask (CPaintManagerUI:: // FillRoundRectAlphaMask). The two outlines disagree by up to 1px along the // corner arc, and the shadow window is composited ABOVE the parent window, so: // * scan lines where the 1-bit arc sits INSIDE the real arc keep shadow on // pixels the window already covers fully -> they get darkened. On a // saturated title bar that reads as a black line on the top corners. // * scan lines where the 1-bit arc sits OUTSIDE the real arc zero out shadow // that belongs to the outside of the arc -> the corner shadow is bitten // off, which reads as broken / too thin shadow on the bottom corners. // // Fix: instead of the boolean hole, fade the shadow by the SAME coverage the // window mask uses. cov = 8x8 supersampled area coverage of the rounded rect at // that pixel (0..255), so: // cov == 255 (inside) -> alpha 0 (the hole, as before) // 0 < cov < 255 (arc) -> alpha * (255 - cov) / 255 (smooth + aligned) // cov == 0 (outside) -> alpha unchanged // Straight edges are unaffected: coverage there is always 255 or 0, never // partial, so the top / bottom / left / right shadow bands keep their exact // current values (verified: 180 / 205 / 231 on all four sides). // // NOTE: this file is compiled without a UTF-8 BOM - keep every comment ASCII. // --------------------------------------------------------------------------- // Exact port of CPaintManagerUI::FillRoundRectAlphaMask's coverage maths // (same pixel-centre sampling, same corner centre, same 8x8 supersampling), // otherwise the two outlines would keep drifting apart by half a pixel. static int ShadowRoundRectCoverage(int x, int y, int w, int h, int r) { if (r <= 0 || w <= 0 || h <= 0) return 255; if (r > w / 2) r = w / 2; if (r > h / 2) r = h / 2; if (r < 1) return 255; if (x < 0 || y < 0 || x >= w || y >= h) return 0; const double cx = w * 0.5; const double cy = h * 0.5; const double hw = cx; const double hh = cy; const double px = fabs(x + 0.5 - cx) - (hw - r); const double py = fabs(y + 0.5 - cy) - (hh - r); // Inside band / straight edge: the window boundary IS the mask boundary. if (px <= 0.0 || py <= 0.0) return 255; // Corner block: corner centre = the inner rect's corner point. // Getting the sign wrong moves the centre to the diagonally opposite side of // the window, every sample falls outside and the whole corner reads 0. const double ccx = cx + ((x + 0.5 < cx) ? -(hw - r) : (hw - r)); const double ccy = cy + ((y + 0.5 < cy) ? -(hh - r) : (hh - r)); const double rr = (double)r * r; int nIn = 0; for (int sy = 0; sy < 8; ++sy) { const double syp = y + (sy + 0.5) / 8.0; for (int sx = 0; sx < 8; ++sx) { const double sxp = x + (sx + 0.5) / 8.0; const double ddx = sxp - ccx; const double ddy = syp - ccy; if (ddx * ddx + ddy * ddy <= rr) ++nIn; } } return (nIn * 255 + 32) / 64; } // [2026-09-26 R31] One-key rollback for the corner colour compensation below. // Default ON. Set the environment variable ZP_SHADOW_COLOR_COMP=0 (before start-up) // to fall back to the plain linear alpha attenuation R28 introduced. static bool ShadowCornerColorCompEnabled() { static int s_nEnabled = -1; if (s_nEnabled < 0) { TCHAR szBuf[8] = { 0 }; const DWORD dwRet = ::GetEnvironmentVariable(_T("ZP_SHADOW_COLOR_COMP"), szBuf, 8); s_nEnabled = (dwRet == 1 && szBuf[0] == _T('0')) ? 0 : 1; } return s_nEnabled != 0; } void CShadowUI::MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent) { // The shadow algorithm: // Get the region of parent window, // Apply morphologic erosion to shrink it into the size (ShadowWndSize - Sharpness) // Apply modified (with blur effect) morphologic dilation to make the blurred border // The algorithm is optimized by assuming parent window is just "one piece" and without "wholes" on it // Get the region of parent window. // // [2026-09-24] In layered anti-aliased rounded-corner mode the window deliberately has // NO window region: CPaintManagerUI::GetRoundCorner() returns {0,0} so that every // window's OnSize skips CreateRoundRectRgn/SetWindowRgn (a 1-bit HRGN would re-cut the // smooth anti-aliased corner produced by the alpha mask). Consequence: GetWindowRgn // below cannot read the real shape, hParentRgn keeps the plain full rectangle, so the // shadow's inner silhouette degrades into a right angle. In the four corners of a // rounded window the background then shows through inside a SQUARE shadow frame // (looks like a "white corner"). // Fix: when no valid region is available, synthesise a rounded region from the window's // configured corner radius. radius == 0 keeps the old rectangle behaviour, and windows // that DO have a region (GetWindowRgn returns SIMPLEREGION/COMPLEXREGION) are untouched. const int nParentW = abs(rcParent->right - rcParent->left); const int nParentH = abs(rcParent->bottom - rcParent->top); // [2026-09-25] R25: radius used by the anti-aliased hole further down. Kept // separate from szRgnCorner because that one is only applied when the radius // fits inside the window; here we only need "is there a radius at all" // (0 == no rounded corner == plain rectangle hole, i.e. the old behaviour). SIZE szAaCorner = { 0, 0 }; if (m_pManager != NULL) szAaCorner = m_pManager->GetRoundCornerForShadow(); HRGN hParentRgn = CreateRectRgn(0, 0, nParentW, nParentH); const int nRgnType = ::GetWindowRgn(hParent, hParentRgn); if (nRgnType == ERROR || nRgnType == NULLREGION) { SIZE szRgnCorner = { 0, 0 }; if (m_pManager != NULL) szRgnCorner = m_pManager->GetRoundCornerForShadow(); if (szRgnCorner.cx > 0 && szRgnCorner.cy > 0 && szRgnCorner.cx * 2 < nParentW && szRgnCorner.cy * 2 < nParentH) { ::DeleteObject(hParentRgn); // [2026-09-24] R17: CreateRoundRectRgn takes 2x radius. // CreateRoundRectRgn's nWidthEllipse/nHeightEllipse is the FULL width/height of // the corner ellipse (radius = value / 2), while GetRoundCornerForShadow() returns // the RADIUS (same unit as CPaintManagerUI::FillRoundRectAlphaMask, which uses the // value directly as a radius). Feeding the radius here produced a shadow silhouette // with only HALF the window's corner radius: the dark band cut across at a smaller // arc and left a sliver of unshadowed background glued to the real arc (a "white // edge" on light backgrounds, a stray shadow blob on dark ones). // [2026-09-25] R25: dropped the old "+1". CreateRoundRectRgn(0,0,W,H,..) // already covers pixel columns [0,W) and rows [0,H) - exactly the pixel grid // the scan loop samples with PtInRegion(j,i). The +1 pushed the right and // bottom arcs 1px further out than the window's own mask arc, which made the // bottom-right corner shadow heavier than the other three corners. hParentRgn = ::CreateRoundRectRgn(0, 0, nParentW, nParentH, szRgnCorner.cx * 2, szRgnCorner.cy * 2); } } // Determine the Start and end point of each horizontal scan line SIZE szParent = {rcParent->right - rcParent->left, rcParent->bottom - rcParent->top}; SIZE szShadow = {szParent.cx + 2 * m_nSize, szParent.cy + 2 * m_nSize}; // Extra 2 lines (set to be empty) in ptAnchors are used in dilation int nAnchors = max(szParent.cy, szShadow.cy); // # of anchor points pares int (*ptAnchors)[2] = new int[nAnchors + 2][2]; int (*ptAnchorsOri)[2] = new int[szParent.cy][2]; // anchor points, will not modify during erosion ptAnchors[0][0] = szParent.cx; ptAnchors[0][1] = 0; ptAnchors[nAnchors + 1][0] = szParent.cx; ptAnchors[nAnchors + 1][1] = 0; if(m_nSize > 0) { // Put the parent window anchors at the center for(int i = 0; i < m_nSize; i++) { ptAnchors[i + 1][0] = szParent.cx; ptAnchors[i + 1][1] = 0; ptAnchors[szShadow.cy - i][0] = szParent.cx; ptAnchors[szShadow.cy - i][1] = 0; } ptAnchors += m_nSize; } for(int i = 0; i < szParent.cy; i++) { // find start point int j; for(j = 0; j < szParent.cx; j++) { if(PtInRegion(hParentRgn, j, i)) { ptAnchors[i + 1][0] = j + m_nSize; ptAnchorsOri[i][0] = j; break; } } // Start point not found if(j >= szParent.cx) { ptAnchors[i + 1][0] = szParent.cx; ptAnchorsOri[i][1] = 0; ptAnchors[i + 1][0] = szParent.cx; ptAnchorsOri[i][1] = 0; } else { // find end point for(j = szParent.cx - 1; j >= ptAnchors[i + 1][0]; j--) { if(PtInRegion(hParentRgn, j, i)) { ptAnchors[i + 1][1] = j + 1 + m_nSize; ptAnchorsOri[i][1] = j + 1; break; } } } } if(m_nSize > 0) ptAnchors -= m_nSize; // Restore pos of ptAnchors for erosion int (*ptAnchorsTmp)[2] = new int[nAnchors + 2][2]; // Store the result of erosion // First and last line should be empty ptAnchorsTmp[0][0] = szParent.cx; ptAnchorsTmp[0][1] = 0; ptAnchorsTmp[nAnchors + 1][0] = szParent.cx; ptAnchorsTmp[nAnchors + 1][1] = 0; int nEroTimes = 0; // morphologic erosion for(int i = 0; i < m_nSharpness - m_nSize; i++) { nEroTimes++; //ptAnchorsTmp[1][0] = szParent.cx; //ptAnchorsTmp[1][1] = 0; //ptAnchorsTmp[szParent.cy + 1][0] = szParent.cx; //ptAnchorsTmp[szParent.cy + 1][1] = 0; for(int j = 1; j < nAnchors + 1; j++) { ptAnchorsTmp[j][0] = max(ptAnchors[j - 1][0], max(ptAnchors[j][0], ptAnchors[j + 1][0])) + 1; ptAnchorsTmp[j][1] = min(ptAnchors[j - 1][1], min(ptAnchors[j][1], ptAnchors[j + 1][1])) - 1; } // Exchange ptAnchors and ptAnchorsTmp; int (*ptAnchorsXange)[2] = ptAnchorsTmp; ptAnchorsTmp = ptAnchors; ptAnchors = ptAnchorsXange; } // morphologic dilation ptAnchors += (m_nSize < 0 ? -m_nSize : 0) + 1; // now coordinates in ptAnchors are same as in shadow window // Generate the kernel int nKernelSize = m_nSize > m_nSharpness ? m_nSize : m_nSharpness; int nCenterSize = m_nSize > m_nSharpness ? (m_nSize - m_nSharpness) : 0; UINT32 *pKernel = new UINT32[(2 * nKernelSize + 1) * (2 * nKernelSize + 1)]; UINT32 *pKernelIter = pKernel; for(int i = 0; i <= 2 * nKernelSize; i++) { for(int j = 0; j <= 2 * nKernelSize; j++) { double dLength = sqrt((i - nKernelSize) * (i - nKernelSize) + (j - nKernelSize) * (double)(j - nKernelSize)); if(dLength < nCenterSize) *pKernelIter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness); else if(dLength <= nKernelSize) { UINT32 nFactor = ((UINT32)((1 - (dLength - nCenterSize) / (m_nSharpness + 1)) * m_nDarkness)); *pKernelIter = nFactor << 24 | PreMultiply(m_Color, nFactor); } else *pKernelIter = 0; //TRACE("%d ", *pKernelIter >> 24); pKernelIter ++; } //TRACE("\n"); } // Generate blurred border for(int i = nKernelSize; i < szShadow.cy - nKernelSize; i++) { int j; if(ptAnchors[i][0] < ptAnchors[i][1]) { // Start of line for(j = ptAnchors[i][0]; j < min(max(ptAnchors[i - 1][0], ptAnchors[i + 1][0]) + 1, ptAnchors[i][1]); j++) { for(int k = 0; k <= 2 * nKernelSize; k++) { UINT32 *pPixel = pShadBits + (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize; UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1); for(int l = 0; l <= 2 * nKernelSize; l++) { if(*pPixel < *pKernelPixel) *pPixel = *pKernelPixel; pPixel++; pKernelPixel++; } } } // for() start of line // End of line for(j = max(j, min(ptAnchors[i - 1][1], ptAnchors[i + 1][1]) - 1); j < ptAnchors[i][1]; j++) { for(int k = 0; k <= 2 * nKernelSize; k++) { UINT32 *pPixel = pShadBits + (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize; UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1); for(int l = 0; l <= 2 * nKernelSize; l++) { if(*pPixel < *pKernelPixel) *pPixel = *pKernelPixel; pPixel++; pKernelPixel++; } } } // for() end of line } } // for() Generate blurred border // Erase unwanted parts and complement missing UINT32 clCenter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness); const int nAaRadius = (szAaCorner.cx > 0) ? szAaCorner.cx : 0; for(int i = min(nKernelSize, max(m_nSize - m_nyOffset, 0)); i < max(szShadow.cy - nKernelSize, min(szParent.cy + m_nSize - m_nyOffset, szParent.cy + 2 * m_nSize)); i++) { UINT32 *pLine = pShadBits + (szShadow.cy - i - 1) * szShadow.cx; // Line is not covered by parent window if(i - m_nSize + m_nyOffset < 0 || i - m_nSize + m_nyOffset >= szParent.cy) { for(int j = ptAnchors[i][0]; j < ptAnchors[i][1]; j++) { *(pLine + j) = clCenter; } } else { for(int j = ptAnchors[i][0]; j < min(ptAnchorsOri[i - m_nSize + m_nyOffset][0] + m_nSize - m_nxOffset, ptAnchors[i][1]); j++) *(pLine + j) = clCenter; for(int j = max(ptAnchorsOri[i - m_nSize + m_nyOffset][1] + m_nSize - m_nxOffset, ptAnchors[i][0]); j < ptAnchors[i][1]; j++) *(pLine + j) = clCenter; // [2026-09-25] R25: the anti-aliased hole replaces the old 1-bit // "*(pLine + j) = 0" loop. It must stay AFTER the two clCenter fill // passes above, otherwise those passes would overwrite the fade. // Scope is the whole parent-rect span of this line (not just the // ptAnchorsOri span), so the partially covered pixels that the 1-bit // silhouette classified as "outside" are faded too. { const int nHoleX0 = max(m_nSize - m_nxOffset, 0); const int nHoleX1 = min(szParent.cx + m_nSize - m_nxOffset, szShadow.cx); const int nHoleY = i - m_nSize + m_nyOffset; for(int j = nHoleX0; j < nHoleX1; j++) { UINT32 nPixel = *(pLine + j); if(nPixel == 0) continue; const int nCov = ShadowRoundRectCoverage(j - (m_nSize - m_nxOffset), nHoleY, szParent.cx, szParent.cy, nAaRadius); if(nCov >= 255) { *(pLine + j) = 0; continue; } if(nCov <= 0) continue; UINT32 nAlpha = ((nPixel >> 24) * (255 - nCov)) / 255; *(pLine + j) = (nAlpha == 0) ? 0 : ((nAlpha << 24) | PreMultiply(m_Color, (unsigned char)nAlpha)); } } } } // ----------------------------------------------------------------------- // [2026-09-26] R26 - corner shadow top-up. // // The morphological dilation above grows the silhouette by // (m_nSharpness - m_nSize) erosion steps and then dilates with a radius of // max(m_nSize, m_nSharpness). Along a straight edge that lands exactly // m_nSize px outside the window - which is why the four straight bands carry // the expected 75 / 50 / 25 alpha. Around a CONVEX corner it does not: every // erosion step takes max() over the three neighbouring rows, so on the corner // arc it tightens faster than the arc itself, and the band left after the // dilation is 1..3 px too narrow there. On a white background that reads as // "the shadow is missing at the bottom corners". // // Fix: after the erase pass, top up every pixel that is FULLY outside the // window (coverage == 0) using the analytic distance to the rounded rect and // the very same radial profile the kernel uses. Straight edges are untouched // - they already carry the profile value and the update is a max(). Pixels the // window covers even partially are skipped, so the hole stays a hole. // Note the ordering: this must run AFTER the erase/hole pass above, otherwise // the erase would wipe the corner band again. // ----------------------------------------------------------------------- if (nAaRadius > 0) { const int nEroSteps = (m_nSharpness > m_nSize) ? (int)(m_nSharpness - m_nSize) : 0; // [2026-09-26 R31] Parent window content x mask, premultiplied (the value the // compensation below needs). Only the layered rounded-mask path has it; every other // path, and any frame painted before the parent has painted at all, leaves it NULL // and the block below falls back to the old pure-black band. const bool bColorComp = ShadowCornerColorCompEnabled(); COLORREF* pBkgBits = NULL; RECT rcBkg = { 0, 0, 0, 0 }; int nBkgW = 0; int nBkgH = 0; if (bColorComp && m_pManager != NULL) pBkgBits = m_pManager->GetLayeredContentBits(&nBkgW, &nBkgH, &rcBkg); const double dCx = szParent.cx * 0.5; const double dCy = szParent.cy * 0.5; const double dHw = dCx; const double dHh = dCy; const int nPad = nKernelSize + 2; for (int iy = -nPad; iy <= szParent.cy + nPad; ++iy) { const int i = iy + m_nSize - m_nyOffset; if (i < 0 || i >= szShadow.cy) continue; UINT32 *pLineTop = pShadBits + (szShadow.cy - i - 1) * szShadow.cx; const double dPy = fabs(iy + 0.5 - dCy) - (dHh - nAaRadius); for (int jx = -nPad; jx <= szParent.cx + nPad; ++jx) { const int j = jx + m_nSize - m_nxOffset; if (j < 0 || j >= szShadow.cx) continue; // Straight-band interior (the bulk of the window): nothing to do. const double dPx = fabs(jx + 0.5 - dCx) - (dHw - nAaRadius); if (dPx <= 0.0 && dPy <= 0.0) continue; // [2026-09-26 R28] Coverage of the window's own anti-aliased mask at // this pixel. The old guard was 'coverage != 0 - skip', which is why // the corner arc still looked wrong. Along a straight edge coverage is // only ever 0 or 255, but the corner arc carries a one pixel ring where // coverage is strictly between the two. Those pixels were skipped here, // kept whatever the too tight morphological band had produced, and // showed up as a bright dash hugging the arc: a pale seam between the // title bar and the shadow on the top corners, a stair stepped dashed // edge on the bottom ones. The bottom corners had twice as many such // pixels and twice the gap, which is why the two ends of the window // looked like two different defects. Now only a FULLY covered pixel is // skipped, so the hole R25 punched stays a hole; partially covered // pixels are topped up and attenuated further down by the fraction of // the pixel the window does not cover. const int nCov = ShadowRoundRectCoverage(jx, iy, szParent.cx, szParent.cy, nAaRadius); if (nCov >= 255) continue; // fully covered: the hole stays a hole double d; // [2026-09-25 R27] Calibrate the corner band so it joins the four // straight edges seamlessly: // * the two straight-edge branches below used to return the distance // to the straight-band inner boundary instead of to the window // edge, i.e. nAaRadius too small a deduction. d stayed above // nKernelSize so the top-up never fired along the four edges; // those pixels kept the base band while the corner arc got the // full analytic value -- the arc sat +12 alpha above the band at // every distance and reached b=3.0 instead of 2.5, so every corner // showed a hard seam plus a fatter, darker hook. // * with all three branches corrected d is simply the rounded-rect // signed distance field, so this top-up becomes a pure function of // the distance and the whole band matches the four edges exactly. // * u carries +0.5 because the base band is calibrated as // K of d plus 2.5: 1/2/3 px outside -> alpha 75/50/25. Without // it the arc peaked at 87 instead of 75. if (dPx <= 0.0) d = dPy - nAaRadius; // nearest horizontal edge else if (dPy <= 0.0) d = dPx - nAaRadius; // nearest vertical edge else d = sqrt(dPx * dPx + dPy * dPy) - nAaRadius; // nearest corner arc // [2026-09-26 R29] A partially covered pixel whose centre is still inside // the window, d <= 0, used to be skipped by the guard that stood here, and // that guard is what left a pale seam between the shadow and the window all // along the corner arcs -- most visible on the top left and top right // rounding of the main window and of the dialogs. // // Such a pixel is only partly covered by the window, but the part that is // NOT covered lies outside the window, between 0 and one pixel away from // the silhouette. That is exactly the rim the band profile is calibrated // for, so the band value here is the one at distance 0. Clamping to 0 keeps // the corner arc a pure function of the distance, the same shape R27 gave // to the fully outside pixels of the arc. The pixel is then combined below // by coverage -- R28 attenuated its alpha linearly, R31 instead keeps the // band alpha and replaces the black source with the coverage-corrected // content colour. // // Scope: only 0 < coverage < 255 can reach this line. A fully covered pixel // was skipped above so the hole stays a hole, and a fully outside one has // d > 0, so clamping is a no-op there. The four straight edges, the whole // outer arc and the hole are therefore left bit for bit untouched. if (d < 0.0) d = 0.0; // clamp to the window rim const double u = d + nEroSteps + 0.5; UINT32 nWant; if (u < (double)nCenterSize) nWant = m_nDarkness; else if (u <= (double)nKernelSize) nWant = (UINT32)((1.0 - (u - nCenterSize) / (double)(m_nSharpness + 1)) * m_nDarkness); else continue; if (nWant == 0) continue; // [2026-09-26 R28 -> R31] SUPERSEDED. This used to be a plain linear // attenuation, as = a * (255 - cov) / 255 with a black source. It is the // correct lower bound only when the content colour equals the desktop // colour, and it is still the fallback used below when the parent content // bitmap is unavailable, but on darker content it under-attenuated and left // the pale 1px arc along the top corners. R31 replaced it with the exact // coverage-corrected source colour. // [2026-09-26 R31] Exact colour compensation for the partially covered rim. // // What the window + shadow stack really does at a pixel the window only partly // covers (coverage c, content colour w, desktop D): // under = w*c + D*(1-c) window composited onto the desktop // out = under*(1-as) + S shadow window composited on top of it // and what it SHOULD look like is the shadow darkening only the part of the pixel // the window does not cover: // ideal = w*c + D*(1-c)*(1-a) // Solving out == ideal with as = a (the band value, undiminished) gives // S = a*w*c // which is independent of D -- so the ring no longer depends on what sits behind // the window. The rule that used to stand here was as = a*(1-c) with S = 0, i.e. a // plain black shadow thinned by the coverage; that is the correct lower bound only // when w == D. For darker content (the green title bar, w ~ 96..131 on a bright // desktop) it under-attenuates by // delta = a * (1-c) * (D-w) * c / 255 // which is 10..14 of 255 on the top corners of this app: the pale 1px arc between // the shadow and the rounded window that R26..R30 could only shrink, never remove. // // w*c is exactly the premultiplied content pixel CPaintManagerUI keeps in // m_pBackgroundBits for the layered rounded-mask path, so the whole fix is "read // that pixel, multiply it by the band alpha, and do not dim the alpha". Straight // edges and the outer arc have c == 0, hence S == 0, hence keep the old black band // bit for bit. A fully covered pixel (c == 255) is the hole and never gets here. UINT32 nRgb = PreMultiply(m_Color, (unsigned char)nWant); if (nCov > 0 && pBkgBits != NULL) { const LONG nBx = jx - rcBkg.left; const LONG nBy = iy - rcBkg.top; if (nBx >= 0 && nBx < nBkgW && nBy >= 0 && nBy < nBkgH) { // bottom-up DIB: window row nBy lives at memory row (nBkgH - 1 - nBy) const UINT32 nPc = ((UINT32)pBkgBits[(LONG)(nBkgH - 1 - nBy) * nBkgW + nBx]) & 0x00FFFFFFu; if (nPc != 0) { const UINT32 nRc = (((nPc >> 16) & 0xFFu) * nWant + 127) / 255; const UINT32 nGc = (((nPc >> 8) & 0xFFu) * nWant + 127) / 255; const UINT32 nBc = ((nPc & 0xFFu) * nWant + 127) / 255; nRgb = (nRc << 16) | (nGc << 8) | nBc; } } } if (nWant == 0) continue; if ((*(pLineTop + j) >> 24) < nWant) *(pLineTop + j) = (nWant << 24) | nRgb; } } } // Delete used resources delete[] (ptAnchors - (m_nSize < 0 ? -m_nSize : 0) - 1); delete[] ptAnchorsTmp; delete[] ptAnchorsOri; delete[] pKernel; DeleteObject(hParentRgn); } void CShadowUI::ShowShadow(bool bShow) { m_bIsShowShadow = bShow; } bool CShadowUI::IsShowShadow() const { return m_bIsShowShadow; } void CShadowUI::DisableShadow(bool bDisable) { m_bIsDisableShadow = bDisable; if (m_hWnd != NULL) { if (m_bIsDisableShadow) { ::ShowWindow(m_hWnd, SW_HIDE); } else { // Determine the initial show state of shadow according to parent window's state LONG lParentStyle = GetWindowLongPtr(GetParent(m_hWnd), GWL_STYLE); if (!(WS_VISIBLE & lParentStyle)) // Parent invisible m_Status = SS_ENABLED; else if ((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow m_Status = SS_ENABLED | SS_PARENTVISIBLE; else { m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE; } if ((WS_VISIBLE & lParentStyle) && !((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle)) { ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE); Update(GetParent(m_hWnd)); } } } } ////TODO shadow disnable fix//// bool CShadowUI::IsDisableShadow() const { return m_bIsDisableShadow; } bool CShadowUI::SetSize(int NewSize) { if(NewSize > 35 || NewSize < -35) return false; m_nSize = (signed char)NewSize; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetSharpness(unsigned int NewSharpness) { if(NewSharpness > 35) return false; m_nSharpness = (unsigned char)NewSharpness; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetDarkness(unsigned int NewDarkness) { if(NewDarkness > 255) return false; m_nDarkness = (unsigned char)NewDarkness; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetPosition(int NewXOffset, int NewYOffset) { if(NewXOffset > 35 || NewXOffset < -35 || NewYOffset > 35 || NewYOffset < -35) return false; m_nxOffset = (signed char)NewXOffset; m_nyOffset = (signed char)NewYOffset; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetColor(COLORREF NewColor) { m_Color = NewColor; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetImage(LPCTSTR szImage) { if (szImage == NULL) return false; m_bIsImageMode = true; m_sShadowImage = szImage; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) Update(GetParent(m_hWnd)); return true; } bool CShadowUI::SetShadowCorner(RECT rcCorner) { if (rcCorner.left < 0 || rcCorner.top < 0 || rcCorner.right < 0 || rcCorner.bottom < 0) return false; m_rcShadowCorner = rcCorner; if(m_hWnd != NULL && (SS_VISABLE & m_Status)) { Update(GetParent(m_hWnd)); } return true; } bool CShadowUI::CopyShadow(CShadowUI* pShadow) { if (m_bIsImageMode) { pShadow->SetImage(m_sShadowImage); pShadow->SetShadowCorner(m_rcShadowCorner); pShadow->SetSize((int)m_nSize); } else { pShadow->SetSize((int)m_nSize); pShadow->SetSharpness((unsigned int)m_nSharpness); pShadow->SetDarkness((unsigned int)m_nDarkness); pShadow->SetColor(m_Color); pShadow->SetPosition((int)m_nxOffset, (int)m_nyOffset); } pShadow->ShowShadow(m_bIsShowShadow); return true; } } //namespace DuiLib