UIShadow.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. {
  24. ::ZeroMemory(&m_rcShadowCorner, sizeof(RECT));
  25. }
  26. CShadowUI::~CShadowUI(void)
  27. {
  28. }
  29. bool CShadowUI::Initialize(HINSTANCE hInstance)
  30. {
  31. if (s_bHasInit)
  32. return false;
  33. // Register window class for shadow window
  34. WNDCLASSEX wcex;
  35. memset(&wcex, 0, sizeof(wcex));
  36. wcex.cbSize = sizeof(WNDCLASSEX);
  37. wcex.style = CS_HREDRAW | CS_VREDRAW;
  38. wcex.lpfnWndProc = DefWindowProc;
  39. wcex.cbClsExtra = 0;
  40. wcex.cbWndExtra = 0;
  41. wcex.hInstance = hInstance;
  42. wcex.hIcon = NULL;
  43. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  44. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  45. wcex.lpszMenuName = NULL;
  46. wcex.lpszClassName = strWndClassName;
  47. wcex.hIconSm = NULL;
  48. RegisterClassEx(&wcex);
  49. s_bHasInit = true;
  50. return true;
  51. }
  52. void CShadowUI::Create(CPaintManagerUI* pPaintManager)
  53. {
  54. if(!m_bIsShowShadow)
  55. return;
  56. // Already initialized
  57. _ASSERT(CPaintManagerUI::GetInstance() != INVALID_HANDLE_VALUE);
  58. _ASSERT(pPaintManager != NULL);
  59. m_pManager = pPaintManager;
  60. HWND hParentWnd = m_pManager->GetPaintWindow();
  61. // Add parent window - shadow pair to the map
  62. _ASSERT(GetShadowMap().find(hParentWnd) == GetShadowMap().end()); // Only one shadow for each window
  63. GetShadowMap()[hParentWnd] = this;
  64. // Determine the initial show state of shadow according to parent window's state
  65. LONG lParentStyle = GetWindowLongPtr(hParentWnd, GWL_STYLE);
  66. // Create the shadow window
  67. LONG styleValue = lParentStyle & WS_CAPTION;
  68. m_hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, strWndClassName, NULL,
  69. /*WS_VISIBLE | */styleValue | WS_POPUPWINDOW,
  70. CW_USEDEFAULT, 0, 0, 0, hParentWnd, NULL, CPaintManagerUI::GetInstance(), NULL);
  71. if(!(WS_VISIBLE & lParentStyle)) // Parent invisible
  72. m_Status = SS_ENABLED;
  73. else if((WS_MAXIMIZE | WS_MINIMIZE) & lParentStyle) // Parent visible but does not need shadow
  74. m_Status = SS_ENABLED | SS_PARENTVISIBLE;
  75. else // Show the shadow
  76. {
  77. m_Status = SS_ENABLED | SS_VISABLE | SS_PARENTVISIBLE;
  78. ::ShowWindow(m_hWnd, SW_SHOWNA);
  79. Update(hParentWnd);
  80. }
  81. // Replace the original WndProc of parent window to steal messages
  82. m_OriParentProc = GetWindowLongPtr(hParentWnd, GWLP_WNDPROC);
  83. #pragma warning(disable: 4311) // temporrarily disable the type_cast warning in Win32
  84. SetWindowLongPtr(hParentWnd, GWLP_WNDPROC, (LONG_PTR)ParentProc);
  85. #pragma warning(default: 4311)
  86. }
  87. std::map<HWND, CShadowUI *>& CShadowUI::GetShadowMap()
  88. {
  89. static std::map<HWND, CShadowUI *> s_Shadowmap;
  90. return s_Shadowmap;
  91. }
  92. LRESULT CALLBACK CShadowUI::ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  93. {
  94. _ASSERT(GetShadowMap().find(hwnd) != GetShadowMap().end()); // Shadow must have been attached
  95. CShadowUI *pThis = GetShadowMap()[hwnd];
  96. switch(uMsg)
  97. {
  98. case WM_MOVE:
  99. if(pThis->m_Status & SS_VISABLE)
  100. {
  101. RECT WndRect;
  102. GetWindowRect(hwnd, &WndRect);
  103. if (pThis->m_bIsImageMode)
  104. {
  105. SetWindowPos(pThis->m_hWnd, 0,
  106. WndRect.left - pThis->m_rcShadowCorner.left, WndRect.top - pThis->m_rcShadowCorner.top,
  107. 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  108. }
  109. else
  110. {
  111. SetWindowPos(pThis->m_hWnd, 0,
  112. WndRect.left + pThis->m_nxOffset - pThis->m_nSize, WndRect.top + pThis->m_nyOffset - pThis->m_nSize,
  113. 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  114. }
  115. }
  116. break;
  117. case WM_SIZE:
  118. if(pThis->m_Status & SS_ENABLED)
  119. {
  120. if(SIZE_MAXIMIZED == wParam || SIZE_MINIMIZED == wParam)
  121. {
  122. ::ShowWindow(pThis->m_hWnd, SW_HIDE);
  123. pThis->m_Status &= ~SS_VISABLE;
  124. }
  125. else if(pThis->m_Status & SS_PARENTVISIBLE) // Parent maybe resized even if invisible
  126. {
  127. // Awful! It seems that if the window size was not decreased
  128. // the window region would never be updated until WM_PAINT was sent.
  129. // So do not Update() until next WM_PAINT is received in this case
  130. if(LOWORD(lParam) > LOWORD(pThis->m_WndSize) || HIWORD(lParam) > HIWORD(pThis->m_WndSize))
  131. pThis->m_bUpdate = true;
  132. else
  133. pThis->Update(hwnd);
  134. if(!(pThis->m_Status & SS_VISABLE))
  135. {
  136. ::ShowWindow(pThis->m_hWnd, SW_SHOWNA);
  137. pThis->m_Status |= SS_VISABLE;
  138. }
  139. }
  140. pThis->m_WndSize = lParam;
  141. }
  142. break;
  143. case WM_PAINT:
  144. {
  145. if(pThis->m_bUpdate)
  146. {
  147. pThis->Update(hwnd);
  148. pThis->m_bUpdate = false;
  149. }
  150. //return hr;
  151. break;
  152. }
  153. // In some cases of sizing, the up-right corner of the parent window region would not be properly updated
  154. // Update() again when sizing is finished
  155. case WM_EXITSIZEMOVE:
  156. if(pThis->m_Status & SS_VISABLE)
  157. {
  158. pThis->Update(hwnd);
  159. }
  160. break;
  161. case WM_SHOWWINDOW:
  162. if(pThis->m_Status & SS_ENABLED)
  163. {
  164. if(!wParam) // the window is being hidden
  165. {
  166. ::ShowWindow(pThis->m_hWnd, SW_HIDE);
  167. pThis->m_Status &= ~(SS_VISABLE | SS_PARENTVISIBLE);
  168. }
  169. else if(!(pThis->m_Status & SS_PARENTVISIBLE))
  170. {
  171. //pThis->Update(hwnd);
  172. pThis->m_bUpdate = true;
  173. ::ShowWindow(pThis->m_hWnd, SW_SHOWNA);
  174. pThis->m_Status |= SS_VISABLE | SS_PARENTVISIBLE;
  175. }
  176. }
  177. break;
  178. case WM_DESTROY:
  179. DestroyWindow(pThis->m_hWnd); // Destroy the shadow
  180. break;
  181. case WM_NCDESTROY:
  182. GetShadowMap().erase(hwnd); // Remove this window and shadow from the map
  183. break;
  184. }
  185. #pragma warning(disable: 4312) // temporrarily disable the type_cast warning in Win32
  186. // Call the default(original) window procedure for other messages or messages processed but not returned
  187. return ((WNDPROC)pThis->m_OriParentProc)(hwnd, uMsg, wParam, lParam);
  188. #pragma warning(default: 4312)
  189. }
  190. void CShadowUI::Update(HWND hParent)
  191. {
  192. RECT WndRect;
  193. GetWindowRect(hParent, &WndRect);
  194. int nShadWndWid;
  195. int nShadWndHei;
  196. if (m_bIsImageMode)
  197. {
  198. if(m_sShadowImage.IsEmpty())
  199. return;
  200. nShadWndWid = WndRect.right - WndRect.left + m_rcShadowCorner.left + m_rcShadowCorner.right;
  201. nShadWndHei = WndRect.bottom - WndRect.top + m_rcShadowCorner.top + m_rcShadowCorner.bottom;
  202. }
  203. else
  204. {
  205. nShadWndWid = WndRect.right - WndRect.left + m_nSize * 2;
  206. nShadWndHei = WndRect.bottom - WndRect.top + m_nSize * 2;
  207. }
  208. // Create the alpha blending bitmap
  209. BITMAPINFO bmi; // bitmap header
  210. ZeroMemory(&bmi, sizeof(BITMAPINFO));
  211. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  212. bmi.bmiHeader.biWidth = nShadWndWid;
  213. bmi.bmiHeader.biHeight = nShadWndHei;
  214. bmi.bmiHeader.biPlanes = 1;
  215. bmi.bmiHeader.biBitCount = 32; // four 8-bit components
  216. bmi.bmiHeader.biCompression = BI_RGB;
  217. bmi.bmiHeader.biSizeImage = nShadWndWid * nShadWndHei * 4;
  218. BYTE *pvBits; // pointer to DIB section
  219. HBITMAP hbitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pvBits, NULL, 0);
  220. HDC hMemDC = CreateCompatibleDC(NULL);
  221. HBITMAP hOriBmp = (HBITMAP)SelectObject(hMemDC, hbitmap);
  222. if (m_bIsImageMode)
  223. {
  224. RECT rcPaint = {0, 0, nShadWndWid, nShadWndHei};
  225. const TImageInfo* data = m_pManager->GetImageEx((LPCTSTR)m_sShadowImage, NULL, 0);
  226. if( !data )
  227. return;
  228. RECT rcBmpPart = {0};
  229. rcBmpPart.right = data->nX;
  230. rcBmpPart.bottom = data->nY;
  231. CRenderEngine::DrawImage(hMemDC, data->hBitmap, rcPaint, rcPaint, rcBmpPart, m_rcShadowCorner,data->bAlpha, 0xFF, true, false, false);
  232. }
  233. else
  234. {
  235. ZeroMemory(pvBits, bmi.bmiHeader.biSizeImage);
  236. MakeShadow((UINT32 *)pvBits, hParent, &WndRect);
  237. }
  238. POINT ptDst;
  239. if (m_bIsImageMode)
  240. {
  241. ptDst.x = WndRect.left - m_rcShadowCorner.left;
  242. ptDst.y = WndRect.top - m_rcShadowCorner.top;
  243. }
  244. else
  245. {
  246. ptDst.x = WndRect.left + m_nxOffset - m_nSize;
  247. ptDst.y = WndRect.top + m_nyOffset - m_nSize;
  248. }
  249. POINT ptSrc = {0, 0};
  250. SIZE WndSize = {nShadWndWid, nShadWndHei};
  251. BLENDFUNCTION blendPixelFunction= { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  252. MoveWindow(m_hWnd, ptDst.x, ptDst.y, nShadWndWid, nShadWndHei, FALSE);
  253. BOOL bRet= ::UpdateLayeredWindow(m_hWnd, NULL, &ptDst, &WndSize, hMemDC,
  254. &ptSrc, 0, &blendPixelFunction, ULW_ALPHA);
  255. _ASSERT(bRet); // something was wrong....
  256. // Delete used resources
  257. SelectObject(hMemDC, hOriBmp);
  258. DeleteObject(hbitmap);
  259. DeleteDC(hMemDC);
  260. }
  261. void CShadowUI::MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent)
  262. {
  263. // The shadow algorithm:
  264. // Get the region of parent window,
  265. // Apply morphologic erosion to shrink it into the size (ShadowWndSize - Sharpness)
  266. // Apply modified (with blur effect) morphologic dilation to make the blurred border
  267. // The algorithm is optimized by assuming parent window is just "one piece" and without "wholes" on it
  268. // Get the region of parent window,
  269. HRGN hParentRgn = CreateRectRgn(0, 0, 0, 0);
  270. GetWindowRgn(hParent, hParentRgn);
  271. // Determine the Start and end point of each horizontal scan line
  272. SIZE szParent = {rcParent->right - rcParent->left, rcParent->bottom - rcParent->top};
  273. SIZE szShadow = {szParent.cx + 2 * m_nSize, szParent.cy + 2 * m_nSize};
  274. // Extra 2 lines (set to be empty) in ptAnchors are used in dilation
  275. int nAnchors = max(szParent.cy, szShadow.cy); // # of anchor points pares
  276. int (*ptAnchors)[2] = new int[nAnchors + 2][2];
  277. int (*ptAnchorsOri)[2] = new int[szParent.cy][2]; // anchor points, will not modify during erosion
  278. ptAnchors[0][0] = szParent.cx;
  279. ptAnchors[0][1] = 0;
  280. ptAnchors[nAnchors + 1][0] = szParent.cx;
  281. ptAnchors[nAnchors + 1][1] = 0;
  282. if(m_nSize > 0)
  283. {
  284. // Put the parent window anchors at the center
  285. for(int i = 0; i < m_nSize; i++)
  286. {
  287. ptAnchors[i + 1][0] = szParent.cx;
  288. ptAnchors[i + 1][1] = 0;
  289. ptAnchors[szShadow.cy - i][0] = szParent.cx;
  290. ptAnchors[szShadow.cy - i][1] = 0;
  291. }
  292. ptAnchors += m_nSize;
  293. }
  294. for(int i = 0; i < szParent.cy; i++)
  295. {
  296. // find start point
  297. int j;
  298. for(j = 0; j < szParent.cx; j++)
  299. {
  300. if(PtInRegion(hParentRgn, j, i))
  301. {
  302. ptAnchors[i + 1][0] = j + m_nSize;
  303. ptAnchorsOri[i][0] = j;
  304. break;
  305. }
  306. }
  307. if(j >= szParent.cx) // Start point not found
  308. {
  309. ptAnchors[i + 1][0] = szParent.cx;
  310. ptAnchorsOri[i][1] = 0;
  311. ptAnchors[i + 1][0] = szParent.cx;
  312. ptAnchorsOri[i][1] = 0;
  313. }
  314. else
  315. {
  316. // find end point
  317. for(j = szParent.cx - 1; j >= ptAnchors[i + 1][0]; j--)
  318. {
  319. if(PtInRegion(hParentRgn, j, i))
  320. {
  321. ptAnchors[i + 1][1] = j + 1 + m_nSize;
  322. ptAnchorsOri[i][1] = j + 1;
  323. break;
  324. }
  325. }
  326. }
  327. }
  328. if(m_nSize > 0)
  329. ptAnchors -= m_nSize; // Restore pos of ptAnchors for erosion
  330. int (*ptAnchorsTmp)[2] = new int[nAnchors + 2][2]; // Store the result of erosion
  331. // First and last line should be empty
  332. ptAnchorsTmp[0][0] = szParent.cx;
  333. ptAnchorsTmp[0][1] = 0;
  334. ptAnchorsTmp[nAnchors + 1][0] = szParent.cx;
  335. ptAnchorsTmp[nAnchors + 1][1] = 0;
  336. int nEroTimes = 0;
  337. // morphologic erosion
  338. for(int i = 0; i < m_nSharpness - m_nSize; i++)
  339. {
  340. nEroTimes++;
  341. //ptAnchorsTmp[1][0] = szParent.cx;
  342. //ptAnchorsTmp[1][1] = 0;
  343. //ptAnchorsTmp[szParent.cy + 1][0] = szParent.cx;
  344. //ptAnchorsTmp[szParent.cy + 1][1] = 0;
  345. for(int j = 1; j < nAnchors + 1; j++)
  346. {
  347. ptAnchorsTmp[j][0] = max(ptAnchors[j - 1][0], max(ptAnchors[j][0], ptAnchors[j + 1][0])) + 1;
  348. ptAnchorsTmp[j][1] = min(ptAnchors[j - 1][1], min(ptAnchors[j][1], ptAnchors[j + 1][1])) - 1;
  349. }
  350. // Exchange ptAnchors and ptAnchorsTmp;
  351. int (*ptAnchorsXange)[2] = ptAnchorsTmp;
  352. ptAnchorsTmp = ptAnchors;
  353. ptAnchors = ptAnchorsXange;
  354. }
  355. // morphologic dilation
  356. ptAnchors += (m_nSize < 0 ? -m_nSize : 0) + 1; // now coordinates in ptAnchors are same as in shadow window
  357. // Generate the kernel
  358. int nKernelSize = m_nSize > m_nSharpness ? m_nSize : m_nSharpness;
  359. int nCenterSize = m_nSize > m_nSharpness ? (m_nSize - m_nSharpness) : 0;
  360. UINT32 *pKernel = new UINT32[(2 * nKernelSize + 1) * (2 * nKernelSize + 1)];
  361. UINT32 *pKernelIter = pKernel;
  362. for(int i = 0; i <= 2 * nKernelSize; i++)
  363. {
  364. for(int j = 0; j <= 2 * nKernelSize; j++)
  365. {
  366. double dLength = sqrt((i - nKernelSize) * (i - nKernelSize) + (j - nKernelSize) * (double)(j - nKernelSize));
  367. if(dLength < nCenterSize)
  368. *pKernelIter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  369. else if(dLength <= nKernelSize)
  370. {
  371. UINT32 nFactor = ((UINT32)((1 - (dLength - nCenterSize) / (m_nSharpness + 1)) * m_nDarkness));
  372. *pKernelIter = nFactor << 24 | PreMultiply(m_Color, nFactor);
  373. }
  374. else
  375. *pKernelIter = 0;
  376. //TRACE("%d ", *pKernelIter >> 24);
  377. pKernelIter ++;
  378. }
  379. //TRACE("\n");
  380. }
  381. // Generate blurred border
  382. for(int i = nKernelSize; i < szShadow.cy - nKernelSize; i++)
  383. {
  384. int j;
  385. if(ptAnchors[i][0] < ptAnchors[i][1])
  386. {
  387. // Start of line
  388. for(j = ptAnchors[i][0];
  389. j < min(max(ptAnchors[i - 1][0], ptAnchors[i + 1][0]) + 1, ptAnchors[i][1]);
  390. j++)
  391. {
  392. for(int k = 0; k <= 2 * nKernelSize; k++)
  393. {
  394. UINT32 *pPixel = pShadBits +
  395. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  396. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  397. for(int l = 0; l <= 2 * nKernelSize; l++)
  398. {
  399. if(*pPixel < *pKernelPixel)
  400. *pPixel = *pKernelPixel;
  401. pPixel++;
  402. pKernelPixel++;
  403. }
  404. }
  405. } // for() start of line
  406. // End of line
  407. for(j = max(j, min(ptAnchors[i - 1][1], ptAnchors[i + 1][1]) - 1);
  408. j < ptAnchors[i][1];
  409. j++)
  410. {
  411. for(int k = 0; k <= 2 * nKernelSize; k++)
  412. {
  413. UINT32 *pPixel = pShadBits +
  414. (szShadow.cy - i - 1 + nKernelSize - k) * szShadow.cx + j - nKernelSize;
  415. UINT32 *pKernelPixel = pKernel + k * (2 * nKernelSize + 1);
  416. for(int l = 0; l <= 2 * nKernelSize; l++)
  417. {
  418. if(*pPixel < *pKernelPixel)
  419. *pPixel = *pKernelPixel;
  420. pPixel++;
  421. pKernelPixel++;
  422. }
  423. }
  424. } // for() end of line
  425. }
  426. } // for() Generate blurred border
  427. // Erase unwanted parts and complement missing
  428. UINT32 clCenter = m_nDarkness << 24 | PreMultiply(m_Color, m_nDarkness);
  429. for(int i = min(nKernelSize, max(m_nSize - m_nyOffset, 0));
  430. i < max(szShadow.cy - nKernelSize, min(szParent.cy + m_nSize - m_nyOffset, szParent.cy + 2 * m_nSize));
  431. i++)
  432. {
  433. UINT32 *pLine = pShadBits + (szShadow.cy - i - 1) * szShadow.cx;
  434. if(i - m_nSize + m_nyOffset < 0 || i - m_nSize + m_nyOffset >= szParent.cy) // Line is not covered by parent window
  435. {
  436. for(int j = ptAnchors[i][0]; j < ptAnchors[i][1]; j++)
  437. {
  438. *(pLine + j) = clCenter;
  439. }
  440. }
  441. else
  442. {
  443. for(int j = ptAnchors[i][0];
  444. j < min(ptAnchorsOri[i - m_nSize + m_nyOffset][0] + m_nSize - m_nxOffset, ptAnchors[i][1]);
  445. j++)
  446. *(pLine + j) = clCenter;
  447. for(int j = max(ptAnchorsOri[i - m_nSize + m_nyOffset][0] + m_nSize - m_nxOffset, 0);
  448. j < min(ptAnchorsOri[i - m_nSize + m_nyOffset][1] + m_nSize - m_nxOffset, szShadow.cx);
  449. j++)
  450. *(pLine + j) = 0;
  451. for(int j = max(ptAnchorsOri[i - m_nSize + m_nyOffset][1] + m_nSize - m_nxOffset, ptAnchors[i][0]);
  452. j < ptAnchors[i][1];
  453. j++)
  454. *(pLine + j) = clCenter;
  455. }
  456. }
  457. // Delete used resources
  458. delete[] (ptAnchors - (m_nSize < 0 ? -m_nSize : 0) - 1);
  459. delete[] ptAnchorsTmp;
  460. delete[] ptAnchorsOri;
  461. delete[] pKernel;
  462. DeleteObject(hParentRgn);
  463. }
  464. void CShadowUI::ShowShadow(bool bShow)
  465. {
  466. m_bIsShowShadow = bShow;
  467. }
  468. bool CShadowUI::IsShowShadow() const
  469. {
  470. return m_bIsShowShadow;
  471. }
  472. bool CShadowUI::SetSize(int NewSize)
  473. {
  474. if(NewSize > 20 || NewSize < -20)
  475. return false;
  476. m_nSize = (signed char)NewSize;
  477. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  478. Update(GetParent(m_hWnd));
  479. return true;
  480. }
  481. bool CShadowUI::SetSharpness(unsigned int NewSharpness)
  482. {
  483. if(NewSharpness > 20)
  484. return false;
  485. m_nSharpness = (unsigned char)NewSharpness;
  486. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  487. Update(GetParent(m_hWnd));
  488. return true;
  489. }
  490. bool CShadowUI::SetDarkness(unsigned int NewDarkness)
  491. {
  492. if(NewDarkness > 255)
  493. return false;
  494. m_nDarkness = (unsigned char)NewDarkness;
  495. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  496. Update(GetParent(m_hWnd));
  497. return true;
  498. }
  499. bool CShadowUI::SetPosition(int NewXOffset, int NewYOffset)
  500. {
  501. if(NewXOffset > 20 || NewXOffset < -20 ||
  502. NewYOffset > 20 || NewYOffset < -20)
  503. return false;
  504. m_nxOffset = (signed char)NewXOffset;
  505. m_nyOffset = (signed char)NewYOffset;
  506. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  507. Update(GetParent(m_hWnd));
  508. return true;
  509. }
  510. bool CShadowUI::SetColor(COLORREF NewColor)
  511. {
  512. m_Color = NewColor;
  513. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  514. Update(GetParent(m_hWnd));
  515. return true;
  516. }
  517. bool CShadowUI::SetImage(LPCTSTR szImage)
  518. {
  519. if (szImage == NULL)
  520. return false;
  521. m_bIsImageMode = true;
  522. m_sShadowImage = szImage;
  523. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  524. Update(GetParent(m_hWnd));
  525. return true;
  526. }
  527. bool CShadowUI::SetShadowCorner(RECT rcCorner)
  528. {
  529. if (rcCorner.left < 0 || rcCorner.top < 0 || rcCorner.right < 0 || rcCorner.bottom < 0)
  530. return false;
  531. m_rcShadowCorner = rcCorner;
  532. if(m_hWnd != NULL && (SS_VISABLE & m_Status))
  533. Update(GetParent(m_hWnd));
  534. return true;
  535. }
  536. bool CShadowUI::CopyShadow(CShadowUI* pShadow)
  537. {
  538. if (m_bIsImageMode)
  539. {
  540. pShadow->SetImage(m_sShadowImage);
  541. pShadow->SetShadowCorner(m_rcShadowCorner);
  542. }
  543. else
  544. {
  545. pShadow->SetSize((int)m_nSize);
  546. pShadow->SetSharpness((unsigned int)m_nSharpness);
  547. pShadow->SetDarkness((unsigned int)m_nDarkness);
  548. pShadow->SetColor(m_Color);
  549. pShadow->SetPosition((int)m_nxOffset, (int)m_nyOffset);
  550. }
  551. return true;
  552. }
  553. }; //namespace DuiLib