BtnST.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // Class: CButtonST
  3. //
  4. // Compiler: Visual C++
  5. // Tested on: Visual C++ 5.0
  6. // Visual C++ 6.0
  7. //
  8. // Version: See GetVersionC() or GetVersionI()
  9. //
  10. // Created: xx/xxxx/1998
  11. // Updated: 03/March/2003
  12. //
  13. // Author: Davide Calabro' davide_calabro@yahoo.com
  14. // http://www.softechsoftware.it
  15. //
  16. // Note: Code for the PreSubclassWindow and OnSetStyle functions
  17. // has been taken from the COddButton class
  18. // published by Paolo Messina and Jerzy Kaczorowski
  19. //
  20. // Disclaimer
  21. // ----------
  22. // THIS SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT
  23. // ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE
  24. // DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE
  25. // RISK OF USING THIS SOFTWARE.
  26. //
  27. // Terms of use
  28. // ------------
  29. // THIS SOFTWARE IS FREE FOR PERSONAL USE OR FREEWARE APPLICATIONS.
  30. // IF YOU USE THIS SOFTWARE IN COMMERCIAL OR SHAREWARE APPLICATIONS YOU
  31. // ARE GENTLY ASKED TO DONATE 5$ (FIVE U.S. DOLLARS) TO THE AUTHOR:
  32. //
  33. // Davide Calabro'
  34. // P.O. Box 65
  35. // 21019 Somma Lombardo (VA)
  36. // Italy
  37. //
  38. #ifndef _BTNST_H
  39. #define _BTNST_H
  40. // Uncomment the following 2 lines to enable support for BCMenu class
  41. #define BTNST_USE_BCMENU
  42. #include "BCMenu.h"
  43. // Uncomment the following line to enable support for sound effects
  44. #define BTNST_USE_SOUND
  45. #if _MSC_VER >= 1000
  46. #pragma once
  47. #endif // _MSC_VER >= 1000
  48. // Return values
  49. #ifndef BTNST_OK
  50. #define BTNST_OK 0
  51. #endif
  52. #ifndef BTNST_INVALIDRESOURCE
  53. #define BTNST_INVALIDRESOURCE 1
  54. #endif
  55. #ifndef BTNST_FAILEDMASK
  56. #define BTNST_FAILEDMASK 2
  57. #endif
  58. #ifndef BTNST_INVALIDINDEX
  59. #define BTNST_INVALIDINDEX 3
  60. #endif
  61. #ifndef BTNST_INVALIDALIGN
  62. #define BTNST_INVALIDALIGN 4
  63. #endif
  64. #ifndef BTNST_BADPARAM
  65. #define BTNST_BADPARAM 5
  66. #endif
  67. #ifndef BTNST_INVALIDPRESSEDSTYLE
  68. #define BTNST_INVALIDPRESSEDSTYLE 6
  69. #endif
  70. // Dummy identifier for grayscale icon
  71. #ifndef BTNST_AUTO_GRAY
  72. #define BTNST_AUTO_GRAY (HICON)(0xffffffff - 1L)
  73. #endif
  74. // Dummy identifier for 15% darker icon
  75. #ifndef BTNST_AUTO_DARKER
  76. #define BTNST_AUTO_DARKER (HICON)(0xffffffff - 2L)
  77. #endif
  78. class CButtonST : public CButton
  79. {
  80. public:
  81. CButtonST();
  82. ~CButtonST();
  83. enum { ST_ALIGN_HORIZ = 0, // Icon/bitmap on the left, text on the right
  84. ST_ALIGN_VERT, // Icon/bitmap on the top, text on the bottom
  85. ST_ALIGN_HORIZ_RIGHT, // Icon/bitmap on the right, text on the left
  86. ST_ALIGN_OVERLAP // Icon/bitmap on the same space as text
  87. };
  88. enum { BTNST_COLOR_BK_IN = 0, // Background color when mouse is INside
  89. BTNST_COLOR_FG_IN, // Text color when mouse is INside
  90. BTNST_COLOR_BK_OUT, // Background color when mouse is OUTside
  91. BTNST_COLOR_FG_OUT, // Text color when mouse is OUTside
  92. BTNST_COLOR_BK_FOCUS, // Background color when the button is focused
  93. BTNST_COLOR_FG_FOCUS, // Text color when the button is focused
  94. BTNST_MAX_COLORS
  95. };
  96. enum { BTNST_PRESSED_LEFTRIGHT = 0, // Pressed style from left to right (as usual)
  97. BTNST_PRESSED_TOPBOTTOM // Pressed style from top to bottom
  98. };
  99. // ClassWizard generated virtual function overrides
  100. //{{AFX_VIRTUAL(CButtonST)
  101. public:
  102. virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  103. virtual BOOL PreTranslateMessage(MSG* pMsg);
  104. protected:
  105. virtual void PreSubclassWindow();
  106. //}}AFX_VIRTUAL
  107. public:
  108. DWORD SetDefaultColors(BOOL bRepaint = TRUE);
  109. DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE);
  110. DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor);
  111. DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE);
  112. DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE);
  113. int GetCheck();
  114. DWORD SetURL(LPCTSTR lpszURL = NULL);
  115. void DrawTransparent(BOOL bRepaint = FALSE);
  116. DWORD SetBk(CDC* pDC);
  117. BOOL GetDefault();
  118. DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE);
  119. void SetTooltipText(int nText, BOOL bActivate = TRUE);
  120. void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);
  121. void ActivateTooltip(BOOL bEnable = TRUE);
  122. DWORD EnableBalloonTooltip();
  123. DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE);
  124. DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE);
  125. DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE);
  126. DWORD SetPressedStyle(BYTE byStyle, BOOL bRepaint = TRUE);
  127. DWORD DrawBorder(BOOL bDrawBorder = TRUE, BOOL bRepaint = TRUE);
  128. DWORD DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = TRUE);
  129. DWORD SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut = NULL, int nCxDesiredOut = 0, int nCyDesiredOut = 0);
  130. DWORD SetIcon(int nIconIn, int nIconOut = NULL);
  131. DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL);
  132. DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut = NULL, COLORREF crTransColorOut = 0);
  133. DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0);
  134. void SizeToContent();
  135. #ifdef BTNST_USE_BCMENU
  136. DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle = TRUE, UINT nToolbarID = NULL, CSize sizeToolbarIcon = CSize(16, 16), COLORREF crToolbarBk = RGB(255, 0, 255), BOOL bRepaint = TRUE);
  137. #else
  138. DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE);
  139. #endif
  140. DWORD SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam = 0);
  141. #ifdef BTNST_USE_SOUND
  142. DWORD SetSound(LPCTSTR lpszSound, HMODULE hMod = NULL, BOOL bPlayOnClick = FALSE, BOOL bPlayAsync = TRUE);
  143. #endif
  144. static short GetVersionI() {return 39;}
  145. static LPCTSTR GetVersionC() {return (LPCTSTR)_T("3.9");}
  146. BOOL m_bShowDisabledBitmap;
  147. POINT m_ptImageOrg;
  148. POINT m_ptPressedOffset;
  149. protected:
  150. //{{AFX_MSG(CButtonST)
  151. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  152. afx_msg void OnKillFocus(CWnd* pNewWnd);
  153. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  154. afx_msg void OnSysColorChange();
  155. afx_msg BOOL OnClicked();
  156. afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
  157. afx_msg void OnEnable(BOOL bEnable);
  158. afx_msg void OnCancelMode();
  159. afx_msg UINT OnGetDlgCode();
  160. //}}AFX_MSG
  161. #ifdef BTNST_USE_BCMENU
  162. afx_msg LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);
  163. afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
  164. #endif
  165. afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
  166. HICON CreateGrayscaleIcon(HICON hIcon);
  167. HICON CreateDarkerIcon(HICON hIcon);
  168. HBITMAP CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
  169. HBITMAP CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
  170. COLORREF DarkenColor(COLORREF crColor, double dFactor);
  171. virtual DWORD OnDrawBackground(CDC* pDC, CRect* pRect);
  172. virtual DWORD OnDrawBorder(CDC* pDC, CRect* pRect);
  173. BOOL m_bIsFlat; // Is a flat button?
  174. BOOL m_bMouseOnButton; // Is mouse over the button?
  175. BOOL m_bDrawTransparent; // Draw transparent?
  176. BOOL m_bIsPressed; // Is button pressed?
  177. BOOL m_bIsFocused; // Is button focused?
  178. BOOL m_bIsDisabled; // Is button disabled?
  179. BOOL m_bIsDefault; // Is default button?
  180. BOOL m_bIsCheckBox; // Is the button a checkbox?
  181. BYTE m_byAlign; // Align mode
  182. BOOL m_bDrawBorder; // Draw border?
  183. BOOL m_bDrawFlatFocus; // Draw focus rectangle for flat button?
  184. COLORREF m_crColors[BTNST_MAX_COLORS]; // Colors to be used
  185. HWND m_hParentWndMenu; // Handle to window for menu selection
  186. BOOL m_bMenuDisplayed; // Is menu displayed ?
  187. #ifdef BTNST_USE_BCMENU
  188. BCMenu m_menuPopup; // BCMenu class instance
  189. #else
  190. HMENU m_hMenu; // Handle to associated menu
  191. #endif
  192. private:
  193. LRESULT OnSetCheck(WPARAM wParam, LPARAM lParam);
  194. LRESULT OnGetCheck(WPARAM wParam, LPARAM lParam);
  195. LRESULT OnSetStyle(WPARAM wParam, LPARAM lParam);
  196. LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
  197. void CancelHover();
  198. void FreeResources(BOOL bCheckForNULL = TRUE);
  199. void PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage);
  200. HBITMAP CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor);
  201. virtual void DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  202. virtual void DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  203. virtual void DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  204. void PaintBk(CDC* pDC);
  205. void InitToolTip();
  206. HCURSOR m_hCursor; // Handle to cursor
  207. CToolTipCtrl m_ToolTip; // Tooltip
  208. CDC m_dcBk;
  209. CBitmap m_bmpBk;
  210. CBitmap* m_pbmpOldBk;
  211. BOOL m_bAlwaysTrack; // Always hilight button?
  212. int m_nCheck; // Current value for checkbox
  213. UINT m_nTypeStyle; // Button style
  214. DWORD m_dwToolTipStyle; // Style of tooltip control
  215. TCHAR m_szURL[_MAX_PATH]; // URL to open when clicked
  216. #pragma pack(1)
  217. typedef struct _STRUCT_ICONS
  218. {
  219. HICON hIcon; // Handle to icon
  220. DWORD dwWidth; // Width of icon
  221. DWORD dwHeight; // Height of icon
  222. } STRUCT_ICONS;
  223. #pragma pack()
  224. #pragma pack(1)
  225. typedef struct _STRUCT_BITMAPS
  226. {
  227. HBITMAP hBitmap; // Handle to bitmap
  228. DWORD dwWidth; // Width of bitmap
  229. DWORD dwHeight; // Height of bitmap
  230. HBITMAP hMask; // Handle to mask bitmap
  231. COLORREF crTransparent; // Transparent color
  232. } STRUCT_BITMAPS;
  233. #pragma pack()
  234. #pragma pack(1)
  235. typedef struct _STRUCT_CALLBACK
  236. {
  237. HWND hWnd; // Handle to window
  238. UINT nMessage; // Message identifier
  239. WPARAM wParam;
  240. LPARAM lParam;
  241. } STRUCT_CALLBACK;
  242. #pragma pack()
  243. STRUCT_ICONS m_csIcons[2];
  244. STRUCT_BITMAPS m_csBitmaps[2];
  245. STRUCT_CALLBACK m_csCallbacks;
  246. #ifdef BTNST_USE_SOUND
  247. #pragma pack(1)
  248. typedef struct _STRUCT_SOUND
  249. {
  250. TCHAR szSound[_MAX_PATH];
  251. LPCTSTR lpszSound;
  252. HMODULE hMod;
  253. DWORD dwFlags;
  254. } STRUCT_SOUND;
  255. #pragma pack()
  256. STRUCT_SOUND m_csSounds[2]; // Index 0 = Over 1 = Clicked
  257. #endif
  258. DECLARE_MESSAGE_MAP()
  259. };
  260. //{{AFX_INSERT_LOCATION}}
  261. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  262. #endif