BtnST.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: 18/April/2002
  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 1$ (ONE U.S. DOLLAR) 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. #if _MSC_VER >= 1000
  44. #pragma once
  45. #endif // _MSC_VER >= 1000
  46. // Return values
  47. #ifndef BTNST_OK
  48. #define BTNST_OK 0
  49. #endif
  50. #ifndef BTNST_INVALIDRESOURCE
  51. #define BTNST_INVALIDRESOURCE 1
  52. #endif
  53. #ifndef BTNST_FAILEDMASK
  54. #define BTNST_FAILEDMASK 2
  55. #endif
  56. #ifndef BTNST_INVALIDINDEX
  57. #define BTNST_INVALIDINDEX 3
  58. #endif
  59. #ifndef BTNST_INVALIDALIGN
  60. #define BTNST_INVALIDALIGN 4
  61. #endif
  62. #ifndef BTNST_BADPARAM
  63. #define BTNST_BADPARAM 5
  64. #endif
  65. // Dummy identifier for grayscale icon
  66. #ifndef BTNST_AUTO_GRAY
  67. #define BTNST_AUTO_GRAY (HICON)(0xffffffff - 1L)
  68. #endif
  69. class CButtonST : public CButton
  70. {
  71. public:
  72. CButtonST();
  73. ~CButtonST();
  74. enum { ST_ALIGN_HORIZ = 0, // Icon/bitmap on the left, text on the right
  75. ST_ALIGN_VERT, // Icon/bitmap on the top, text on the bottom
  76. ST_ALIGN_HORIZ_RIGHT // Icon/bitmap on the right, text on the left
  77. };
  78. enum { BTNST_COLOR_BK_IN = 0, // Background color when mouse is INside
  79. BTNST_COLOR_FG_IN, // Text color when mouse is INside
  80. BTNST_COLOR_BK_OUT, // Background color when mouse is OUTside
  81. BTNST_COLOR_FG_OUT, // Text color when mouse is OUTside
  82. BTNST_COLOR_BK_FOCUS, // Background color when the button is focused
  83. BTNST_COLOR_FG_FOCUS, // Text color when the button is focused
  84. BTNST_COLOR_CHOOSE, // Ñ¡ÖÐ״̬
  85. BTNST_COLOR_NOTCHOOSE, //ûѡÖÐ״̬
  86. BTNST_MAX_COLORS
  87. };
  88. // ClassWizard generated virtual function overrides
  89. //{{AFX_VIRTUAL(CButtonST)
  90. public:
  91. virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  92. virtual BOOL PreTranslateMessage(MSG* pMsg);
  93. protected:
  94. virtual void PreSubclassWindow();
  95. virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  96. //}}AFX_VIRTUAL
  97. public:
  98. DWORD SetDefaultColors(BOOL bRepaint = TRUE);
  99. DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE);
  100. DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor);
  101. DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE);
  102. DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE);
  103. int GetCheck();
  104. DWORD SetURL(LPCTSTR lpszURL = NULL);
  105. void DrawTransparent(BOOL bRepaint = FALSE);
  106. BOOL GetDefault();
  107. DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE);
  108. void SetTooltipText(int nText, BOOL bActivate = TRUE);
  109. void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);
  110. void ActivateTooltip(BOOL bEnable = TRUE);
  111. DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE);
  112. DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE);
  113. DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE);
  114. DWORD DrawBorder(BOOL bDrawBorder = TRUE, BOOL bRepaint = TRUE);
  115. DWORD DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = TRUE);
  116. DWORD SetIcon(int nIconIn, int nIconOut = NULL);
  117. DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL);
  118. DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut = NULL, COLORREF crTransColorOut = 0);
  119. DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0);
  120. int m_nIsChoose;//1 choose -1 not 0 set
  121. #ifdef BTNST_USE_BCMENU
  122. 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);
  123. #else
  124. DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE);
  125. #endif
  126. static short GetVersionI() {return 35;}
  127. static LPCTSTR GetVersionC() {return (LPCTSTR)_T("3.5");}
  128. BOOL m_bShowDisabledBitmap;
  129. protected:
  130. //{{AFX_MSG(CButtonST)
  131. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  132. afx_msg void OnKillFocus(CWnd* pNewWnd);
  133. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  134. afx_msg void OnSysColorChange();
  135. afx_msg BOOL OnClicked();
  136. afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
  137. afx_msg void OnEnable(BOOL bEnable);
  138. afx_msg void OnCancelMode();
  139. afx_msg UINT OnGetDlgCode();
  140. //}}AFX_MSG
  141. #ifdef BTNST_USE_BCMENU
  142. afx_msg LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);
  143. afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
  144. #endif
  145. afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
  146. HICON CreateGrayscaleIcon(HICON hIcon);
  147. virtual DWORD OnDrawBackground(CDC* pDC, LPCRECT pRect);
  148. virtual DWORD OnDrawBorder(CDC* pDC, LPCRECT pRect);
  149. BOOL m_bIsFlat; // Is a flat button?
  150. BOOL m_bMouseOnButton; // Is mouse over the button?
  151. BOOL m_bDrawTransparent; // Draw transparent?
  152. BOOL m_bIsPressed; // Is button pressed?
  153. BOOL m_bIsFocused; // Is button focused?
  154. BOOL m_bIsDisabled; // Is button disabled?
  155. BOOL m_bIsDefault; // Is default button?
  156. BOOL m_bIsCheckBox; // Is the button a checkbox?
  157. BYTE m_byAlign; // Align mode
  158. BOOL m_bDrawBorder; // Draw border?
  159. BOOL m_bDrawFlatFocus; // Draw focus rectangle for flat button?
  160. COLORREF m_crColors[BTNST_MAX_COLORS]; // Colors to be used
  161. HWND m_hParentWndMenu; // Handle to window for menu selection
  162. BOOL m_bMenuDisplayed; // Is menu displayed ?
  163. #ifdef BTNST_USE_BCMENU
  164. BCMenu m_menuPopup; // BCMenu class instance
  165. #else
  166. HMENU m_hMenu; // Handle to associated menu
  167. #endif
  168. private:
  169. LRESULT OnSetCheck(WPARAM wParam, LPARAM lParam);
  170. LRESULT OnGetCheck(WPARAM wParam, LPARAM lParam);
  171. LRESULT OnSetStyle(WPARAM wParam, LPARAM lParam);
  172. LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
  173. void CancelHover();
  174. void FreeResources(BOOL bCheckForNULL = TRUE);
  175. void PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage);
  176. HBITMAP CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor);
  177. void DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled);
  178. void DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT *rItem, CRect *rCaption, BOOL bIsPressed, BOOL bIsDisabled);
  179. void PaintBk(CDC* pDC);
  180. void InitToolTip();
  181. HCURSOR m_hCursor; // Handle to cursor
  182. CToolTipCtrl m_ToolTip; // Tooltip
  183. CDC m_dcBk;
  184. CBitmap m_bmpBk;
  185. CBitmap* m_pbmpOldBk;
  186. BOOL m_bAlwaysTrack; // Always hilight button?
  187. int m_nCheck; // Current value for checkbox
  188. UINT m_nTypeStyle; // Button style
  189. TCHAR m_szURL[_MAX_PATH]; // URL to open when clicked
  190. #pragma pack(1)
  191. typedef struct _STRUCT_ICONS
  192. {
  193. HICON hIcon; // Handle to icon
  194. DWORD dwWidth; // Width of icon
  195. DWORD dwHeight; // Height of icon
  196. } STRUCT_ICONS;
  197. #pragma pack()
  198. #pragma pack(1)
  199. typedef struct _STRUCT_BITMAPS
  200. {
  201. HBITMAP hBitmap; // Handle to bitmap
  202. DWORD dwWidth; // Width of bitmap
  203. DWORD dwHeight; // Height of bitmap
  204. HBITMAP hMask; // Handle to mask bitmap
  205. COLORREF crTransparent; // Transparent color
  206. } STRUCT_BITMAPS;
  207. #pragma pack()
  208. STRUCT_ICONS m_csIcons[2];
  209. STRUCT_BITMAPS m_csBitmaps[2];
  210. DECLARE_MESSAGE_MAP()
  211. };
  212. //{{AFX_INSERT_LOCATION}}
  213. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  214. #endif