1#include "stdafx.h"
  2#include "BtnST.h"
  3
  4#ifdef    BTNST_USE_SOUND
  5#pragma comment(lib, "winmm.lib")
  6#include <Mmsystem.h>
  7#endif
  8
  9#ifdef _DEBUG
 10#define new DEBUG_NEW
 11#undef THIS_FILE
 12static char THIS_FILE[] = __FILE__;
 13#endif
 14
 15/////////////////////////////////////////////////////////////////////////////
 16// CButtonST
 17
 18// Mask for control's type
 19#define BS_TYPEMASK SS_TYPEMASK
 20
 21#ifndef    TTM_SETTITLE
 22#define TTM_SETTITLEA           (WM_USER + 32)  // wParam = TTI_*, lParam = char* szTitle
 23#define TTM_SETTITLEW           (WM_USER + 33)  // wParam = TTI_*, lParam = wchar* szTitle
 24#ifdef    UNICODE
 25#define TTM_SETTITLE            TTM_SETTITLEW
 26#else
 27#define TTM_SETTITLE            TTM_SETTITLEA
 28#endif
 29#endif
 30
 31#ifndef    TTS_BALLOON
 32#define    TTS_BALLOON        0x40
 33#endif
 34
 35CButtonST::CButtonST()
 36{
 37    m_bIsPressed        = FALSE;
 38    m_bIsFocused        = FALSE;
 39    m_bIsDisabled        = FALSE;
 40    m_bMouseOnButton    = FALSE;
 41
 42    FreeResources(FALSE);
 43
 44    // Default type is "flat" button
 45    m_bIsFlat = TRUE;
 46    // Button will be tracked also if when the window is inactive (like Internet Explorer)
 47    m_bAlwaysTrack = TRUE;
 48  
 49    // By default draw border in "flat" button 
 50    m_bDrawBorder = TRUE; 
 51  
 52    // By default icon is aligned horizontally
 53    m_byAlign = ST_ALIGN_HORIZ; 
 54
 55    // By default use usual pressed style
 56    SetPressedStyle(BTNST_PRESSED_LEFTRIGHT, FALSE);
 57  
 58    // By default, for "flat" button, don't draw the focus rect
 59    m_bDrawFlatFocus = FALSE;
 60
 61    // By default the button is not the default button
 62    m_bIsDefault = FALSE;
 63    // Invalid value, since type still unknown
 64    m_nTypeStyle = BS_TYPEMASK;
 65
 66    // By default the button is not a checkbox
 67    m_bIsCheckBox = FALSE;
 68    m_nCheck = 0;
 69
 70    // Set default colors
 71    SetDefaultColors(FALSE);
 72
 73    // No tooltip created
 74    m_ToolTip.m_hWnd = NULL;
 75    m_dwToolTipStyle = 0;
 76
 77    // Do not draw as a transparent button
 78    m_bDrawTransparent = FALSE;
 79    m_pbmpOldBk = NULL;
 80
 81    // No URL defined
 82    SetURL(NULL);
 83
 84    // No cursor defined
 85    m_hCursor = NULL;
 86
 87    // No associated menu
 88#ifndef    BTNST_USE_BCMENU
 89    m_hMenu = NULL;
 90#endif
 91    m_hParentWndMenu = NULL;
 92    m_bMenuDisplayed = FALSE;
 93
 94    m_bShowDisabledBitmap = TRUE;
 95
 96    m_ptImageOrg.x = 3;
 97    m_ptImageOrg.y = 3;
 98
 99    // No defined callbacks
100    ::ZeroMemory(&m_csCallbacks, sizeof(m_csCallbacks));
101
102#ifdef    BTNST_USE_SOUND
103    // No defined sounds
104    ::ZeroMemory(&m_csSounds, sizeof(m_csSounds));
105#endif
106}
 // End of CButtonST
107
108CButtonST::~CButtonST()
109{
110    // Restore old bitmap (if any)
111    if (m_dcBk.m_hDC && m_pbmpOldBk)
112    {
113        m_dcBk.SelectObject(m_pbmpOldBk);
114    }
 // if
115
116    FreeResources();
117
118    // Destroy the cursor (if any)
119    if (m_hCursor) ::DestroyCursor(m_hCursor);
120
121    // Destroy the menu (if any)
122#ifdef    BTNST_USE_BCMENU
123    if (m_menuPopup.m_hMenu)    m_menuPopup.DestroyMenu();
124#else
125    if (m_hMenu)    ::DestroyMenu(m_hMenu);
126#endif
127}
 // End of ~CButtonST
