BtnST.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. void ReconstructFont();
  109. void UpdateSurface();
  110. virtual CButtonST& SetFontBold(BOOL bBold = TRUE);
  111. virtual CButtonST& SetFontName(const CString& strFont, BYTE byCharSet = ANSI_CHARSET);
  112. virtual CButtonST& SetFontUnderline(BOOL bSet = TRUE);
  113. virtual CButtonST& SetFontItalic(BOOL bSet = TRUE);
  114. virtual CButtonST& SetFontSize(int nSize);
  115. DWORD SetDefaultColors(BOOL bRepaint = TRUE);
  116. DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE);
  117. DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor);
  118. DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE);
  119. DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE);
  120. int GetCheck();
  121. DWORD SetURL(LPCTSTR lpszURL = NULL);
  122. void DrawTransparent(BOOL bRepaint = FALSE);
  123. DWORD SetBk(CDC* pDC);
  124. BOOL GetDefault();
  125. DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE);
  126. void SetTooltipText(int nText, BOOL bActivate = TRUE);
  127. void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);
  128. void ActivateTooltip(BOOL bEnable = TRUE);
  129. DWORD EnableBalloonTooltip();
  130. DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE);
  131. DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE);
  132. DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE);
  133. DWORD SetPressedStyle(BYTE byStyle, BOOL bRepaint = TRUE);
  134. DWORD DrawBorder(BOOL bDrawBorder = TRUE, BOOL bRepaint = TRUE);
  135. DWORD DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = TRUE);
  136. DWORD SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut = NULL, int nCxDesiredOut = 0, int nCyDesiredOut = 0);
  137. DWORD SetIcon(int nIconIn, int nIconOut = NULL);
  138. DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL);
  139. DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut = NULL, COLORREF crTransColorOut = 0);
  140. DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0);
  141. void SizeToContent();
  142. #ifdef BTNST_USE_BCMENU
  143. 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);
  144. #else
  145. DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE);
  146. #endif
  147. DWORD SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam = 0);
  148. #ifdef BTNST_USE_SOUND
  149. DWORD SetSound(LPCTSTR lpszSound, HMODULE hMod = NULL, BOOL bPlayOnClick = FALSE, BOOL bPlayAsync = TRUE);
  150. #endif
  151. static short GetVersionI() {return 39;}
  152. static LPCTSTR GetVersionC() {return (LPCTSTR)_T("3.9");}
  153. BOOL m_bShowDisabledBitmap;
  154. POINT m_ptImageOrg;
  155. POINT m_ptPressedOffset;
  156. protected:
  157. //{{AFX_MSG(CButtonST)
  158. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  159. afx_msg void OnKillFocus(CWnd* pNewWnd);
  160. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  161. afx_msg void OnSysColorChange();
  162. afx_msg BOOL OnClicked();
  163. afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
  164. afx_msg void OnEnable(BOOL bEnable);
  165. afx_msg void OnCancelMode();
  166. afx_msg UINT OnGetDlgCode();
  167. //}}AFX_MSG
  168. #ifdef BTNST_USE_BCMENU
  169. afx_msg LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);
  170. afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
  171. #endif
  172. afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
  173. HICON CreateGrayscaleIcon(HICON hIcon);
  174. HICON CreateDarkerIcon(HICON hIcon);
  175. HBITMAP CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
  176. HBITMAP CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
  177. COLORREF DarkenColor(COLORREF crColor, double dFactor);
  178. virtual DWORD OnDrawBackground(CDC* pDC, CRect* pRect);
  179. virtual DWORD OnDrawBorder(CDC* pDC, CRect* pRect);
  180. BOOL m_bIsFlat; // Is a flat button?
  181. BOOL m_bMouseOnButton; // Is mouse over the button?
  182. BOOL m_bDrawTransparent; // Draw transparent?
  183. BOOL m_bIsPressed; // Is button pressed?
  184. BOOL m_bIsFocused; // Is button focused?
  185. BOOL m_bIsDisabled; // Is button disabled?
  186. BOOL m_bIsDefault; // Is default button?
  187. BOOL m_bIsCheckBox; // Is the button a checkbox?
  188. BYTE m_byAlign; // Align mode
  189. BOOL m_bDrawBorder; // Draw border?
  190. BOOL m_bDrawFlatFocus; // Draw focus rectangle for flat button?
  191. COLORREF m_crColors[BTNST_MAX_COLORS]; // Colors to be used
  192. HWND m_hParentWndMenu; // Handle to window for menu selection
  193. BOOL m_bMenuDisplayed; // Is menu displayed ?
  194. LOGFONT m_lf;
  195. CFont m_font;
  196. #ifdef BTNST_USE_BCMENU
  197. BCMenu m_menuPopup; // BCMenu class instance
  198. #else
  199. HMENU m_hMenu; // Handle to associated menu
  200. #endif
  201. private:
  202. LRESULT OnSetCheck(WPARAM wParam, LPARAM lParam);
  203. LRESULT OnGetCheck(WPARAM wParam, LPARAM lParam);
  204. LRESULT OnSetStyle(WPARAM wParam, LPARAM lParam);
  205. LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
  206. void CancelHover();
  207. void FreeResources(BOOL bCheckForNULL = TRUE);
  208. void PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage);
  209. HBITMAP CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor);
  210. virtual void DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  211. virtual void DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  212. virtual void DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
  213. void PaintBk(CDC* pDC);
  214. void InitToolTip();
  215. HCURSOR m_hCursor; // Handle to cursor
  216. CToolTipCtrl m_ToolTip; // Tooltip
  217. CDC m_dcBk;
  218. CBitmap m_bmpBk;
  219. CBitmap* m_pbmpOldBk;
  220. BOOL m_bAlwaysTrack; // Always hilight button?
  221. int m_nCheck; // Current value for checkbox
  222. UINT m_nTypeStyle; // Button style
  223. DWORD m_dwToolTipStyle; // Style of tooltip control
  224. TCHAR m_szURL[_MAX_PATH]; // URL to open when clicked
  225. #pragma pack(1)
  226. typedef struct _STRUCT_ICONS
  227. {
  228. HICON hIcon; // Handle to icon
  229. DWORD dwWidth; // Width of icon
  230. DWORD dwHeight; // Height of icon
  231. } STRUCT_ICONS;
  232. #pragma pack()
  233. #pragma pack(1)
  234. typedef struct _STRUCT_BITMAPS
  235. {
  236. HBITMAP hBitmap; // Handle to bitmap
  237. DWORD dwWidth; // Width of bitmap
  238. DWORD dwHeight; // Height of bitmap
  239. HBITMAP hMask; // Handle to mask bitmap
  240. COLORREF crTransparent; // Transparent color
  241. } STRUCT_BITMAPS;
  242. #pragma pack()
  243. #pragma pack(1)
  244. typedef struct _STRUCT_CALLBACK
  245. {
  246. HWND hWnd; // Handle to window
  247. UINT nMessage; // Message identifier
  248. WPARAM wParam;
  249. LPARAM lParam;
  250. } STRUCT_CALLBACK;
  251. #pragma pack()
  252. STRUCT_ICONS m_csIcons[2];
  253. STRUCT_BITMAPS m_csBitmaps[2];
  254. STRUCT_CALLBACK m_csCallbacks;
  255. #ifdef BTNST_USE_SOUND
  256. #pragma pack(1)
  257. typedef struct _STRUCT_SOUND
  258. {
  259. TCHAR szSound[_MAX_PATH];
  260. LPCTSTR lpszSound;
  261. HMODULE hMod;
  262. DWORD dwFlags;
  263. } STRUCT_SOUND;
  264. #pragma pack()
  265. STRUCT_SOUND m_csSounds[2]; // Index 0 = Over 1 = Clicked
  266. #endif
  267. DECLARE_MESSAGE_MAP()
  268. };
  269. //{{AFX_INSERT_LOCATION}}
  270. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  271. #endif