WndShadow.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #pragma once
  29. class DUILIB_API CWndShadow
  30. {
  31. public:
  32. CWndShadow(void);
  33. public:
  34. virtual ~CWndShadow(void);
  35. protected:
  36. // Instance handle, used to register window class and create window
  37. static HINSTANCE s_hInstance;
  38. // Parent HWND and CWndShadow object pares, in order to find CWndShadow in ParentProc()
  39. static DuiLib::CDuiValArray s_ShadowArray;
  40. //
  41. typedef BOOL (WINAPI *pfnUpdateLayeredWindow)(HWND hWnd, HDC hdcDst, POINT *pptDst,
  42. SIZE *psize, HDC hdcSrc, POINT *pptSrc, COLORREF crKey,
  43. BLENDFUNCTION *pblend, DWORD dwFlags);
  44. static pfnUpdateLayeredWindow s_UpdateLayeredWindow;
  45. HWND m_hWnd;
  46. WNDPROC m_OriParentProc; // Original WndProc of parent window
  47. enum ShadowStatus
  48. {
  49. SS_ENABLED = 1, // Shadow is enabled, if not, the following one is always false
  50. SS_VISABLE = 1 << 1, // Shadow window is visible
  51. SS_PARENTVISIBLE = 1<< 2 // Parent window is visible, if not, the above one is always false
  52. };
  53. BYTE m_Status;
  54. unsigned char m_nDarkness; // Darkness, transparency of blurred area
  55. unsigned char m_nSharpness; // Sharpness, width of blurred border of shadow window
  56. signed char m_nSize; // Shadow window size, relative to parent window size
  57. // The X and Y offsets of shadow window,
  58. // relative to the parent window, at center of both windows (not top-left corner), signed
  59. signed char m_nxOffset;
  60. signed char m_nyOffset;
  61. // Restore last parent window size, used to determine the update strategy when parent window is resized
  62. LPARAM m_WndSize;
  63. // Set this to true if the shadow should not be update until next WM_PAINT is received
  64. bool m_bUpdate;
  65. COLORREF m_Color; // Color of shadow
  66. DuiLib::TImageInfo* m_pImageInfo;
  67. RECT m_rcCorner;
  68. RECT m_rcHoleOffset;
  69. public:
  70. static bool Initialize(HINSTANCE hInstance);
  71. HWND GetHWND() const;
  72. operator HWND() const;
  73. void Create(HWND hParentWnd);
  74. // 使用图片只需要调用这个方法(rcHoleOffset作用是修复圆角显示空白的bug)
  75. bool SetImage(LPCTSTR image, RECT rcCorner, RECT rcHoleOffset);
  76. // 使用颜色可以使用如下几个方法
  77. bool SetSize(int NewSize = 0);
  78. bool SetSharpness(unsigned int NewSharpness = 5);
  79. bool SetDarkness(unsigned int NewDarkness = 200);
  80. bool SetPosition(int NewXOffset = 5, int NewYOffset = 5);
  81. bool SetColor(COLORREF NewColor = 0);
  82. protected:
  83. static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  84. static LRESULT CALLBACK ParentProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  85. static CWndShadow* FindShadowWindow(HWND hWnd);
  86. static int GetShadowWindowIndex(HWND hWnd);
  87. // Redraw, resize and move the shadow
  88. // called when window resized or shadow properties changed, but not only moved without resizing
  89. void Update(HWND hParent);
  90. // Fill in the shadow window alpha blend bitmap with shadow image pixels
  91. void MakeShadow(UINT32 *pShadBits, HWND hParent, RECT *rcParent);
  92. // Helper to calculate the alpha-premultiled value for a pixel
  93. inline DWORD PreMultiply(COLORREF cl, unsigned char nAlpha)
  94. {
  95. // It's strange that the byte order of RGB in 32b BMP is reverse to in COLORREF
  96. return (GetRValue(cl) * (DWORD)nAlpha / 255) << 16 |
  97. (GetGValue(cl) * (DWORD)nAlpha / 255) << 8 |
  98. (GetBValue(cl) * (DWORD)nAlpha / 255);
  99. }
  100. };