UIShadow.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #include "StdAfx.h"
  2. #include "UIShadow.h"
  3. #include "math.h"
  4. #include "crtdbg.h"
  5. namespace DuiLib
  6. {
  7. const TCHAR *strWndClassName = _T("PerryShadowWnd");
  8. bool CShadowUI::s_bHasInit = FALSE;
  9. CShadowUI::CShadowUI(void)
  10. : m_hWnd((HWND)NULL)
  11. , m_OriParentProc(NULL)
  12. , m_Status(0)
  13. , m_nDarkness(150)
  14. , m_nSharpness(5)
  15. , m_nSize(0)
  16. , m_nxOffset(0)
  17. , m_nyOffset(0)
  18. , m_Color(RGB(0, 0, 0))
  19. , m_WndSize(0)
  20. , m_bUpdate(false)
  21. , m_bIsImageMode(false)
  22. , m_bIsShowShadow(false)
  23. , m_bIsDisableShadow(false)
  24. {
  25. ::ZeroMemory(&m_rcShadowCorner, sizeof(RECT));
  26. }
  27. CShadowUI::~CShadowUI(void)
  28. {
  29. }
  30. bool CShadowUI::Initialize(HINSTANCE hInstance)
  31. {
  32. if (s_bHasInit)
  33. return false;
  34. // Register window class for shadow window
  35. WNDCLASSEX wcex;
  36. memset(&wcex, 0, sizeof(wcex));
  37. wcex.cbSize = sizeof(WNDCLASSEX);
  38. wcex.style = CS_HREDRAW | CS_VREDRAW;
  39. wcex.lpfnWndProc = DefWindowProc;
  40. wcex.cbClsExtra = 0;
  41. wcex.cbWndExtra = 0;
  42. wcex.hInstance = hInstance;
  43. wcex.hIcon = NULL;
  44. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  45. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  46. wcex.lpszMenuName = NULL;
  47. wcex.lpszClassName = strWndClassName;
  48. wcex.hIconSm = NULL;
  49. RegisterClassEx(&wcex);
  50. s_bHasInit = true;
  51. return true;
  52. }
  53. void CShadowUI::Create(CPaintManagerUI* pPaintManager)
  54. {
  55. if(!m_bIsShowShadow)
  56. return;
  57. // Already initialized
  58. _ASSERT(CPaintManagerUI::GetInstance() != INVALID_HANDLE_VALUE);
  59. _ASSERT(pPaintManager != NULL);
  60. m_pManager = pPaintManager;
  61. HWND hParentWnd = m_pManager->GetPaintWindow();
  62. // Add parent window - shadow pair to the map
  63. _ASSERT(GetShadowMap().find(hParentWnd) == GetShadowMap().end()); // Only one shadow for each window
  64. GetShadowMap()[hParentWnd] = this;
  65. // Determine the initial show state of shadow according to parent window's state
  66. LONG lParentStyle = GetWindowLongPtr(hParentWnd, GWL_STYLE);
  67. // Create the shadow window
  68. LONG styleValue = lParentStyle & WS_CAPTION;
  69. m_hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, strWndClassName, NULL,
  70. /*WS_VISIBLE | */styleValue | WS_POPUPWINDOW,
  71. CW_USEDEFAULT, 0, 0, 0, hParentWnd, NULL, CPaintManagerUI::GetInstance(), NULL);
  72. if(!(WS_VISIBLE & lParentStyle)) // Parent invisible
  73. m_Status = SS_ENABLED;
  74. else if((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow
  75. m_Status = SS_ENABLED | SS_PARENTVISIBLE;
  76. else // Show the shadow
  77. {
  78. m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE;
  79. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  80. Update(hParentWnd);
  81. }
  82. // Replace the original WndProc of parent window to steal messages
  83. m_OriParentProc = GetWindowLongPtr(hParentWnd, GWLP_WNDPROC);
  84. #pragma warning(disable: 4311) // temporrarily disable the type_cast warning in Win32
  85. SetWindowLongPtr(hParentWnd, GWLP_WNDPROC, (LONG_PTR)ParentProc);
  86. #pragma warning(default: 4311)
  87. }
  88. std::map<HWND, CShadowUI *>& CShadowUI::GetShadowMap()
  89. {
  90. static std::map<HWND, CShadowUI *> s_Shadowmap;
  91. return s_Shadowmap;
  92. }
  93. LRESULT CALLBACK CShadowUI::ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  94. {
  95. _ASSERT(GetShadowMap().find(hwnd) != GetShadowMap().end()); // Shadow must have been attached
  96. CShadowUI *pThis = GetShadowMap()[hwnd];
  97. if (pThis->IsDisableShadow()) {
  98. return CallWindowProc((WNDPROC)pThis->m_OriParentProc, hwnd, uMsg, wParam, lParam);
  99. }
  100. switch(uMsg)
  101. {
  102. case WM_ACTIVATEAPP:
  103. {
  104. if(!IsWindowEnabled(hwnd)) break;
  105. ::SetWindowPos(pThis->m_hWnd, hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW);
  106. break;
  107. }
  108. case WM_NCACTIVATE:
  109. {
  110. if(!IsWindowEnabled(hwnd)) break;
  111. ::SetWindowPos(pThis->m_hWnd, hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW);
  112. break;
  113. }
  114. case WM_WINDOWPOSCHANGED:
  115. {
  116. RECT WndRect;
  117. LPWINDOWPOS pWndPos;
  118. pWndPos = (LPWINDOWPOS)lParam;
  119. GetWindowRect(hwnd, &WndRect);
  120. if (pThis->m_bIsImageMode) {
  121. SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left - pThis->m_nSize, WndRect.top - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  122. }
  123. else {
  124. 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);
  125. }
  126. if (pWndPos->flags & SWP_SHOWWINDOW) {
  127. if (pThis->m_Status & SS_ENABLED && !(pThis->m_Status & SS_PARENTVISIBLE))
  128. {
  129. pThis->m_bUpdate = true;
  130. ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE);
  131. pThis->m_Status |= SS_VISABLE | SS_PARENTVISIBLE;
  132. }
  133. }
  134. else if (pWndPos->flags & SWP_HIDEWINDOW) {
  135. if (pThis->m_Status & SS_ENABLED)
  136. {
  137. ::ShowWindow(pThis->m_hWnd, SW_HIDE);
  138. pThis->m_Status &= ~(SS_VISABLE | SS_PARENTVISIBLE);
  139. }
  140. }
  141. break;
  142. }
  143. case WM_MOVE:
  144. if(pThis->m_Status & SS_VISABLE) {
  145. RECT WndRect;
  146. GetWindowRect(hwnd, &WndRect);
  147. if (pThis->m_bIsImageMode) {
  148. SetWindowPos(pThis->m_hWnd, hwnd, WndRect.left - pThis->m_nSize, WndRect.top - pThis->m_nSize, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  149. }
  150. else {
  151. 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);
  152. }
  153. }
  154. break;
  155. case WM_SIZE:
  156. if(pThis->m_Status & SS_ENABLED) {
  157. if(SIZE_MAXIMIZED == wParam || SIZE_MINIMIZED == wParam) {
  158. ::ShowWindow(pThis->m_hWnd, SW_HIDE);
  159. pThis->m_Status &= ~SS_VISABLE;
  160. }
  161. // Parent maybe resized even if invisible
  162. else if(pThis->m_Status & SS_PARENTVISIBLE) {
  163. // Awful! It seems that if the window size was not decreased
  164. // the window region would never be updated until WM_PAINT was sent.
  165. // So do not Update() until next WM_PAINT is received in this case
  166. if(LOWORD(lParam) > LOWORD(pThis->m_WndSize) || HIWORD(lParam) > HIWORD(pThis->m_WndSize))
  167. pThis->m_bUpdate = true;
  168. else
  169. pThis->Update(hwnd);
  170. if(!(pThis->m_Status & SS_VISABLE)) {
  171. ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE);
  172. pThis->m_Status |= SS_VISABLE;
  173. }
  174. }
  175. pThis->m_WndSize = lParam;
  176. }
  177. break;
  178. case WM_PAINT:
  179. {
  180. if(pThis->m_bUpdate) {
  181. pThis->Update(hwnd);
  182. pThis->m_bUpdate = false;
  183. }
  184. break;
  185. }
  186. // In some cases of sizing, the up-right corner of the parent window region would not be properly updated
  187. // Update() again when sizing is finished
  188. case WM_EXITSIZEMOVE:
  189. if(pThis->m_Status & SS_VISABLE) {
  190. pThis->Update(hwnd);
  191. }
  192. break;
  193. case WM_SHOWWINDOW:
  194. if(pThis->m_Status & SS_ENABLED) {
  195. // the window is being hidden
  196. if(!wParam) {
  197. ::ShowWindow(pThis->m_hWnd, SW_HIDE);
  198. pThis->m_Status &= ~(SS_VISABLE | SS_PARENTVISIBLE);
  199. }
  200. else if(!(pThis->m_Status & SS_PARENTVISIBLE)) {
  201. //pThis->Update(hwnd);
  202. pThis->m_bUpdate = true;
  203. ::ShowWindow(pThis->m_hWnd, SW_SHOWNOACTIVATE);
  204. pThis->m_Status |= SS_VISABLE | SS_PARENTVISIBLE;
  205. }
  206. }
  207. break;
  208. case WM_DESTROY:
  209. DestroyWindow(pThis->m_hWnd); // Destroy the shadow
  210. break;
  211. case WM_NCDESTROY:
  212. GetShadowMap().erase(hwnd); // Remove this window and shadow from the map
  213. break;
  214. }
  215. return CallWindowProc((WNDPROC)pThis->m_OriParentProc, hwnd, uMsg, wParam, lParam);
  216. }
  217. //Formats GetLastError() value.
  218. void GetLastErrorMessage()
  219. {
  220. LPVOID lpMsgBuf;
  221. FormatMessage(
  222. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  223. NULL, GetLastError(),
  224. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  225. (LPTSTR)&lpMsgBuf, 0, NULL
  226. );
  227. // Display the string.
  228. //MessageBox(NULL, (const wchar_t*)lpMsgBuf, L"GetLastError", MB_OK | MB_ICONINFORMATION);
  229. // Free the buffer.
  230. LocalFree(lpMsgBuf);
  231. }
  232. void CShadowUI::Update(HWND hParent)
  233. {
  234. if(!m_bIsShowShadow || !(m_Status & SS_VISABLE)) return;
  235. RECT WndRect;
  236. GetWindowRect(hParent, &WndRect);
  237. int nShadWndWid;
  238. int nShadWndHei;
  239. if (m_bIsImageMode) {
  240. if(m_sShadowImage.IsEmpty()) return;
  241. nShadWndWid = WndRect.right - WndRect.left + m_nSize * 2;
  242. nShadWndHei = WndRect.bottom - WndRect.top + m_nSize * 2;
  243. }
  244. else {
  245. if (m_nSize == 0) return;
  246. nShadWndWid = WndRect.right - WndRect.left + m_nSize * 2;
  247. nShadWndHei = WndRect.bottom - WndRect.top + m_nSize * 2;
  248. }
  249. // Create the alpha blending bitmap
  250. BITMAPINFO bmi; // bitmap header
  251. ZeroMemory(&bmi, sizeof(BITMAPINFO));
  252. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  253. bmi.bmiHeader.biWidth = nShadWndWid;
  254. bmi.bmiHeader.biHeight = nShadWndHei;
  255. bmi.bmiHeader.biPlanes = 1;
  256. bmi.bmiHeader.biBitCount = 32; // four 8-bit components
  257. bmi.bmiHeader.biCompression = BI_RGB;
  258. bmi.bmiHeader.biSizeImage = nShadWndWid * nShadWndHei * 4;
  259. BYTE *pvBits; // pointer to DIB section
  260. HBITMAP hbitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pvBits, NULL, 0);
  261. if (hbitmap == NULL) {
  262. GetLastErrorMessage();
  263. }
  264. HDC hMemDC = CreateCompatibleDC(NULL);
  265. if (hMemDC == NULL) {
  266. GetLastErrorMessage();
  267. }
  268. HBITMAP hOriBmp = (HBITMAP)SelectObject(hMemDC, hbitmap);
  269. if (GetLastError()!=0) {
  270. GetLastErrorMessage();
  271. }
  272. if (m_bIsImageMode) {
  273. RECT rcPaint = {0, 0, nShadWndWid, nShadWndHei};
  274. const TDrawInfo* pDrawInfo = m_pManager->GetDrawInfo(m_sShadowImage, _T(""));
  275. if(pDrawInfo != NULL) {
  276. const TImageInfo* data = m_pManager->GetImageEx(pDrawInfo->sImageName, pDrawInfo->sResType, pDrawInfo->dwMask, pDrawInfo->bHSL, pDrawInfo->bGdiplus);
  277. if( !data ) return;
  278. RECT rcBmpPart = {0};
  279. rcBmpPart.right = data->nX;
  280. rcBmpPart.bottom = data->nY;
  281. RECT corner = m_rcShadowCorner;
  282. CRenderEngine::DrawImage(hMemDC, data->hBitmap, rcPaint, rcPaint, rcBmpPart, corner, data->bAlpha, 0xFF, true, false, false);
  283. }
  284. }
  285. else {
  286. ZeroMemory(pvBits, bmi.bmiHeader.biSizeImage);
  287. MakeShadow((UINT32 *)pvBits, hParent, &WndRect);
  288. }
  289. POINT ptDst;
  290. if (m_bIsImageMode) {
  291. ptDst.x = WndRect.left - m_nSize;
  292. ptDst.y = WndRect.top - m_nSize;
  293. }
  294. else {
  295. ptDst.x = WndRect.left + m_nxOffset - m_nSize;
  296. ptDst.y = WndRect.top + m_nyOffset - m_nSize;
  297. }
  298. POINT ptSrc = {0, 0};
  299. SIZE WndSize = {nShadWndWid, nShadWndHei};
  300. BLENDFUNCTION blendPixelFunction= { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  301. MoveWindow(m_hWnd, ptDst.x, ptDst.y, nShadWndWid, nShadWndHei, FALSE);
  302. BOOL bRet= ::UpdateLayeredWindow(m_hWnd, NULL, &ptDst, &WndSize, hMemDC, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA);
  303. _ASSERT(bRet); // something was wrong....
  304. // Delete used resources
  305. SelectObject(hMemDC, hOriBmp);
  306. DeleteObject(hbitmap);
  307. DeleteDC(hMemDC);
  308. }
  309. // ---------------------------------------------------------------------------
  310. // [2026-09-25] R25 - anti-aliased inner hole for the shadow.
  311. //
  312. // Symptom: on a layered anti-aliased rounded window the top corners showed a
  313. // dark line, and the bottom corners showed a broken / too-thin shadow.
  314. //
  315. // Root cause: the hole punched into the shadow bitmap was cut with a 1-BIT
  316. // silhouette (PtInRegion over hParentRgn), while the window content itself is
  317. // cut with an 8x8 supersampled alpha mask (CPaintManagerUI::
  318. // FillRoundRectAlphaMask). The two outlines disagree by up to 1px along the
  319. // corner arc, and the shadow window is composited ABOVE the parent window, so:
  320. // * scan lines where the 1-bit arc sits INSIDE the real arc keep shadow on
  321. // pixels the window already covers fully -> they get darkened. On a
  322. // saturated title bar that reads as a black line on the top corners.
  323. // * scan lines where the 1-bit arc sits OUTSIDE the real arc zero out shadow
  324. // that belongs to the outside of the arc -> the corner shadow is bitten
  325. // off, which reads as broken / too thin shadow on the bottom corners.
  326. //
  327. // Fix: instead of the boolean hole, fade the shadow by the SAME coverage the
  328. // window mask uses. cov = 8x8 supersampled area coverage of the rounded rect at
  329. // that pixel (0..255), so:
  330. // cov == 255 (inside) -> alpha 0 (the hole, as before)
  331. // 0 < cov < 255 (arc) -> alpha * (255 - cov) / 255 (smooth + aligned)
  332. // cov == 0 (outside) -> alpha unchanged
  333. // Straight edges are unaffected: coverage there is always 255 or 0, never
  334. // partial, so the top / bottom / left / right shadow bands keep their exact
  335. // current values (verified: 180 / 205 / 231 on all four sides).
  336. //
  337. // NOTE: this file is compiled without a UTF-8 BOM - keep every comment ASCII.
  338. // ---------------------------------------------------------------------------
  339. // Exact port of CPaintManagerUI::FillRoundRectAlphaMask's coverage maths
  340. // (same pixel-centre sampling, same corner centre, same 8x8 supersampling),
  341. // otherwise the two outlines would keep drifting apart by half a pixel.
  342. static int ShadowRoundRectCoverage(int x, int y, int w, int h, int r)
  343. {
  344. if (r <= 0 || w <= 0 || h <= 0) return 255;
  345. if (r > w / 2) r = w / 2;
  346. if (r > h / 2) r = h / 2;
  347. if (r < 1) return 255;
  348. if (x < 0 || y < 0 || x >= w || y >= h) return 0;
  349. const double cx = w * 0.5;
  350. const double cy = h * 0.5;
  351. const double hw = cx;
  352. const double hh = cy;
  353. const double px = fabs(x + 0.5 - cx) - (hw - r);
  354. const double py = fabs(y + 0.5 - cy) - (hh - r);
  355. // Inside band / straight edge: the window boundary IS the mask boundary.
  356. if (px <= 0.0 || py <= 0.0) return 255;
  357. // Corner block: corner centre = the inner rect's corner point.
  358. // Getting the sign wrong moves the centre to the diagonally opposite side of
  359. // the window, every sample falls outside and the whole corner reads 0.
  360. const double ccx = cx + ((x + 0.5 < cx) ? -(hw - r) : (hw - r));
  361. const double ccy = cy + ((y + 0.5 < cy) ? -(hh - r) : (hh - r));
  362. const double rr = (double)r * r;
  363. int nIn = 0;
  364. for (int sy = 0; sy < 8; ++sy)
  365. {
  366. const double syp = y + (sy + 0.5) / 8.0;
  367. for (int sx = 0; sx < 8; ++sx)
  368. {
  369. const double sxp = x + (sx + 0.5) / 8.0;
  370. const double ddx = sxp - ccx;
  371. const double ddy = syp - ccy;
  372. if (ddx * ddx + ddy * ddy <= rr) ++nIn;
  373. }
  374. }
  375. return (nIn * 255 + 32) / 64;
  376. }
  377. // [2026-09-26 R31] One-key rollback for the corner colour compensation below.
  378. // Default ON. Set the environment variable ZP_SHADOW_COLOR_COMP=0 (before start-up)
  379. // to fall back to the plain linear alpha attenuation R28 introduced.
  380. static bool ShadowCornerColorCompEnabled()
  381. {
  382. static int s_nEnabled = -1;
  383. if (s_nEnabled < 0)
  384. {
  385. TCHAR szBuf[8] = { 0 };
  386. const DWORD dwRet = ::GetEnvironmentVariable(_T("ZP_SHADOW_COLOR_COMP"), szBuf, 8);
  387. s_nEnabled = (dwRet == 1 && szBuf[0] == _T('0')) ? 0 : 1;
  388. }
  389. return s_nEnabled != 0;
  390. }
  391. void CShadowUI::MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent)
  392. {
  393. // The shadow algorithm:
  394. // Get the region of parent window,
  395. // Apply morphologic erosion to shrink it into the size (ShadowWndSize - Sharpness)
  396. // Apply modified (with blur effect) morphologic dilation to make the blurred border
  397. // The algorithm is optimized by assuming parent window is just "one piece" and without "wholes" on it
  398. // Get the region of parent window.
  399. //
  400. // [2026-09-24] In layered anti-aliased rounded-corner mode the window deliberately has
  401. // NO window region: CPaintManagerUI::GetRoundCorner() returns {0,0} so that every
  402. // window's OnSize skips CreateRoundRectRgn/SetWindowRgn (a 1-bit HRGN would re-cut the
  403. // smooth anti-aliased corner produced by the alpha mask). Consequence: GetWindowRgn
  404. // below cannot read the real shape, hParentRgn keeps the plain full rectangle, so the
  405. // shadow's inner silhouette degrades into a right angle. In the four corners of a
  406. // rounded window the background then shows through inside a SQUARE shadow frame
  407. // (looks like a "white corner").
  408. // Fix: when no valid region is available, synthesise a rounded region from the window's
  409. // configured corner radius. radius == 0 keeps the old rectangle behaviour, and windows
  410. // that DO have a region (GetWindowRgn returns SIMPLEREGION/COMPLEXREGION) are untouched.
  411. const int nParentW = abs(rcParent->right - rcParent->left);
  412. const int nParentH = abs(rcParent->bottom - rcParent->top);
  413. // [2026-09-25] R25: radius used by the anti-aliased hole further down. Kept
  414. // separate from szRgnCorner because that one is only applied when the radius
  415. // fits inside the window; here we only need "is there a radius at all"
  416. // (0 == no rounded corner == plain rectangle hole, i.e. the old behaviour).
  417. SIZE szAaCorner = { 0, 0 };
  418. if (m_pManager != NULL) szAaCorner = m_pManager->GetRoundCornerForShadow();
  419. HRGN hParentRgn = CreateRectRgn(0, 0, nParentW, nParentH);
  420. const int nRgnType = ::GetWindowRgn(hParent, hParentRgn);
  421. if (nRgnType == ERROR || nRgnType == NULLREGION)
  422. {
  423. SIZE szRgnCorner = { 0, 0 };
  424. if (m_pManager != NULL) szRgnCorner = m_pManager->GetRoundCornerForShadow();
  425. if (szRgnCorner.cx > 0 && szRgnCorner.cy > 0
  426. && szRgnCorner.cx * 2 < nParentW && szRgnCorner.cy * 2 < nParentH)
  427. {
  428. ::DeleteObject(hParentRgn);
  429. // [2026-09-24] R17: CreateRoundRectRgn takes 2x radius.
  430. // CreateRoundRectRgn's nWidthEllipse/nHeightEllipse is the FULL width/height of
  431. // the corner ellipse (radius = value / 2), while GetRoundCornerForShadow() returns
  432. // the RADIUS (same unit as CPaintManagerUI::FillRoundRectAlphaMask, which uses the
  433. // value directly as a radius). Feeding the radius here produced a shadow silhouette
  434. // with only HALF the window's corner radius: the dark band cut across at a smaller
  435. // arc and left a sliver of unshadowed background glued to the real arc (a "white
  436. // edge" on light backgrounds, a stray shadow blob on dark ones).
  437. // [2026-09-25] R25: dropped the old "+1". CreateRoundRectRgn(0,0,W,H,..)
  438. // already covers pixel columns [0,W) and rows [0,H) - exactly the pixel grid
  439. // the scan loop samples with PtInRegion(j,i). The +1 pushed the right and
  440. // bottom arcs 1px further out than the window's own mask arc, which made the
  441. // bottom-right corner shadow heavier than the other three corners.
  442. hParentRgn = ::CreateRoundRectRgn(0, 0, nParentW, nParentH,
  443. szRgnCorner.cx * 2, szRgnCorner.cy * 2);
  444. }
  445. }
  446. // Determine the Start and end point of each horizontal scan line
  447. SIZE szParent = {rcParent->right - rcParent->left, rcParent->bottom - rcParent->top};
  448. SIZE szShadow = {szParent.cx + 2 * m_nSize, szParent.cy + 2 * m_nSize};
  449. // Extra 2 lines (set to be empty) in ptAnchors are used in dilation
  450. int nAnchors = max(szParent.cy, szShadow.cy); // # of anchor points pares
  451. int (*ptAnchors)[2] = new int[nAnchors + 2][2];
  452. int (*ptAnchorsOri)[2] = new int[szParent.cy][2]; // anchor points, will not modify during erosion
  453. // [2026-10-09] R55: zero both arrays. The scan loop below only writes the rows it
  454. // can resolve; a row with no pixel inside hParentRgn leaves its [1] / [0] pair
  455. // untouched (see the "Start point not found" branch further down). Memory from
  456. // new[] is not zeroed, so those slots used to carry arbitrary values which then
  457. // leaked into the fill loops below as loop bounds.
  458. ::ZeroMemory(ptAnchors, sizeof(int) * 2 * (nAnchors + 2));
  459. ::ZeroMemory(ptAnchorsOri, sizeof(int) * 2 * szParent.cy);
  460. ptAnchors[0][0] = szParent.cx;
  461. ptAnchors[0][1] = 0;
  462. ptAnchors[nAnchors + 1][0] = szParent.cx;
  463. ptAnchors[nAnchors + 1][1] = 0;
  464. if(m_nSize > 0) {
  465. // Put the parent window anchors at the center
  466. for(int i = 0; i < m_nSize; i++) {
  467. ptAnchors[i + 1][0] = szParent.cx;
  468. ptAnchors[i + 1][1] = 0;
  469. ptAnchors[szShadow.cy - i][0] = szParent.cx;
  470. ptAnchors[szShadow.cy - i][1] = 0;
  471. }
  472. ptAnchors += m_nSize;
  473. }
  474. for(int i = 0; i < szParent.cy; i++) {
  475. // find start point
  476. int j;
  477. for(j = 0; j < szParent.cx; j++) {
  478. if(PtInRegion(hParentRgn, j, i)) {
  479. ptAnchors[i + 1][0] = j + m_nSize;
  480. ptAnchorsOri[i][0] = j;
  481. break;
  482. }
  483. }
  484. // Start point not found
  485. if(j >= szParent.cx) {
  486. // [2026-10-09] R55: the 2nd and 3rd line used to duplicate the 1st and 2nd,
  487. // so ptAnchors[i + 1][1] and ptAnchorsOri[i][0] were never written at all.
  488. // Empty rows are not theoretical here: a rounded window region built by
  489. // CreateRoundRectRgn has NO pixel on its very last row, so this branch runs
  490. // on every shadow repaint of a roundcorner window. The uninitialised pair
  491. // then acted as the bound of the clCenter fill below, which ran past the
  492. // end of the DIB section (crash: access violation on write).
  493. ptAnchors[i + 1][0] = szParent.cx;
  494. ptAnchorsOri[i][0] = szParent.cx;
  495. ptAnchors[i + 1][1] = 0;
  496. ptAnchorsOri[i][1] = 0;
  497. }
  498. else {
  499. // find end point
  500. for(j = szParent.cx - 1; j >= ptAnchors[i + 1][0]; j--) {
  501. if(PtInRegion(hParentRgn, j, i)) {
  502. ptAnchors[i + 1][1] = j + 1 + m_nSize;
  503. ptAnchorsOri[i][1] = j + 1;
  504. break;
  505. }
  506. }
  507. }
  508. }
  509. if(m_nSize > 0)
  510. ptAnchors -= m_nSize; // Restore pos of ptAnchors for erosion
  511. int (*ptAnchorsTmp)[2] = new int[nAnchors + 2][2]; // Store the result of erosion
  512. // First and last line should be empty
  513. ptAnchorsTmp[0][0] = szParent.cx;
  514. ptAnchorsTmp[0][1] = 0;
  515. ptAnchorsTmp[nAnchors + 1][0] = szParent.cx;
  516. ptAnchorsTmp[nAnchors + 1][1] = 0;
  517. int nEroTimes = 0;
  518. // morphologic erosion
  519. for(int i = 0; i < m_nSharpness - m_nSize; i++) {
  520. nEroTimes++;
  521. //ptAnchorsTmp[1][0] = szParent.cx;
  522. //ptAnchorsTmp[1][1] = 0;
  523. //ptAnchorsTmp[szParent.cy + 1][0] = szParent.cx;
  524. //ptAnchorsTmp[szParent.cy + 1][1] = 0;
  525. for(int j = 1; j < nAnchors + 1; j++) {
  526. ptAnchorsTmp[j][0] = max(ptAnchors[j - 1][0], max(ptAnchors[j][0], ptAnchors[j + 1][0])) + 1;
  527. ptAnchorsTmp[j][1] = min(ptAnchors[j - 1][1], min(ptAnchors[j][1], ptAnchors[j + 1][1])) - 1;
  528. }
  529. // Exchange ptAnchors and ptAnchorsTmp;
  530. int (*ptAnchorsXange)[2] = ptAnchorsTmp;
  531. ptAnchorsTmp = ptAnchors;
  532. ptAnchors = ptAnchorsXange;
  533. }
  534. // morphologic dilation
  535. ptAnchors += (m_nSize < 0 ? -m_nSize : 0) + 1; // now coordinates in ptAnchors are same as in shadow window
  536. // [2026-10-09] R55: last line of defence. Both anchor arrays are consumed directly
  537. // as x bounds of the clCenter / kernel loops below, so clamp every span into the
  538. // DIB row before it is used. Healthy values already live in [0, szShadow.cx], so
  539. // this is a no-op on every correct path; it only bites when something still slips
  540. // through, and it turns "write past the end of a 620x544 DIB" into "write nothing".
  541. // The +1 shift above maps index 0..nAnchorCnt onto the original 1..nAnchorCnt+1
  542. // slots, which is exactly the index range both loops touch.
  543. const int nAnchorCnt = nAnchors - (m_nSize < 0 ? -m_nSize : 0);
  544. for(int i = 0; i <= nAnchorCnt; i++) {
  545. if(ptAnchors[i][0] < 0) ptAnchors[i][0] = 0;
  546. else if(ptAnchors[i][0] > szShadow.cx) ptAnchors[i][0] = szShadow.cx;
  547. if(ptAnchors[i][1] < 0) ptAnchors[i][1] = 0;
  548. else if(ptAnchors[i][1] > szShadow.cx) ptAnchors[i][1] = szShadow.cx;
  549. }
  550. for(int i = 0; i < szParent.cy; i++) {
  551. if(ptAnchorsOri[i][0] < 0) ptAnchorsOri[i][0] = 0;
  552. else if(ptAnchorsOri[i][0] > szShadow.cx) ptAnchorsOri[i][0] = szShadow.cx;
  553. if(ptAnchorsOri[i][1] < 0) ptAnchorsOri[i][1] = 0;
  554. else if(ptAnchorsOri[i][1] > szShadow.cx) ptAnchorsOri[i][1] = szShadow.cx;
  555. }
  556. // Generate the kernel
  557. int nKernelSize = m_nSize > m_nSharpness ? m_nSize : m_nSharpness;
  558. int nCenterSize = m_nSize > m_nSharpness ? (m_nSize - m_nSharpness) : 0;
  559. UINT32 *pKernel = new UINT32[(2 * nKernelSize + 1) * (2 * nKernelSize + 1)];
  560. UINT32 *pKernelIter = pKernel;
  561. for(int i = 0; i <= 2 * nKernelSize; i++) {
  562. for(int j = 0; j <= 2 * nKernelSize; j++) {
  563. double dLength = sqrt((i - nKernelSize) * (i - nKernelSize) + (j - nKernelSize) * (double)(j - nKernelSize));
  564. if(dLength < nCenterSize)
  565. *pKernelIter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  566. else if(dLength <= nKernelSize) {
  567. UINT32 nFactor = ((UINT32)((1 - (dLength - nCenterSize) / (m_nSharpness + 1)) * m_nDarkness));
  568. *pKernelIter = nFactor << 24 | PreMultiply(m_Color, nFactor);
  569. }
  570. else
  571. *pKernelIter = 0;
  572. //TRACE("%d ", *pKernelIter >> 24);
  573. pKernelIter ++;
  574. }
  575. //TRACE("\n");
  576. }
  577. // Generate blurred border
  578. for(int i = nKernelSize; i < szShadow.cy - nKernelSize; i++)
  579. {
  580. int j;
  581. if(ptAnchors[i][0] < ptAnchors[i][1])
  582. {
  583. // Start of line
  584. for(j = ptAnchors[i][0]; j < min(max(ptAnchors[i - 1][0], ptAnchors[i + 1][0]) + 1, ptAnchors[i][1]); j++)
  585. {
  586. for(int k = 0; k <= 2 * nKernelSize; k++)
  587. {
  588. UINT32 *pPixel = pShadBits +
  589. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  590. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  591. for(int l = 0; l <= 2 * nKernelSize; l++)
  592. {
  593. if(*pPixel < *pKernelPixel)
  594. *pPixel = *pKernelPixel;
  595. pPixel++;
  596. pKernelPixel++;
  597. }
  598. }
  599. } // for() start of line
  600. // End of line
  601. for(j = max(j, min(ptAnchors[i - 1][1], ptAnchors[i + 1][1]) - 1); j < ptAnchors[i][1]; j++)
  602. {
  603. for(int k = 0; k <= 2 * nKernelSize; k++)
  604. {
  605. UINT32 *pPixel = pShadBits +
  606. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  607. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  608. for(int l = 0; l <= 2 * nKernelSize; l++)
  609. {
  610. if(*pPixel < *pKernelPixel)
  611. *pPixel = *pKernelPixel;
  612. pPixel++;
  613. pKernelPixel++;
  614. }
  615. }
  616. } // for() end of line
  617. }
  618. } // for() Generate blurred border
  619. // Erase unwanted parts and complement missing
  620. UINT32 clCenter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  621. const int nAaRadius = (szAaCorner.cx > 0) ? szAaCorner.cx : 0;
  622. 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++) {
  623. UINT32 *pLine = pShadBits + (szShadow.cy - i - 1) * szShadow.cx;
  624. // Line is not covered by parent window
  625. if(i - m_nSize + m_nyOffset < 0 || i - m_nSize + m_nyOffset >= szParent.cy) {
  626. for(int j = ptAnchors[i][0]; j < ptAnchors[i][1]; j++) {
  627. *(pLine + j) = clCenter;
  628. }
  629. }
  630. else {
  631. for(int j = ptAnchors[i][0]; j < min(ptAnchorsOri[i - m_nSize + m_nyOffset][0] + m_nSize - m_nxOffset, ptAnchors[i][1]); j++)
  632. *(pLine + j) = clCenter;
  633. for(int j = max(ptAnchorsOri[i - m_nSize + m_nyOffset][1] + m_nSize - m_nxOffset, ptAnchors[i][0]); j < ptAnchors[i][1]; j++)
  634. *(pLine + j) = clCenter;
  635. // [2026-09-25] R25: the anti-aliased hole replaces the old 1-bit
  636. // "*(pLine + j) = 0" loop. It must stay AFTER the two clCenter fill
  637. // passes above, otherwise those passes would overwrite the fade.
  638. // Scope is the whole parent-rect span of this line (not just the
  639. // ptAnchorsOri span), so the partially covered pixels that the 1-bit
  640. // silhouette classified as "outside" are faded too.
  641. {
  642. const int nHoleX0 = max(m_nSize - m_nxOffset, 0);
  643. const int nHoleX1 = min(szParent.cx + m_nSize - m_nxOffset, szShadow.cx);
  644. const int nHoleY = i - m_nSize + m_nyOffset;
  645. for(int j = nHoleX0; j < nHoleX1; j++)
  646. {
  647. UINT32 nPixel = *(pLine + j);
  648. if(nPixel == 0) continue;
  649. const int nCov = ShadowRoundRectCoverage(j - (m_nSize - m_nxOffset), nHoleY,
  650. szParent.cx, szParent.cy, nAaRadius);
  651. if(nCov >= 255) { *(pLine + j) = 0; continue; }
  652. if(nCov <= 0) continue;
  653. UINT32 nAlpha = ((nPixel >> 24) * (255 - nCov)) / 255;
  654. *(pLine + j) = (nAlpha == 0) ? 0
  655. : ((nAlpha << 24) | PreMultiply(m_Color, (unsigned char)nAlpha));
  656. }
  657. }
  658. }
  659. }
  660. // -----------------------------------------------------------------------
  661. // [2026-09-26] R26 - corner shadow top-up.
  662. //
  663. // The morphological dilation above grows the silhouette by
  664. // (m_nSharpness - m_nSize) erosion steps and then dilates with a radius of
  665. // max(m_nSize, m_nSharpness). Along a straight edge that lands exactly
  666. // m_nSize px outside the window - which is why the four straight bands carry
  667. // the expected 75 / 50 / 25 alpha. Around a CONVEX corner it does not: every
  668. // erosion step takes max() over the three neighbouring rows, so on the corner
  669. // arc it tightens faster than the arc itself, and the band left after the
  670. // dilation is 1..3 px too narrow there. On a white background that reads as
  671. // "the shadow is missing at the bottom corners".
  672. //
  673. // Fix: after the erase pass, top up every pixel that is FULLY outside the
  674. // window (coverage == 0) using the analytic distance to the rounded rect and
  675. // the very same radial profile the kernel uses. Straight edges are untouched
  676. // - they already carry the profile value and the update is a max(). Pixels the
  677. // window covers even partially are skipped, so the hole stays a hole.
  678. // Note the ordering: this must run AFTER the erase/hole pass above, otherwise
  679. // the erase would wipe the corner band again.
  680. // -----------------------------------------------------------------------
  681. if (nAaRadius > 0)
  682. {
  683. const int nEroSteps = (m_nSharpness > m_nSize) ? (int)(m_nSharpness - m_nSize) : 0;
  684. // [2026-09-26 R31] Parent window content x mask, premultiplied (the value the
  685. // compensation below needs). Only the layered rounded-mask path has it; every other
  686. // path, and any frame painted before the parent has painted at all, leaves it NULL
  687. // and the block below falls back to the old pure-black band.
  688. const bool bColorComp = ShadowCornerColorCompEnabled();
  689. COLORREF* pBkgBits = NULL;
  690. RECT rcBkg = { 0, 0, 0, 0 };
  691. int nBkgW = 0;
  692. int nBkgH = 0;
  693. if (bColorComp && m_pManager != NULL)
  694. pBkgBits = m_pManager->GetLayeredContentBits(&nBkgW, &nBkgH, &rcBkg);
  695. const double dCx = szParent.cx * 0.5;
  696. const double dCy = szParent.cy * 0.5;
  697. const double dHw = dCx;
  698. const double dHh = dCy;
  699. const int nPad = nKernelSize + 2;
  700. for (int iy = -nPad; iy <= szParent.cy + nPad; ++iy)
  701. {
  702. const int i = iy + m_nSize - m_nyOffset;
  703. if (i < 0 || i >= szShadow.cy) continue;
  704. UINT32 *pLineTop = pShadBits + (szShadow.cy - i - 1) * szShadow.cx;
  705. const double dPy = fabs(iy + 0.5 - dCy) - (dHh - nAaRadius);
  706. for (int jx = -nPad; jx <= szParent.cx + nPad; ++jx)
  707. {
  708. const int j = jx + m_nSize - m_nxOffset;
  709. if (j < 0 || j >= szShadow.cx) continue;
  710. // Straight-band interior (the bulk of the window): nothing to do.
  711. const double dPx = fabs(jx + 0.5 - dCx) - (dHw - nAaRadius);
  712. if (dPx <= 0.0 && dPy <= 0.0) continue;
  713. // [2026-09-26 R28] Coverage of the window's own anti-aliased mask at
  714. // this pixel. The old guard was 'coverage != 0 - skip', which is why
  715. // the corner arc still looked wrong. Along a straight edge coverage is
  716. // only ever 0 or 255, but the corner arc carries a one pixel ring where
  717. // coverage is strictly between the two. Those pixels were skipped here,
  718. // kept whatever the too tight morphological band had produced, and
  719. // showed up as a bright dash hugging the arc: a pale seam between the
  720. // title bar and the shadow on the top corners, a stair stepped dashed
  721. // edge on the bottom ones. The bottom corners had twice as many such
  722. // pixels and twice the gap, which is why the two ends of the window
  723. // looked like two different defects. Now only a FULLY covered pixel is
  724. // skipped, so the hole R25 punched stays a hole; partially covered
  725. // pixels are topped up and attenuated further down by the fraction of
  726. // the pixel the window does not cover.
  727. const int nCov = ShadowRoundRectCoverage(jx, iy, szParent.cx, szParent.cy, nAaRadius);
  728. if (nCov >= 255) continue; // fully covered: the hole stays a hole
  729. double d;
  730. // [2026-09-25 R27] Calibrate the corner band so it joins the four
  731. // straight edges seamlessly:
  732. // * the two straight-edge branches below used to return the distance
  733. // to the straight-band inner boundary instead of to the window
  734. // edge, i.e. nAaRadius too small a deduction. d stayed above
  735. // nKernelSize so the top-up never fired along the four edges;
  736. // those pixels kept the base band while the corner arc got the
  737. // full analytic value -- the arc sat +12 alpha above the band at
  738. // every distance and reached b=3.0 instead of 2.5, so every corner
  739. // showed a hard seam plus a fatter, darker hook.
  740. // * with all three branches corrected d is simply the rounded-rect
  741. // signed distance field, so this top-up becomes a pure function of
  742. // the distance and the whole band matches the four edges exactly.
  743. // * u carries +0.5 because the base band is calibrated as
  744. // K of d plus 2.5: 1/2/3 px outside -> alpha 75/50/25. Without
  745. // it the arc peaked at 87 instead of 75.
  746. if (dPx <= 0.0) d = dPy - nAaRadius; // nearest horizontal edge
  747. else if (dPy <= 0.0) d = dPx - nAaRadius; // nearest vertical edge
  748. else d = sqrt(dPx * dPx + dPy * dPy) - nAaRadius; // nearest corner arc
  749. // [2026-09-26 R29] A partially covered pixel whose centre is still inside
  750. // the window, d <= 0, used to be skipped by the guard that stood here, and
  751. // that guard is what left a pale seam between the shadow and the window all
  752. // along the corner arcs -- most visible on the top left and top right
  753. // rounding of the main window and of the dialogs.
  754. //
  755. // Such a pixel is only partly covered by the window, but the part that is
  756. // NOT covered lies outside the window, between 0 and one pixel away from
  757. // the silhouette. That is exactly the rim the band profile is calibrated
  758. // for, so the band value here is the one at distance 0. Clamping to 0 keeps
  759. // the corner arc a pure function of the distance, the same shape R27 gave
  760. // to the fully outside pixels of the arc. The pixel is then combined below
  761. // by coverage -- R28 attenuated its alpha linearly, R31 instead keeps the
  762. // band alpha and replaces the black source with the coverage-corrected
  763. // content colour.
  764. //
  765. // Scope: only 0 < coverage < 255 can reach this line. A fully covered pixel
  766. // was skipped above so the hole stays a hole, and a fully outside one has
  767. // d > 0, so clamping is a no-op there. The four straight edges, the whole
  768. // outer arc and the hole are therefore left bit for bit untouched.
  769. if (d < 0.0) d = 0.0; // clamp to the window rim
  770. const double u = d + nEroSteps + 0.5;
  771. UINT32 nWant;
  772. if (u < (double)nCenterSize) nWant = m_nDarkness;
  773. else if (u <= (double)nKernelSize)
  774. nWant = (UINT32)((1.0 - (u - nCenterSize) / (double)(m_nSharpness + 1)) * m_nDarkness);
  775. else continue;
  776. if (nWant == 0) continue;
  777. // [2026-09-26 R28 -> R31] SUPERSEDED. This used to be a plain linear
  778. // attenuation, as = a * (255 - cov) / 255 with a black source. It is the
  779. // correct lower bound only when the content colour equals the desktop
  780. // colour, and it is still the fallback used below when the parent content
  781. // bitmap is unavailable, but on darker content it under-attenuated and left
  782. // the pale 1px arc along the top corners. R31 replaced it with the exact
  783. // coverage-corrected source colour.
  784. // [2026-09-26 R31] Exact colour compensation for the partially covered rim.
  785. //
  786. // What the window + shadow stack really does at a pixel the window only partly
  787. // covers (coverage c, content colour w, desktop D):
  788. // under = w*c + D*(1-c) window composited onto the desktop
  789. // out = under*(1-as) + S shadow window composited on top of it
  790. // and what it SHOULD look like is the shadow darkening only the part of the pixel
  791. // the window does not cover:
  792. // ideal = w*c + D*(1-c)*(1-a)
  793. // Solving out == ideal with as = a (the band value, undiminished) gives
  794. // S = a*w*c
  795. // which is independent of D -- so the ring no longer depends on what sits behind
  796. // the window. The rule that used to stand here was as = a*(1-c) with S = 0, i.e. a
  797. // plain black shadow thinned by the coverage; that is the correct lower bound only
  798. // when w == D. For darker content (the green title bar, w ~ 96..131 on a bright
  799. // desktop) it under-attenuates by
  800. // delta = a * (1-c) * (D-w) * c / 255
  801. // which is 10..14 of 255 on the top corners of this app: the pale 1px arc between
  802. // the shadow and the rounded window that R26..R30 could only shrink, never remove.
  803. //
  804. // w*c is exactly the premultiplied content pixel CPaintManagerUI keeps in
  805. // m_pBackgroundBits for the layered rounded-mask path, so the whole fix is "read
  806. // that pixel, multiply it by the band alpha, and do not dim the alpha". Straight
  807. // edges and the outer arc have c == 0, hence S == 0, hence keep the old black band
  808. // bit for bit. A fully covered pixel (c == 255) is the hole and never gets here.
  809. UINT32 nRgb = PreMultiply(m_Color, (unsigned char)nWant);
  810. if (nCov > 0 && pBkgBits != NULL)
  811. {
  812. const LONG nBx = jx - rcBkg.left;
  813. const LONG nBy = iy - rcBkg.top;
  814. if (nBx >= 0 && nBx < nBkgW && nBy >= 0 && nBy < nBkgH)
  815. {
  816. // bottom-up DIB: window row nBy lives at memory row (nBkgH - 1 - nBy)
  817. const UINT32 nPc = ((UINT32)pBkgBits[(LONG)(nBkgH - 1 - nBy) * nBkgW + nBx]) & 0x00FFFFFFu;
  818. if (nPc != 0)
  819. {
  820. const UINT32 nRc = (((nPc >> 16) & 0xFFu) * nWant + 127) / 255;
  821. const UINT32 nGc = (((nPc >> 8) & 0xFFu) * nWant + 127) / 255;
  822. const UINT32 nBc = ((nPc & 0xFFu) * nWant + 127) / 255;
  823. nRgb = (nRc << 16) | (nGc << 8) | nBc;
  824. }
  825. }
  826. }
  827. if (nWant == 0) continue;
  828. if ((*(pLineTop + j) >> 24) < nWant)
  829. *(pLineTop + j) = (nWant << 24) | nRgb;
  830. }
  831. }
  832. }
  833. // Delete used resources
  834. delete[] (ptAnchors - (m_nSize < 0 ? -m_nSize : 0) - 1);
  835. delete[] ptAnchorsTmp;
  836. delete[] ptAnchorsOri;
  837. delete[] pKernel;
  838. DeleteObject(hParentRgn);
  839. }
  840. void CShadowUI::ShowShadow(bool bShow)
  841. {
  842. m_bIsShowShadow = bShow;
  843. }
  844. bool CShadowUI::IsShowShadow() const
  845. {
  846. return m_bIsShowShadow;
  847. }
  848. void CShadowUI::DisableShadow(bool bDisable)
  849. {
  850. m_bIsDisableShadow = bDisable;
  851. if (m_hWnd != NULL) {
  852. if (m_bIsDisableShadow) {
  853. ::ShowWindow(m_hWnd, SW_HIDE);
  854. }
  855. else {
  856. // Determine the initial show state of shadow according to parent window's state
  857. LONG lParentStyle = GetWindowLongPtr(GetParent(m_hWnd), GWL_STYLE);
  858. if (!(WS_VISIBLE & lParentStyle)) // Parent invisible
  859. m_Status = SS_ENABLED;
  860. else if ((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow
  861. m_Status = SS_ENABLED | SS_PARENTVISIBLE;
  862. else {
  863. m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE;
  864. }
  865. if ((WS_VISIBLE & lParentStyle) && !((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle)) {
  866. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  867. Update(GetParent(m_hWnd));
  868. }
  869. }
  870. }
  871. }
  872. ////TODO shadow disnable fix////
  873. bool CShadowUI::IsDisableShadow() const
  874. {
  875. return m_bIsDisableShadow;
  876. }
  877. bool CShadowUI::SetSize(int NewSize)
  878. {
  879. if(NewSize > 35 || NewSize < -35)
  880. return false;
  881. m_nSize = (signed char)NewSize;
  882. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  883. Update(GetParent(m_hWnd));
  884. return true;
  885. }
  886. bool CShadowUI::SetSharpness(unsigned int NewSharpness)
  887. {
  888. if(NewSharpness > 35)
  889. return false;
  890. m_nSharpness = (unsigned char)NewSharpness;
  891. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  892. Update(GetParent(m_hWnd));
  893. return true;
  894. }
  895. bool CShadowUI::SetDarkness(unsigned int NewDarkness)
  896. {
  897. if(NewDarkness > 255)
  898. return false;
  899. m_nDarkness = (unsigned char)NewDarkness;
  900. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  901. Update(GetParent(m_hWnd));
  902. return true;
  903. }
  904. bool CShadowUI::SetPosition(int NewXOffset, int NewYOffset)
  905. {
  906. if(NewXOffset > 35 || NewXOffset < -35 ||
  907. NewYOffset > 35 || NewYOffset < -35)
  908. return false;
  909. m_nxOffset = (signed char)NewXOffset;
  910. m_nyOffset = (signed char)NewYOffset;
  911. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  912. Update(GetParent(m_hWnd));
  913. return true;
  914. }
  915. bool CShadowUI::SetColor(COLORREF NewColor)
  916. {
  917. m_Color = NewColor;
  918. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  919. Update(GetParent(m_hWnd));
  920. return true;
  921. }
  922. bool CShadowUI::SetImage(LPCTSTR szImage)
  923. {
  924. if (szImage == NULL)
  925. return false;
  926. m_bIsImageMode = true;
  927. m_sShadowImage = szImage;
  928. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  929. Update(GetParent(m_hWnd));
  930. return true;
  931. }
  932. bool CShadowUI::SetShadowCorner(RECT rcCorner)
  933. {
  934. if (rcCorner.left < 0 || rcCorner.top < 0 || rcCorner.right < 0 || rcCorner.bottom < 0) return false;
  935. m_rcShadowCorner = rcCorner;
  936. if(m_hWnd != NULL && (SS_VISABLE & m_Status)) {
  937. Update(GetParent(m_hWnd));
  938. }
  939. return true;
  940. }
  941. bool CShadowUI::CopyShadow(CShadowUI* pShadow)
  942. {
  943. if (m_bIsImageMode) {
  944. pShadow->SetImage(m_sShadowImage);
  945. pShadow->SetShadowCorner(m_rcShadowCorner);
  946. pShadow->SetSize((int)m_nSize);
  947. }
  948. else {
  949. pShadow->SetSize((int)m_nSize);
  950. pShadow->SetSharpness((unsigned int)m_nSharpness);
  951. pShadow->SetDarkness((unsigned int)m_nDarkness);
  952. pShadow->SetColor(m_Color);
  953. pShadow->SetPosition((int)m_nxOffset, (int)m_nyOffset);
  954. }
  955. pShadow->ShowShadow(m_bIsShowShadow);
  956. return true;
  957. }
  958. } //namespace DuiLib