UIShadow.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // WndShadow.h : header file
  2. //
  3. // Version 0.1
  4. //
  5. // Copyright (c) 2006 Perry Zhu, All Rights Reserved.
  6. //
  7. // mailto:perry@live.com
  8. //
  9. //
  10. // This source file may be redistributed unmodified by any means PROVIDING
  11. // it is NOT sold for profit without the authors expressed written
  12. // consent, and providing that this notice and the author's name and all
  13. // copyright notices remain intact. This software is by no means to be
  14. // included as part of any third party components library, or as part any
  15. // development solution that offers MFC extensions that are sold for profit.
  16. //
  17. // If the source code is used in any commercial applications then a statement
  18. // along the lines of:
  19. //
  20. // "Portions Copyright (c) 2006 Perry Zhu" must be included in the "Startup
  21. // Banner", "About Box" or "Printed Documentation". This software is provided
  22. // "as is" without express or implied warranty. Use it at your own risk! The
  23. // author accepts no liability for any damage/loss of business that this
  24. // product may cause.
  25. //
  26. /////////////////////////////////////////////////////////////////////////////
  27. //****************************************************************************
  28. /********************************************************************
  29. created: 2015/01/09
  30. filename: UIShadow.h
  31. author: Redrain
  32. purpose: DuiLib阴影类,在原WndShadow类的基础上,增加了通过PNG图片设置阴影的功能,并且把代码与DuiLib融合
  33. *********************************************************************/
  34. #ifndef __UISHADOW_H__
  35. #define __UISHADOW_H__
  36. #pragma once
  37. #include "map"
  38. namespace DuiLib
  39. {
  40. class UILIB_API CShadowUI
  41. {
  42. public:
  43. friend class CPaintManagerUI;
  44. CShadowUI(void);
  45. virtual ~CShadowUI(void);
  46. public:
  47. // bShow为真时才会创建阴影
  48. void ShowShadow(bool bShow);
  49. bool IsShowShadow() const;
  50. // 算法阴影的函数
  51. bool SetSize(int NewSize = 0);
  52. bool SetSharpness(unsigned int NewSharpness = 5);
  53. bool SetDarkness(unsigned int NewDarkness = 200);
  54. bool SetPosition(int NewXOffset = 5, int NewYOffset = 5);
  55. bool SetColor(COLORREF NewColor = 0);
  56. // 图片阴影的函数
  57. bool SetImage(LPCTSTR szImage);
  58. bool SetShadowCorner(RECT rcCorner); // 九宫格方式描述阴影
  59. // 把自己的阴影样式复制到传入参数
  60. bool CopyShadow(CShadowUI* pShadow);
  61. // 创建阴影窗体,由CPaintManagerUI自动调用,除非自己要单独创建阴影
  62. void Create(CPaintManagerUI* pPaintManager);
  63. protected:
  64. // 初始化并注册阴影类
  65. static bool Initialize(HINSTANCE hInstance);
  66. // 保存已经附加的窗体句柄和与其关联的阴影类,方便在ParentProc()函数中通过句柄得到阴影类
  67. static std::map<HWND, CShadowUI *>& GetShadowMap();
  68. // 子类化父窗体
  69. static LRESULT CALLBACK ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  70. // 父窗体改变大小,移动,或者主动重绘阴影时调用
  71. void Update(HWND hParent);
  72. // 通过算法计算阴影
  73. void MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent);
  74. // 计算alpha预乘值
  75. inline DWORD PreMultiply(COLORREF cl, unsigned char nAlpha)
  76. {
  77. return (GetRValue(cl) * (DWORD)nAlpha / 255) |
  78. (GetGValue(cl) * (DWORD)nAlpha / 255) << 8 |
  79. (GetBValue(cl) * (DWORD)nAlpha / 255) << 16 ;
  80. }
  81. protected:
  82. enum ShadowStatus
  83. {
  84. SS_ENABLED = 1, // Shadow is enabled, if not, the following one is always false
  85. SS_VISABLE = 1 << 1, // Shadow window is visible
  86. SS_PARENTVISIBLE = 1<< 2 // Parent window is visible, if not, the above one is always false
  87. };
  88. static bool s_bHasInit;
  89. CPaintManagerUI *m_pManager; // 父窗体的CPaintManagerUI,用来获取素材资源和父窗体句柄
  90. HWND m_hWnd; // 阴影窗体的句柄
  91. LONG_PTR m_OriParentProc; // 子类化父窗体
  92. BYTE m_Status;
  93. bool m_bIsImageMode; // 是否为图片阴影模式
  94. bool m_bIsShowShadow; // 是否要显示阴影
  95. // 算法阴影成员变量
  96. unsigned char m_nDarkness; // Darkness, transparency of blurred area
  97. unsigned char m_nSharpness; // Sharpness, width of blurred border of shadow window
  98. signed char m_nSize; // Shadow window size, relative to parent window size
  99. // The X and Y offsets of shadow window,
  100. // relative to the parent window, at center of both windows (not top-left corner), signed
  101. signed char m_nxOffset;
  102. signed char m_nyOffset;
  103. // Restore last parent window size, used to determine the update strategy when parent window is resized
  104. LPARAM m_WndSize;
  105. // Set this to true if the shadow should not be update until next WM_PAINT is received
  106. bool m_bUpdate;
  107. COLORREF m_Color; // Color of shadow
  108. // 图片阴影成员变量
  109. CDuiString m_sShadowImage;
  110. RECT m_rcShadowCorner;
  111. };
  112. };
  113. #endif //__UISHADOW_H__