UIShadow.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. ptAnchors[0][0] = szParent.cx;
  454. ptAnchors[0][1] = 0;
  455. ptAnchors[nAnchors + 1][0] = szParent.cx;
  456. ptAnchors[nAnchors + 1][1] = 0;
  457. if(m_nSize > 0) {
  458. // Put the parent window anchors at the center
  459. for(int i = 0; i < m_nSize; i++) {
  460. ptAnchors[i + 1][0] = szParent.cx;
  461. ptAnchors[i + 1][1] = 0;
  462. ptAnchors[szShadow.cy - i][0] = szParent.cx;
  463. ptAnchors[szShadow.cy - i][1] = 0;
  464. }
  465. ptAnchors += m_nSize;
  466. }
  467. for(int i = 0; i < szParent.cy; i++) {
  468. // find start point
  469. int j;
  470. for(j = 0; j < szParent.cx; j++) {
  471. if(PtInRegion(hParentRgn, j, i)) {
  472. ptAnchors[i + 1][0] = j + m_nSize;
  473. ptAnchorsOri[i][0] = j;
  474. break;
  475. }
  476. }
  477. // Start point not found
  478. if(j >= szParent.cx) {
  479. ptAnchors[i + 1][0] = szParent.cx;
  480. ptAnchorsOri[i][1] = 0;
  481. ptAnchors[i + 1][0] = szParent.cx;
  482. ptAnchorsOri[i][1] = 0;
  483. }
  484. else {
  485. // find end point
  486. for(j = szParent.cx - 1; j >= ptAnchors[i + 1][0]; j--) {
  487. if(PtInRegion(hParentRgn, j, i)) {
  488. ptAnchors[i + 1][1] = j + 1 + m_nSize;
  489. ptAnchorsOri[i][1] = j + 1;
  490. break;
  491. }
  492. }
  493. }
  494. }
  495. if(m_nSize > 0)
  496. ptAnchors -= m_nSize; // Restore pos of ptAnchors for erosion
  497. int (*ptAnchorsTmp)[2] = new int[nAnchors + 2][2]; // Store the result of erosion
  498. // First and last line should be empty
  499. ptAnchorsTmp[0][0] = szParent.cx;
  500. ptAnchorsTmp[0][1] = 0;
  501. ptAnchorsTmp[nAnchors + 1][0] = szParent.cx;
  502. ptAnchorsTmp[nAnchors + 1][1] = 0;
  503. int nEroTimes = 0;
  504. // morphologic erosion
  505. for(int i = 0; i < m_nSharpness - m_nSize; i++) {
  506. nEroTimes++;
  507. //ptAnchorsTmp[1][0] = szParent.cx;
  508. //ptAnchorsTmp[1][1] = 0;
  509. //ptAnchorsTmp[szParent.cy + 1][0] = szParent.cx;
  510. //ptAnchorsTmp[szParent.cy + 1][1] = 0;
  511. for(int j = 1; j < nAnchors + 1; j++) {
  512. ptAnchorsTmp[j][0] = max(ptAnchors[j - 1][0], max(ptAnchors[j][0], ptAnchors[j + 1][0])) + 1;
  513. ptAnchorsTmp[j][1] = min(ptAnchors[j - 1][1], min(ptAnchors[j][1], ptAnchors[j + 1][1])) - 1;
  514. }
  515. // Exchange ptAnchors and ptAnchorsTmp;
  516. int (*ptAnchorsXange)[2] = ptAnchorsTmp;
  517. ptAnchorsTmp = ptAnchors;
  518. ptAnchors = ptAnchorsXange;
  519. }
  520. // morphologic dilation
  521. ptAnchors += (m_nSize < 0 ? -m_nSize : 0) + 1; // now coordinates in ptAnchors are same as in shadow window
  522. // Generate the kernel
  523. int nKernelSize = m_nSize > m_nSharpness ? m_nSize : m_nSharpness;
  524. int nCenterSize = m_nSize > m_nSharpness ? (m_nSize - m_nSharpness) : 0;
  525. UINT32 *pKernel = new UINT32[(2 * nKernelSize + 1) * (2 * nKernelSize + 1)];
  526. UINT32 *pKernelIter = pKernel;
  527. for(int i = 0; i <= 2 * nKernelSize; i++) {
  528. for(int j = 0; j <= 2 * nKernelSize; j++) {
  529. double dLength = sqrt((i - nKernelSize) * (i - nKernelSize) + (j - nKernelSize) * (double)(j - nKernelSize));
  530. if(dLength < nCenterSize)
  531. *pKernelIter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  532. else if(dLength <= nKernelSize) {
  533. UINT32 nFactor = ((UINT32)((1 - (dLength - nCenterSize) / (m_nSharpness + 1)) * m_nDarkness));
  534. *pKernelIter = nFactor << 24 | PreMultiply(m_Color, nFactor);
  535. }
  536. else
  537. *pKernelIter = 0;
  538. //TRACE("%d ", *pKernelIter >> 24);
  539. pKernelIter ++;
  540. }
  541. //TRACE("\n");
  542. }
  543. // Generate blurred border
  544. for(int i = nKernelSize; i < szShadow.cy - nKernelSize; i++)
  545. {
  546. int j;
  547. if(ptAnchors[i][0] < ptAnchors[i][1])
  548. {
  549. // Start of line
  550. for(j = ptAnchors[i][0]; j < min(max(ptAnchors[i - 1][0], ptAnchors[i + 1][0]) + 1, ptAnchors[i][1]); j++)
  551. {
  552. for(int k = 0; k <= 2 * nKernelSize; k++)
  553. {
  554. UINT32 *pPixel = pShadBits +
  555. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  556. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  557. for(int l = 0; l <= 2 * nKernelSize; l++)
  558. {
  559. if(*pPixel < *pKernelPixel)
  560. *pPixel = *pKernelPixel;
  561. pPixel++;
  562. pKernelPixel++;
  563. }
  564. }
  565. } // for() start of line
  566. // End of line
  567. for(j = max(j, min(ptAnchors[i - 1][1], ptAnchors[i + 1][1]) - 1); j < ptAnchors[i][1]; j++)
  568. {
  569. for(int k = 0; k <= 2 * nKernelSize; k++)
  570. {
  571. UINT32 *pPixel = pShadBits +
  572. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  573. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  574. for(int l = 0; l <= 2 * nKernelSize; l++)
  575. {
  576. if(*pPixel < *pKernelPixel)
  577. *pPixel = *pKernelPixel;
  578. pPixel++;
  579. pKernelPixel++;
  580. }
  581. }
  582. } // for() end of line
  583. }
  584. } // for() Generate blurred border
  585. // Erase unwanted parts and complement missing
  586. UINT32 clCenter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  587. const int nAaRadius = (szAaCorner.cx > 0) ? szAaCorner.cx : 0;
  588. 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++) {
  589. UINT32 *pLine = pShadBits + (szShadow.cy - i - 1) * szShadow.cx;
  590. // Line is not covered by parent window
  591. if(i - m_nSize + m_nyOffset < 0 || i - m_nSize + m_nyOffset >= szParent.cy) {
  592. for(int j = ptAnchors[i][0]; j < ptAnchors[i][1]; j++) {
  593. *(pLine + j) = clCenter;
  594. }
  595. }
  596. else {
  597. for(int j = ptAnchors[i][0]; j < min(ptAnchorsOri[i - m_nSize + m_nyOffset][0] + m_nSize - m_nxOffset, ptAnchors[i][1]); j++)
  598. *(pLine + j) = clCenter;
  599. for(int j = max(ptAnchorsOri[i - m_nSize + m_nyOffset][1] + m_nSize - m_nxOffset, ptAnchors[i][0]); j < ptAnchors[i][1]; j++)
  600. *(pLine + j) = clCenter;
  601. // [2026-09-25] R25: the anti-aliased hole replaces the old 1-bit
  602. // "*(pLine + j) = 0" loop. It must stay AFTER the two clCenter fill
  603. // passes above, otherwise those passes would overwrite the fade.
  604. // Scope is the whole parent-rect span of this line (not just the
  605. // ptAnchorsOri span), so the partially covered pixels that the 1-bit
  606. // silhouette classified as "outside" are faded too.
  607. {
  608. const int nHoleX0 = max(m_nSize - m_nxOffset, 0);
  609. const int nHoleX1 = min(szParent.cx + m_nSize - m_nxOffset, szShadow.cx);
  610. const int nHoleY = i - m_nSize + m_nyOffset;
  611. for(int j = nHoleX0; j < nHoleX1; j++)
  612. {
  613. UINT32 nPixel = *(pLine + j);
  614. if(nPixel == 0) continue;
  615. const int nCov = ShadowRoundRectCoverage(j - (m_nSize - m_nxOffset), nHoleY,
  616. szParent.cx, szParent.cy, nAaRadius);
  617. if(nCov >= 255) { *(pLine + j) = 0; continue; }
  618. if(nCov <= 0) continue;
  619. UINT32 nAlpha = ((nPixel >> 24) * (255 - nCov)) / 255;
  620. *(pLine + j) = (nAlpha == 0) ? 0
  621. : ((nAlpha << 24) | PreMultiply(m_Color, (unsigned char)nAlpha));
  622. }
  623. }
  624. }
  625. }
  626. // -----------------------------------------------------------------------
  627. // [2026-09-26] R26 - corner shadow top-up.
  628. //
  629. // The morphological dilation above grows the silhouette by
  630. // (m_nSharpness - m_nSize) erosion steps and then dilates with a radius of
  631. // max(m_nSize, m_nSharpness). Along a straight edge that lands exactly
  632. // m_nSize px outside the window - which is why the four straight bands carry
  633. // the expected 75 / 50 / 25 alpha. Around a CONVEX corner it does not: every
  634. // erosion step takes max() over the three neighbouring rows, so on the corner
  635. // arc it tightens faster than the arc itself, and the band left after the
  636. // dilation is 1..3 px too narrow there. On a white background that reads as
  637. // "the shadow is missing at the bottom corners".
  638. //
  639. // Fix: after the erase pass, top up every pixel that is FULLY outside the
  640. // window (coverage == 0) using the analytic distance to the rounded rect and
  641. // the very same radial profile the kernel uses. Straight edges are untouched
  642. // - they already carry the profile value and the update is a max(). Pixels the
  643. // window covers even partially are skipped, so the hole stays a hole.
  644. // Note the ordering: this must run AFTER the erase/hole pass above, otherwise
  645. // the erase would wipe the corner band again.
  646. // -----------------------------------------------------------------------
  647. if (nAaRadius > 0)
  648. {
  649. const int nEroSteps = (m_nSharpness > m_nSize) ? (int)(m_nSharpness - m_nSize) : 0;
  650. // [2026-09-26 R31] Parent window content x mask, premultiplied (the value the
  651. // compensation below needs). Only the layered rounded-mask path has it; every other
  652. // path, and any frame painted before the parent has painted at all, leaves it NULL
  653. // and the block below falls back to the old pure-black band.
  654. const bool bColorComp = ShadowCornerColorCompEnabled();
  655. COLORREF* pBkgBits = NULL;
  656. RECT rcBkg = { 0, 0, 0, 0 };
  657. int nBkgW = 0;
  658. int nBkgH = 0;
  659. if (bColorComp && m_pManager != NULL)
  660. pBkgBits = m_pManager->GetLayeredContentBits(&nBkgW, &nBkgH, &rcBkg);
  661. const double dCx = szParent.cx * 0.5;
  662. const double dCy = szParent.cy * 0.5;
  663. const double dHw = dCx;
  664. const double dHh = dCy;
  665. const int nPad = nKernelSize + 2;
  666. for (int iy = -nPad; iy <= szParent.cy + nPad; ++iy)
  667. {
  668. const int i = iy + m_nSize - m_nyOffset;
  669. if (i < 0 || i >= szShadow.cy) continue;
  670. UINT32 *pLineTop = pShadBits + (szShadow.cy - i - 1) * szShadow.cx;
  671. const double dPy = fabs(iy + 0.5 - dCy) - (dHh - nAaRadius);
  672. for (int jx = -nPad; jx <= szParent.cx + nPad; ++jx)
  673. {
  674. const int j = jx + m_nSize - m_nxOffset;
  675. if (j < 0 || j >= szShadow.cx) continue;
  676. // Straight-band interior (the bulk of the window): nothing to do.
  677. const double dPx = fabs(jx + 0.5 - dCx) - (dHw - nAaRadius);
  678. if (dPx <= 0.0 && dPy <= 0.0) continue;
  679. // [2026-09-26 R28] Coverage of the window's own anti-aliased mask at
  680. // this pixel. The old guard was 'coverage != 0 - skip', which is why
  681. // the corner arc still looked wrong. Along a straight edge coverage is
  682. // only ever 0 or 255, but the corner arc carries a one pixel ring where
  683. // coverage is strictly between the two. Those pixels were skipped here,
  684. // kept whatever the too tight morphological band had produced, and
  685. // showed up as a bright dash hugging the arc: a pale seam between the
  686. // title bar and the shadow on the top corners, a stair stepped dashed
  687. // edge on the bottom ones. The bottom corners had twice as many such
  688. // pixels and twice the gap, which is why the two ends of the window
  689. // looked like two different defects. Now only a FULLY covered pixel is
  690. // skipped, so the hole R25 punched stays a hole; partially covered
  691. // pixels are topped up and attenuated further down by the fraction of
  692. // the pixel the window does not cover.
  693. const int nCov = ShadowRoundRectCoverage(jx, iy, szParent.cx, szParent.cy, nAaRadius);
  694. if (nCov >= 255) continue; // fully covered: the hole stays a hole
  695. double d;
  696. // [2026-09-25 R27] Calibrate the corner band so it joins the four
  697. // straight edges seamlessly:
  698. // * the two straight-edge branches below used to return the distance
  699. // to the straight-band inner boundary instead of to the window
  700. // edge, i.e. nAaRadius too small a deduction. d stayed above
  701. // nKernelSize so the top-up never fired along the four edges;
  702. // those pixels kept the base band while the corner arc got the
  703. // full analytic value -- the arc sat +12 alpha above the band at
  704. // every distance and reached b=3.0 instead of 2.5, so every corner
  705. // showed a hard seam plus a fatter, darker hook.
  706. // * with all three branches corrected d is simply the rounded-rect
  707. // signed distance field, so this top-up becomes a pure function of
  708. // the distance and the whole band matches the four edges exactly.
  709. // * u carries +0.5 because the base band is calibrated as
  710. // K of d plus 2.5: 1/2/3 px outside -> alpha 75/50/25. Without
  711. // it the arc peaked at 87 instead of 75.
  712. if (dPx <= 0.0) d = dPy - nAaRadius; // nearest horizontal edge
  713. else if (dPy <= 0.0) d = dPx - nAaRadius; // nearest vertical edge
  714. else d = sqrt(dPx * dPx + dPy * dPy) - nAaRadius; // nearest corner arc
  715. // [2026-09-26 R29] A partially covered pixel whose centre is still inside
  716. // the window, d <= 0, used to be skipped by the guard that stood here, and
  717. // that guard is what left a pale seam between the shadow and the window all
  718. // along the corner arcs -- most visible on the top left and top right
  719. // rounding of the main window and of the dialogs.
  720. //
  721. // Such a pixel is only partly covered by the window, but the part that is
  722. // NOT covered lies outside the window, between 0 and one pixel away from
  723. // the silhouette. That is exactly the rim the band profile is calibrated
  724. // for, so the band value here is the one at distance 0. Clamping to 0 keeps
  725. // the corner arc a pure function of the distance, the same shape R27 gave
  726. // to the fully outside pixels of the arc. The pixel is then combined below
  727. // by coverage -- R28 attenuated its alpha linearly, R31 instead keeps the
  728. // band alpha and replaces the black source with the coverage-corrected
  729. // content colour.
  730. //
  731. // Scope: only 0 < coverage < 255 can reach this line. A fully covered pixel
  732. // was skipped above so the hole stays a hole, and a fully outside one has
  733. // d > 0, so clamping is a no-op there. The four straight edges, the whole
  734. // outer arc and the hole are therefore left bit for bit untouched.
  735. if (d < 0.0) d = 0.0; // clamp to the window rim
  736. const double u = d + nEroSteps + 0.5;
  737. UINT32 nWant;
  738. if (u < (double)nCenterSize) nWant = m_nDarkness;
  739. else if (u <= (double)nKernelSize)
  740. nWant = (UINT32)((1.0 - (u - nCenterSize) / (double)(m_nSharpness + 1)) * m_nDarkness);
  741. else continue;
  742. if (nWant == 0) continue;
  743. // [2026-09-26 R28 -> R31] SUPERSEDED. This used to be a plain linear
  744. // attenuation, as = a * (255 - cov) / 255 with a black source. It is the
  745. // correct lower bound only when the content colour equals the desktop
  746. // colour, and it is still the fallback used below when the parent content
  747. // bitmap is unavailable, but on darker content it under-attenuated and left
  748. // the pale 1px arc along the top corners. R31 replaced it with the exact
  749. // coverage-corrected source colour.
  750. // [2026-09-26 R31] Exact colour compensation for the partially covered rim.
  751. //
  752. // What the window + shadow stack really does at a pixel the window only partly
  753. // covers (coverage c, content colour w, desktop D):
  754. // under = w*c + D*(1-c) window composited onto the desktop
  755. // out = under*(1-as) + S shadow window composited on top of it
  756. // and what it SHOULD look like is the shadow darkening only the part of the pixel
  757. // the window does not cover:
  758. // ideal = w*c + D*(1-c)*(1-a)
  759. // Solving out == ideal with as = a (the band value, undiminished) gives
  760. // S = a*w*c
  761. // which is independent of D -- so the ring no longer depends on what sits behind
  762. // the window. The rule that used to stand here was as = a*(1-c) with S = 0, i.e. a
  763. // plain black shadow thinned by the coverage; that is the correct lower bound only
  764. // when w == D. For darker content (the green title bar, w ~ 96..131 on a bright
  765. // desktop) it under-attenuates by
  766. // delta = a * (1-c) * (D-w) * c / 255
  767. // which is 10..14 of 255 on the top corners of this app: the pale 1px arc between
  768. // the shadow and the rounded window that R26..R30 could only shrink, never remove.
  769. //
  770. // w*c is exactly the premultiplied content pixel CPaintManagerUI keeps in
  771. // m_pBackgroundBits for the layered rounded-mask path, so the whole fix is "read
  772. // that pixel, multiply it by the band alpha, and do not dim the alpha". Straight
  773. // edges and the outer arc have c == 0, hence S == 0, hence keep the old black band
  774. // bit for bit. A fully covered pixel (c == 255) is the hole and never gets here.
  775. UINT32 nRgb = PreMultiply(m_Color, (unsigned char)nWant);
  776. if (nCov > 0 && pBkgBits != NULL)
  777. {
  778. const LONG nBx = jx - rcBkg.left;
  779. const LONG nBy = iy - rcBkg.top;
  780. if (nBx >= 0 && nBx < nBkgW && nBy >= 0 && nBy < nBkgH)
  781. {
  782. // bottom-up DIB: window row nBy lives at memory row (nBkgH - 1 - nBy)
  783. const UINT32 nPc = ((UINT32)pBkgBits[(LONG)(nBkgH - 1 - nBy) * nBkgW + nBx]) & 0x00FFFFFFu;
  784. if (nPc != 0)
  785. {
  786. const UINT32 nRc = (((nPc >> 16) & 0xFFu) * nWant + 127) / 255;
  787. const UINT32 nGc = (((nPc >> 8) & 0xFFu) * nWant + 127) / 255;
  788. const UINT32 nBc = ((nPc & 0xFFu) * nWant + 127) / 255;
  789. nRgb = (nRc << 16) | (nGc << 8) | nBc;
  790. }
  791. }
  792. }
  793. if (nWant == 0) continue;
  794. if ((*(pLineTop + j) >> 24) < nWant)
  795. *(pLineTop + j) = (nWant << 24) | nRgb;
  796. }
  797. }
  798. }
  799. // Delete used resources
  800. delete[] (ptAnchors - (m_nSize < 0 ? -m_nSize : 0) - 1);
  801. delete[] ptAnchorsTmp;
  802. delete[] ptAnchorsOri;
  803. delete[] pKernel;
  804. DeleteObject(hParentRgn);
  805. }
  806. void CShadowUI::ShowShadow(bool bShow)
  807. {
  808. m_bIsShowShadow = bShow;
  809. }
  810. bool CShadowUI::IsShowShadow() const
  811. {
  812. return m_bIsShowShadow;
  813. }
  814. void CShadowUI::DisableShadow(bool bDisable)
  815. {
  816. m_bIsDisableShadow = bDisable;
  817. if (m_hWnd != NULL) {
  818. if (m_bIsDisableShadow) {
  819. ::ShowWindow(m_hWnd, SW_HIDE);
  820. }
  821. else {
  822. // Determine the initial show state of shadow according to parent window's state
  823. LONG lParentStyle = GetWindowLongPtr(GetParent(m_hWnd), GWL_STYLE);
  824. if (!(WS_VISIBLE & lParentStyle)) // Parent invisible
  825. m_Status = SS_ENABLED;
  826. else if ((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow
  827. m_Status = SS_ENABLED | SS_PARENTVISIBLE;
  828. else {
  829. m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE;
  830. }
  831. if ((WS_VISIBLE & lParentStyle) && !((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle)) {
  832. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  833. Update(GetParent(m_hWnd));
  834. }
  835. }
  836. }
  837. }
  838. ////TODO shadow disnable fix////
  839. bool CShadowUI::IsDisableShadow() const
  840. {
  841. return m_bIsDisableShadow;
  842. }
  843. bool CShadowUI::SetSize(int NewSize)
  844. {
  845. if(NewSize > 35 || NewSize < -35)
  846. return false;
  847. m_nSize = (signed char)NewSize;
  848. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  849. Update(GetParent(m_hWnd));
  850. return true;
  851. }
  852. bool CShadowUI::SetSharpness(unsigned int NewSharpness)
  853. {
  854. if(NewSharpness > 35)
  855. return false;
  856. m_nSharpness = (unsigned char)NewSharpness;
  857. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  858. Update(GetParent(m_hWnd));
  859. return true;
  860. }
  861. bool CShadowUI::SetDarkness(unsigned int NewDarkness)
  862. {
  863. if(NewDarkness > 255)
  864. return false;
  865. m_nDarkness = (unsigned char)NewDarkness;
  866. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  867. Update(GetParent(m_hWnd));
  868. return true;
  869. }
  870. bool CShadowUI::SetPosition(int NewXOffset, int NewYOffset)
  871. {
  872. if(NewXOffset > 35 || NewXOffset < -35 ||
  873. NewYOffset > 35 || NewYOffset < -35)
  874. return false;
  875. m_nxOffset = (signed char)NewXOffset;
  876. m_nyOffset = (signed char)NewYOffset;
  877. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  878. Update(GetParent(m_hWnd));
  879. return true;
  880. }
  881. bool CShadowUI::SetColor(COLORREF NewColor)
  882. {
  883. m_Color = NewColor;
  884. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  885. Update(GetParent(m_hWnd));
  886. return true;
  887. }
  888. bool CShadowUI::SetImage(LPCTSTR szImage)
  889. {
  890. if (szImage == NULL)
  891. return false;
  892. m_bIsImageMode = true;
  893. m_sShadowImage = szImage;
  894. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  895. Update(GetParent(m_hWnd));
  896. return true;
  897. }
  898. bool CShadowUI::SetShadowCorner(RECT rcCorner)
  899. {
  900. if (rcCorner.left < 0 || rcCorner.top < 0 || rcCorner.right < 0 || rcCorner.bottom < 0) return false;
  901. m_rcShadowCorner = rcCorner;
  902. if(m_hWnd != NULL && (SS_VISABLE & m_Status)) {
  903. Update(GetParent(m_hWnd));
  904. }
  905. return true;
  906. }
  907. bool CShadowUI::CopyShadow(CShadowUI* pShadow)
  908. {
  909. if (m_bIsImageMode) {
  910. pShadow->SetImage(m_sShadowImage);
  911. pShadow->SetShadowCorner(m_rcShadowCorner);
  912. pShadow->SetSize((int)m_nSize);
  913. }
  914. else {
  915. pShadow->SetSize((int)m_nSize);
  916. pShadow->SetSharpness((unsigned int)m_nSharpness);
  917. pShadow->SetDarkness((unsigned int)m_nDarkness);
  918. pShadow->SetColor(m_Color);
  919. pShadow->SetPosition((int)m_nxOffset, (int)m_nyOffset);
  920. }
  921. pShadow->ShowShadow(m_bIsShowShadow);
  922. return true;
  923. }
  924. } //namespace DuiLib