128
129BEGIN_MESSAGE_MAP(CButtonST, CButton)
130    //{{AFX_MSG_MAP(CButtonST)
131    ON_WM_SETCURSOR()
132    ON_WM_KILLFOCUS()
133    ON_WM_MOUSEMOVE()
134    ON_WM_SYSCOLORCHANGE()
135    ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
136    ON_WM_ACTIVATE()
137    ON_WM_ENABLE()
138    ON_WM_CANCELMODE()
139    ON_WM_GETDLGCODE()
140    ON_WM_CTLCOLOR_REFLECT()
141    //}}AFX_MSG_MAP
142#ifdef    BTNST_USE_BCMENU
143    ON_WM_MENUCHAR()
144    ON_WM_MEASUREITEM()
145#endif
146
147    ON_MESSAGE(BM_SETSTYLE, OnSetStyle)
148    ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
149    ON_MESSAGE(BM_SETCHECK, OnSetCheck)
150    ON_MESSAGE(BM_GETCHECK, OnGetCheck)
151END_MESSAGE_MAP()
152
153void CButtonST::FreeResources(BOOL bCheckForNULL)
154{
155    if (bCheckForNULL)
156    {
157        // Destroy icons
158        // Note: the following two lines MUST be here! even if
159        // BoundChecker says they are unnecessary!
160        if (m_csIcons[0].hIcon)    ::DestroyIcon(m_csIcons[0].hIcon);
161        if (m_csIcons[1].hIcon)    ::DestroyIcon(m_csIcons[1].hIcon);
162
163        // Destroy bitmaps
164        if (m_csBitmaps[0].hBitmap)    ::DeleteObject(m_csBitmaps[0].hBitmap);
165        if (m_csBitmaps[1].hBitmap)    ::DeleteObject(m_csBitmaps[1].hBitmap);
166
167        // Destroy mask bitmaps
168        if (m_csBitmaps[0].hMask)    ::DeleteObject(m_csBitmaps[0].hMask);
169        if (m_csBitmaps[1].hMask)    ::DeleteObject(m_csBitmaps[1].hMask);
170    }
 // if
171
172    ::ZeroMemory(&m_csIcons, sizeof(m_csIcons));
173    ::ZeroMemory(&m_csBitmaps, sizeof(m_csBitmaps));
174}
 // End of FreeResources
175
176void CButtonST::PreSubclassWindow() 
177{
178    UINT nBS;
179
180    nBS = GetButtonStyle();
181
182    // Set initial control type
183    m_nTypeStyle = nBS & BS_TYPEMASK;
184
185    // Check if this is a checkbox
186    if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
187
188    // Set initial default state flag
189    if (m_nTypeStyle == BS_DEFPUSHBUTTON)
190    {
191        // Set default state for a default button
192        m_bIsDefault = TRUE;
193
194        // Adjust style for default button
195        m_nTypeStyle = BS_PUSHBUTTON;
196    }
 // If
197
198    // You should not set the Owner Draw before this call
199    // (don't use the resource editor "Owner Draw" or
200    // ModifyStyle(0, BS_OWNERDRAW) before calling PreSubclassWindow() )
201    ASSERT(m_nTypeStyle != BS_OWNERDRAW);
202
203    // Switch to owner-draw
204    ModifyStyle(BS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
205
206    CButton::PreSubclassWindow();
207}
 // End of PreSubclassWindow
208
209UINT CButtonST::OnGetDlgCode() 
210{
211    UINT nCode = CButton::OnGetDlgCode();
212
213    // Tell the system if we want default state handling
214    // (losing default state always allowed)
215    nCode |= (m_bIsDefault ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
216
217    return nCode;
218}
 // End of OnGetDlgCode
219
220BOOL CButtonST::PreTranslateMessage(MSG* pMsg) 
221{
222    InitToolTip();
223    m_ToolTip.RelayEvent(pMsg);
224    
225    if (pMsg->message == WM_LBUTTONDBLCLK)
226        pMsg->message = WM_LBUTTONDOWN;
227
228    return CButton::PreTranslateMessage(pMsg);
229}
 // End of PreTranslateMessage
230
231HBRUSH CButtonST::CtlColor(CDC* pDC, UINT nCtlColor) 
232{
233    return (HBRUSH)::GetStockObject(NULL_BRUSH); 
234}
 // End of CtlColor
235
236void CButtonST::OnSysColorChange() 
237{
238    CButton::OnSysColorChange();
239
240    m_dcBk.DeleteDC();
241    m_bmpBk.DeleteObject();    
242    SetDefaultColors();
243}
 // End of OnSysColorChange
244
245LRESULT CButtonST::OnSetStyle(WPARAM wParam, LPARAM lParam)
246{
247    UINT nNewType = (wParam & BS_TYPEMASK);
248
249    // Update default state flag
250    if (nNewType == BS_DEFPUSHBUTTON)
251    {
252        m_bIsDefault = TRUE;
253    }
 // if
254    else if (nNewType == BS_PUSHBUTTON)
255    {
256        // Losing default state always allowed
257        m_bIsDefault = FALSE;
258    }
 // if
259
260    // Can't change control type after owner-draw is set.
261    // Let the system process changes to other style bits
262    // and redrawing, while keeping owner-draw style
263    return DefWindowProc(BM_SETSTYLE,
264        (wParam & ~BS_TYPEMASK) | BS_OWNERDRAW, lParam);
265}
 // End of OnSetStyle
266
267LRESULT CButtonST::OnSetCheck(WPARAM wParam, LPARAM lParam)
268{
269    ASSERT(m_bIsCheckBox);
270
271    switch (wParam)
272    {
273        case BST_CHECKED:
274        case BST_INDETERMINATE:    // Indeterminate state is handled like checked state
275            SetCheck(1);
276            break;
277        default:
278            SetCheck(0);
279            break;
280    }
 // switch
281
282    return 0;
283}
 // End of OnSetCheck
284
285LRESULT CButtonST::OnGetCheck(WPARAM wParam, LPARAM lParam)
286{
287    ASSERT(m_bIsCheckBox);
288    return GetCheck();
289}
 // End of OnGetCheck
290
Posted on 2007-04-12 11:45 艾凡赫 阅读(1173) 评论(0)  编辑 收藏 引用 所属分类: MFC技术

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理