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  
 41  // Uncomment the following 2 lines to enable support for BCMenu class
 42  #define	BTNST_USE_BCMENU
 43  #include "BCMenu.h"
 44  
 45  // Uncomment the following line to enable support for sound effects
 46  #define	BTNST_USE_SOUND
 47  
 48  #if _MSC_VER >= 1000
 49  #pragma once
 50  #endif // _MSC_VER >= 1000
 51  
 52  // Return values
 53  #ifndef	BTNST_OK
 54  #define	BTNST_OK						0
 55  #endif
 56  #ifndef	BTNST_INVALIDRESOURCE
 57  #define	BTNST_INVALIDRESOURCE			1
 58  #endif
 59  #ifndef	BTNST_FAILEDMASK
 60  #define	BTNST_FAILEDMASK				2
 61  #endif
 62  #ifndef	BTNST_INVALIDINDEX
 63  #define	BTNST_INVALIDINDEX				3
 64  #endif
 65  #ifndef	BTNST_INVALIDALIGN
 66  #define	BTNST_INVALIDALIGN				4
 67  #endif
 68  #ifndef	BTNST_BADPARAM
 69  #define	BTNST_BADPARAM					5
 70  #endif
 71  #ifndef	BTNST_INVALIDPRESSEDSTYLE
 72  #define	BTNST_INVALIDPRESSEDSTYLE		6
 73  #endif
 74  
 75  // Dummy identifier for grayscale icon
 76  #ifndef	BTNST_AUTO_GRAY
 77  #define	BTNST_AUTO_GRAY					(HICON)(0xffffffff - 1L)
 78  #endif
 79  // Dummy identifier for 15% darker icon
 80  #ifndef	BTNST_AUTO_DARKER
 81  #define	BTNST_AUTO_DARKER				(HICON)(0xffffffff - 2L)
 82  #endif
 83  
 84  class CButtonST : public CButton
 85  {
 86  public:
 87      CButtonST();
 88  	~CButtonST();
 89  
 90      enum	{	ST_ALIGN_HORIZ	= 0,			// Icon/bitmap on the left, text on the right
 91  				ST_ALIGN_VERT,					// Icon/bitmap on the top, text on the bottom
 92  				ST_ALIGN_HORIZ_RIGHT,			// Icon/bitmap on the right, text on the left
 93  				ST_ALIGN_OVERLAP				// Icon/bitmap on the same space as text
 94  			};
 95  
 96  	enum	{	BTNST_COLOR_BK_IN	= 0,		// Background color when mouse is INside
 97  				BTNST_COLOR_FG_IN,				// Text color when mouse is INside
 98  				BTNST_COLOR_BK_OUT,				// Background color when mouse is OUTside
 99  				BTNST_COLOR_FG_OUT,				// Text color when mouse is OUTside
100  				BTNST_COLOR_BK_FOCUS,			// Background color when the button is focused
101  				BTNST_COLOR_FG_FOCUS,			// Text color when the button is focused
102  
103  				BTNST_MAX_COLORS
104  			};
105  
106      enum	{	BTNST_PRESSED_LEFTRIGHT = 0,	// Pressed style from left to right (as usual)
107  				BTNST_PRESSED_TOPBOTTOM			// Pressed style from top to bottom
108  			};
109  
110  	// ClassWizard generated virtual function overrides
111      //{{AFX_VIRTUAL(CButtonST)
112  	public:
113  	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
114  	virtual BOOL PreTranslateMessage(MSG* pMsg);
115  	protected:
116  	virtual void PreSubclassWindow();
117  	//}}AFX_VIRTUAL
118  
119  public:
120  	DWORD SetDefaultColors(BOOL bRepaint = TRUE);
121  	DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE);
122  	DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor);
123  	DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE);
124  
125  	DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE);
126  	int GetCheck();
127  
128  	DWORD SetURL(LPCTSTR lpszURL = NULL);
129  	void DrawTransparent(BOOL bRepaint = FALSE);
130  	DWORD SetBk(CDC* pDC);
131  
132  	BOOL GetDefault();
133  	DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE);
134  
135  	void SetTooltipText(int nText, BOOL bActivate = TRUE);
136  	void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);
137  	void ActivateTooltip(BOOL bEnable = TRUE);
138  	DWORD EnableBalloonTooltip();
139  
140  	DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE);
141  
142  	DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE);
143  	DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE);
144  	DWORD SetPressedStyle(BYTE byStyle, BOOL bRepaint = TRUE);
145  
146  	DWORD DrawBorder(BOOL bDrawBorder = TRUE, BOOL bRepaint = TRUE);
147  	DWORD DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = TRUE);
148  
149  	DWORD SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut = NULL, int nCxDesiredOut = 0, int nCyDesiredOut = 0);
150  	DWORD SetIcon(int nIconIn, int nIconOut = NULL);
151  	DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL);
152  
153  	DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut = NULL, COLORREF crTransColorOut = 0);
154  	DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0);
155  
156  	void SizeToContent();
157  
158  #ifdef	BTNST_USE_BCMENU
159  	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);
160  #else
161  	DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE);
162  #endif
163  	DWORD SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam = 0);
164  
165  #ifdef	BTNST_USE_SOUND
166  	DWORD SetSound(LPCTSTR lpszSound, HMODULE hMod = NULL, BOOL bPlayOnClick = FALSE, BOOL bPlayAsync = TRUE);
167  #endif
168  
169  	static short GetVersionI()		{return 39;}
170  	static LPCTSTR GetVersionC()	{return (LPCTSTR)_T("3.9");}
171  
172  	BOOL	m_bShowDisabledBitmap;
173  	POINT	m_ptImageOrg;
174  	POINT	m_ptPressedOffset;
175  
176  protected:
177      //{{AFX_MSG(CButtonST)
178  	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
179  	afx_msg void OnKillFocus(CWnd* pNewWnd);
180  	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
181  	afx_msg void OnSysColorChange();
182  	afx_msg BOOL OnClicked();
183  	afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
184  	afx_msg void OnEnable(BOOL bEnable);
185  	afx_msg void OnCancelMode();
186  	afx_msg UINT OnGetDlgCode();
187  	//}}AFX_MSG
188  
189  #ifdef	BTNST_USE_BCMENU
190  	afx_msg LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);
191  	afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
192  #endif
193  
194  	afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
195  	HICON CreateGrayscaleIcon(HICON hIcon);
196  	HICON CreateDarkerIcon(HICON hIcon);
197  	HBITMAP CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
198  	HBITMAP CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans);
199  	COLORREF DarkenColor(COLORREF crColor, double dFactor);
200  	virtual DWORD OnDrawBackground(CDC* pDC, CRect* pRect);
201  	virtual DWORD OnDrawBorder(CDC* pDC, CRect* pRect);
202  
203  	BOOL		m_bIsFlat;			// Is a flat button?
204  	BOOL		m_bMouseOnButton;	// Is mouse over the button?
205  	BOOL		m_bDrawTransparent;	// Draw transparent?
206  	BOOL		m_bIsPressed;		// Is button pressed?
207  	BOOL		m_bIsFocused;		// Is button focused?
208  	BOOL		m_bIsDisabled;		// Is button disabled?
209  	BOOL		m_bIsDefault;		// Is default button?
210  	BOOL		m_bIsCheckBox;		// Is the button a checkbox?
211  	BYTE		m_byAlign;			// Align mode
212  	BOOL		m_bDrawBorder;		// Draw border?
213  	BOOL		m_bDrawFlatFocus;	// Draw focus rectangle for flat button?
214  	COLORREF	m_crColors[BTNST_MAX_COLORS];	// Colors to be used
215  	HWND		m_hParentWndMenu;	// Handle to window for menu selection
216  	BOOL		m_bMenuDisplayed;	// Is menu displayed ?
217  
218  #ifdef	BTNST_USE_BCMENU
219  	BCMenu		m_menuPopup;		// BCMenu class instance
220  #else
221  	HMENU		m_hMenu;			// Handle to associated menu
222  #endif
223  
224  private:
225  	LRESULT OnSetCheck(WPARAM wParam, LPARAM lParam);
226  	LRESULT OnGetCheck(WPARAM wParam, LPARAM lParam);
227  	LRESULT OnSetStyle(WPARAM wParam, LPARAM lParam);
228  	LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
229  
230  	void CancelHover();
231  	void FreeResources(BOOL bCheckForNULL = TRUE);
232  	void PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage);
233  	HBITMAP CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor);
234  	virtual void DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
235  	virtual void DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
236  	virtual void DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled);
237  	void PaintBk(CDC* pDC);
238  
239  	void InitToolTip();
240  
241  	HCURSOR		m_hCursor;			// Handle to cursor
242  	CToolTipCtrl m_ToolTip;			// Tooltip
243  
244  	CDC			m_dcBk;
245  	CBitmap		m_bmpBk;
246  	CBitmap*	m_pbmpOldBk;
247  
248  	BOOL		m_bAlwaysTrack;		// Always hilight button?
249  	int			m_nCheck;			// Current value for checkbox
250  	UINT		m_nTypeStyle;		// Button style
251  	DWORD		m_dwToolTipStyle;	// Style of tooltip control
252  
253  	TCHAR		m_szURL[_MAX_PATH];	// URL to open when clicked
254  
255  #pragma pack(1)
256  	typedef struct _STRUCT_ICONS
257  	{
258  		HICON		hIcon;			// Handle to icon
259  		DWORD		dwWidth;		// Width of icon
260  		DWORD		dwHeight;		// Height of icon
261  	} STRUCT_ICONS;
262  #pragma pack()
263  
264  #pragma pack(1)
265  	typedef struct _STRUCT_BITMAPS
266  	{
267  		HBITMAP		hBitmap;		// Handle to bitmap
268  		DWORD		dwWidth;		// Width of bitmap
269  		DWORD		dwHeight;		// Height of bitmap
270  		HBITMAP		hMask;			// Handle to mask bitmap
271  		COLORREF	crTransparent;	// Transparent color
272  	} STRUCT_BITMAPS;
273  #pragma pack()
274  
275  #pragma pack(1)
276  	typedef struct _STRUCT_CALLBACK
277  	{
278  		HWND		hWnd;			// Handle to window
279  		UINT		nMessage;		// Message identifier
280  		WPARAM		wParam;
281  		LPARAM		lParam;
282  	} STRUCT_CALLBACK;
283  #pragma pack()
284  
285  	STRUCT_ICONS	m_csIcons[2];
286  	STRUCT_BITMAPS	m_csBitmaps[2];
287  
288  	STRUCT_CALLBACK	m_csCallbacks;
289  
290  #ifdef	BTNST_USE_SOUND
291  #pragma pack(1)
292  	typedef struct _STRUCT_SOUND
293  	{
294  		TCHAR		szSound[_MAX_PATH];
295  		LPCTSTR		lpszSound;
296  		HMODULE		hMod;
297  		DWORD		dwFlags;
298  	} STRUCT_SOUND;
299  #pragma pack()
300  
301  	STRUCT_SOUND	m_csSounds[2];	// Index 0 = Over	1 = Clicked
302  #endif
303  
304  	DECLARE_MESSAGE_MAP()
305  };
306  
307  //{{AFX_INSERT_LOCATION}}
308  // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
309  
310  #endif