UIShadow.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef __UISHADOW_H__
  2. #define __UISHADOW_H__
  3. #pragma once
  4. #include <map>
  5. namespace DuiLib
  6. {
  7. class UILIB_API CShadowUI
  8. {
  9. public:
  10. friend class CPaintManagerUI;
  11. CShadowUI(void);
  12. virtual ~CShadowUI(void);
  13. public:
  14. // bShow为真时才会创建阴影
  15. void ShowShadow(bool bShow);
  16. bool IsShowShadow() const;
  17. void DisableShadow(bool bDisable);
  18. bool IsDisableShadow() const;
  19. // 算法阴影的函数
  20. bool SetSize(int NewSize = 0);
  21. bool SetSharpness(unsigned int NewSharpness = 5);
  22. bool SetDarkness(unsigned int NewDarkness = 200);
  23. bool SetPosition(int NewXOffset = 5, int NewYOffset = 5);
  24. bool SetColor(COLORREF NewColor = 0);
  25. // 图片阴影的函数
  26. bool SetImage(LPCTSTR szImage);
  27. bool SetShadowCorner(RECT rcCorner); // 九宫格方式描述阴影
  28. // 把自己的阴影样式复制到传入参数
  29. bool CopyShadow(CShadowUI* pShadow);
  30. // 创建阴影窗体,由CPaintManagerUI自动调用,除非自己要单独创建阴影
  31. void Create(CPaintManagerUI* pPaintManager);
  32. protected:
  33. // 初始化并注册阴影类
  34. static bool Initialize(HINSTANCE hInstance);
  35. // 保存已经附加的窗体句柄和与其关联的阴影类,方便在ParentProc()函数中通过句柄得到阴影类
  36. static std::map<HWND, CShadowUI *>& GetShadowMap();
  37. // 子类化父窗体
  38. static LRESULT CALLBACK ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  39. // 父窗体改变大小,移动,或者主动重绘阴影时调用
  40. void Update(HWND hParent);
  41. // 通过算法计算阴影
  42. void MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent);
  43. // 计算alpha预乘值
  44. inline DWORD PreMultiply(COLORREF cl, unsigned char nAlpha)
  45. {
  46. return (GetRValue(cl) * (DWORD)nAlpha / 255) |
  47. (GetGValue(cl) * (DWORD)nAlpha / 255) << 8 |
  48. (GetBValue(cl) * (DWORD)nAlpha / 255) << 16 ;
  49. }
  50. protected:
  51. enum ShadowStatus
  52. {
  53. SS_ENABLED = 1, // Shadow is enabled, if not, the following one is always false
  54. SS_VISABLE = 1 << 1, // Shadow window is visible
  55. SS_PARENTVISIBLE = 1<< 2 // Parent window is visible, if not, the above one is always false
  56. };
  57. static bool s_bHasInit;
  58. CPaintManagerUI *m_pManager; // 父窗体的CPaintManagerUI,用来获取素材资源和父窗体句柄
  59. HWND m_hWnd; // 阴影窗体的句柄
  60. LONG_PTR m_OriParentProc; // 子类化父窗体
  61. BYTE m_Status;
  62. bool m_bIsImageMode; // 是否为图片阴影模式
  63. bool m_bIsShowShadow; // 是否要显示阴影
  64. bool m_bIsDisableShadow;
  65. // 算法阴影成员变量
  66. unsigned char m_nDarkness; // Darkness, transparency of blurred area
  67. unsigned char m_nSharpness; // Sharpness, width of blurred border of shadow window
  68. signed char m_nSize; // Shadow window size, relative to parent window size
  69. // The X and Y offsets of shadow window,
  70. // relative to the parent window, at center of both windows (not top-left corner), signed
  71. signed char m_nxOffset;
  72. signed char m_nyOffset;
  73. // Restore last parent window size, used to determine the update strategy when parent window is resized
  74. LPARAM m_WndSize;
  75. // Set this to true if the shadow should not be update until next WM_PAINT is received
  76. bool m_bUpdate;
  77. COLORREF m_Color; // Color of shadow
  78. // 图片阴影成员变量
  79. CDuiString m_sShadowImage;
  80. RECT m_rcShadowCorner;
  81. };
  82. }
  83. #endif //__UISHADOW_H__