效果演示
上位机MFC平面按钮实例
当前按钮集成类是在CButton基础之上派生了一个叫做CButtonST的类实现的,
其特征如下:
标准CButton属性。
文本和图标按钮。
仅有文本或仅有图标按钮。
16色和256色图标支持。
多种平面风格。
在运行时间从酷风格改变成标准风格。
具有两个图标:一个用于鼠标光标在按钮上,一个用于鼠标光标不在光标上。
每种颜色可以定制。
可以通过DDX_调用。
包含所有代码及可编译后的可执行程序。
实现过程
1.新建立一基于对话框工程,按照上图添加按钮控件,并进行排版。
2.将工程根目录两文件BTNST.CPP,BTNST.H,复制到自己工程目录下,
并加载到自己项目中,文件代码在帖子底部,也可以复制新建两文件。
这样工程中就多了两个类CButtonST,CMemDC,如图:
上位机MFC平面按钮实例
3.在主对话框中包含上面的头文件,并添加类实例对象,及对象初始化
#include "BtnST.h"
private:
CButtonST m_btnAbout16;
CButtonST m_btnExit16;
CButtonST m_btnScrew16;
CButtonST m_btnRight16;
CButtonST m_btnLeft16;
CButtonST m_btnQuestion16;
CButtonST m_btnText16;
CButtonST m_btnView16;
CButtonST m_btnExplore16;
CButtonST m_btnCancelBor16;
CButtonST m_btnOkBor16;
CButtonST m_btnCDGold256;
CButtonST m_btnCancel256;
CButtonST m_btnOk256;
CButtonST m_btnLamp256;
CButtonST m_btnHand256;
CButtonST m_btnOk16;
CButtonST m_btnCancel16;
CButtonST m_btnZip256;
m_btnHand256.SubclassDlgItem(IDC_HAND256, this);
m_btnHand256.SetIcon(IDI_HAND256);
// Create the Lamp 256 colors button
m_btnLamp256.SubclassDlgItem(IDC_LAMP256, this);
m_btnLamp256.SetIcon(IDI_LAMP256, IDI_LAMP2);
// Don't draw border for this button
m_btnLamp256.DrawBorder(FALSE);
// Create the CD-Gold 256 colors button
m_btnCDGold256.SubclassDlgItem(IDC_CDGOLD256, this);
m_btnCDGold256.SetIcon(IDI_CDGOLD256);
// Don't draw border for this button
m_btnCDGold256.DrawBorder(FALSE);
// Create the Ok 256 colors button
m_btnOk256.SubclassDlgItem(IDC_OK256, this);
m_btnOk256.SetIcon(IDI_OK256);
// Create the Cancel 256 colors button
m_btnCancel256.SubclassDlgItem(IDC_CANCEL256, this);
m_btnCancel256.SetIcon(IDI_CANCEL256);
// Create the Ok 16 colors button
m_btnOk16.SubclassDlgItem(IDC_OK16, this);
m_btnOk16.SetIcon(IDI_OK16);
// Create the Cancel 16 colors button
m_btnCancel16.SubclassDlgItem(IDC_CANCEL16, this);
m_btnCancel16.SetIcon(IDI_CANCEL16);
// Create the OkBor 16 colors button
m_btnOkBor16.SubclassDlgItem(IDC_OKBOR16, this);
m_btnOkBor16.SetIcon(IDI_OKBOR16, IDI_OKBOR2);
m_btnOkBor16.SetActiveBgColor(RGB(220,220,220));
// Create the CancelBor 16 colors button
m_btnCancelBor16.SubclassDlgItem(IDC_CANCELBOR16, this);
m_btnCancelBor16.SetIcon(IDI_CANCELBOR16, IDI_CANCELBOR2);
m_btnCancelBor16.SetActiveBgColor(RGB(220,220,220));
// Create the Explore 16 colors button
m_btnExplore16.SubclassDlgItem(IDC_EXPLORE16, this);
m_btnExplore16.SetIcon(IDI_EXPLORE16);
// Create the View 16 colors button
m_btnView16.SubclassDlgItem(IDC_VIEW16, this);
m_btnView16.SetIcon(IDI_VIEW16);
// Create the Text 16 colors button
m_btnText16.SubclassDlgItem(IDC_TEXT16, this);
m_btnText16.SetIcon(IDI_TEXT16);
// Create the Question 16 colors button
m_btnQuestion16.SubclassDlgItem(IDC_QUESTION16, this);
m_btnQuestion16.SetIcon(IDI_QUESTION16);
// Create the Left 16 colors button
m_btnLeft16.SubclassDlgItem(IDC_LEFT16, this);
m_btnLeft16.SetIcon(IDI_LEFT16);
// Create the Right 16 colors button
m_btnRight16.SubclassDlgItem(IDC_RIGHT16, this);
m_btnRight16.SetIcon(IDI_RIGHT16);
// Create the Screw 16 colors button
m_btnScrew16.SubclassDlgItem(IDC_SCREW16, this);
m_btnScrew16.SetIcon(IDI_SCREW16);
// Set some color effect
m_btnScrew16.SetActiveFgColor(RGB(255,0,0));
m_btnScrew16.SetInactiveFgColor(RGB(0,255,0));
// Create the Exit 16 colors button
m_btnExit16.SubclassDlgItem(IDOK, this);
m_btnExit16.SetIcon(IDI_EXIT16);
// Draw this button as a standard button
m_btnExit16.SetFlat(FALSE);
// Assign a custom cursor
m_btnExit16.SetBtnCursor(IDC_HAND);
// Create the About 16 colors button
COLORREF crStandard = m_btnExit16.GetInactiveBgColor();
m_btnAbout16.SubclassDlgItem(IDC_ABOUT16, this);
m_btnAbout16.SetIcon(IDI_ABOUT16);
// Align icon vertically
m_btnAbout16.SetAlign(CButtonST::ST_ALIGN_VERT);
// Set some color effect
m_btnAbout16.SetInactiveBgColor(crStandard - RGB(20,20,20));
m_btnAbout16.SetActiveBgColor(crStandard + RGB(20,20,20));
3.可以看到初始按钮对象时,使用了相当多的图标 资源,这里一并使用例程根目录的图标,
也可自己制作,资源ID要一致:
IDI_ABOUT16
IDI_CANCEL16IDI_CANCEL256
IDI_CANCEL256
IDI_CANCELBOR2
IDI_CDGOLD256
IDI_EXIT16
IDI_EXPLORE16
4.编译运行就可以查看效果。下面是用到的类文件源代码
- #ifndef _BTNST_H
- #define _BTNST_H
- #if _MSC_VER >= 1000
- #pragma once
- #endif // _MSC_VER >= 1000
- // CBtnST.h : header file
- //
- // Comment this if you don't want that CButtonST hilights itself
- // also when the window is inactive (like happens in Internet Explorer)
- #define ST_LIKEIE
- // Comment this if you don't want to use CMemDC class
- #define ST_USE_MEMDC
- /////////////////////////////////////////////////////////////////////////////
- // CButtonST window
- class CButtonST : public CButton
- {
- // Construction
- public:
- CButtonST();
- ~CButtonST();
- enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT};
- // Attributes
- public:
- // Operations
- public:
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CButtonST)
- public:
- virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
- protected:
- virtual void PreSubclassWindow();
- //}}AFX_VIRTUAL
- // Implementation
- public:
- BOOL SetBtnCursor(int nCursorId = -1);
- void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);
- BOOL GetFlatFocus();
- void SetDefaultActiveFgColor(BOOL bRepaint = FALSE);
- void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
- const COLORREF GetActiveFgColor();
-
- void SetDefaultActiveBgColor(BOOL bRepaint = FALSE);
- void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
- const COLORREF GetActiveBgColor();
-
- void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE);
- void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
- const COLORREF GetInactiveFgColor();
- void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE);
- void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
- const COLORREF GetInactiveBgColor();
- void SetShowText(BOOL bShow = TRUE);
- BOOL GetShowText();
- void SetAlign(int nAlign);
- int GetAlign();
- void SetFlat(BOOL bState = TRUE);
- BOOL GetFlat();
- void DrawBorder(BOOL bEnable = TRUE);
- void SetIcon(int nIconInId, int nIconOutId = NULL, BYTE cx = 32, BYTE cy = 32);
- static const short GetVersionI();
- static const char* GetVersionC();
- protected:
- //{{AFX_MSG(CButtonST)
- afx_msg void OnCaptureChanged(CWnd *pWnd);
- afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
- afx_msg void OnKillFocus(CWnd* pNewWnd);
- afx_msg void OnMouseMove(UINT nFlags, CPoint point);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- private:
- void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled);
- int m_nAlign;
- BOOL m_bShowText;
- BOOL m_bDrawBorder;
- BOOL m_bIsFlat;
- BOOL m_MouseOnButton;
- BOOL m_bDrawFlatFocus;
- HCURSOR m_hCursor;
- HICON m_hIconIn;
- HICON m_hIconOut;
- BYTE m_cyIcon;
- BYTE m_cxIcon;
- COLORREF m_crInactiveBg;
- COLORREF m_crInactiveFg;
- COLORREF m_crActiveBg;
- COLORREF m_crActiveFg;
- };
- #ifdef ST_USE_MEMDC
- class CMemDC : public CDC
- {
- public:
- // constructor sets up the memory DC
- CMemDC(CDC* pDC) : CDC()
- {
- ASSERT(pDC != NULL);
- m_pDC = pDC;
- m_pOldBitmap = NULL;
- m_bMemDC = !pDC->IsPrinting();
-
- if (m_bMemDC) // Create a Memory DC
- {
- pDC->GetClipBox(&m_rect);
- CreateCompatibleDC(pDC);
- m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
- m_pOldBitmap = SelectObject(&m_bitmap);
- SetWindowOrg(m_rect.left, m_rect.top);
- }
- else // Make a copy of the relevent parts of the current DC for printing
- {
- m_bPrinting = pDC->m_bPrinting;
- m_hDC = pDC->m_hDC;
- m_hAttribDC = pDC->m_hAttribDC;
- }
- }
-
- // Destructor copies the contents of the mem DC to the original DC
- ~CMemDC()
- {
- if (m_bMemDC)
- {
- // Copy the offscreen bitmap onto the screen.
- m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
- this, m_rect.left, m_rect.top, SRCCOPY);
- //Swap back the original bitmap.
- SelectObject(m_pOldBitmap);
- } else {
- // All we need to do is replace the DC with an illegal value,
- // this keeps us from accidently deleting the handles associated with
- // the CDC that was passed to the constructor.
- m_hDC = m_hAttribDC = NULL;
- }
- }
- // Allow usage as a pointer
- CMemDC* operator->() {return this;}
-
- // Allow usage as a pointer
- operator CMemDC*() {return this;}
- private:
- CBitmap m_bitmap; // Offscreen bitmap
- CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
- CDC* m_pDC; // Saves CDC passed in constructor
- CRect m_rect; // Rectangle of drawing area.
- BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
- };
- #endif
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
- #endif
复制代码
- // BtnST.cpp : implementation file
- //
- #include "stdafx.h"
- #include "BtnST.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CButtonST
- CButtonST::CButtonST()
- {
- m_MouseOnButton = FALSE;
- m_hIconIn = NULL;
- m_hIconOut = NULL;
- m_cxIcon = 0;
- m_cyIcon = 0;
- m_hCursor = NULL;
-
- // Default type is "flat" button
- m_bIsFlat = TRUE;
-
- // By default draw border in "flat" button
- m_bDrawBorder = TRUE;
-
- // By default icon is aligned horizontally
- m_nAlign = ST_ALIGN_HORIZ;
-
- // By default show the text button
- m_bShowText = TRUE;
-
- // By default, for "flat" button, don't draw the focus rect
- m_bDrawFlatFocus = FALSE;
-
- SetDefaultInactiveBgColor();
- SetDefaultInactiveFgColor();
- SetDefaultActiveBgColor();
- SetDefaultActiveFgColor();
- } // End of CButtonST
- CButtonST::~CButtonST()
- {
- // Destroy the icons (if any)
- if (m_hIconIn != NULL) ::DeleteObject(m_hIconIn);
- if (m_hIconOut != NULL) ::DeleteObject(m_hIconOut);
- // Destroy the cursor (if any)
- if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
- } // End of ~CButtonST
- BEGIN_MESSAGE_MAP(CButtonST, CButton)
- //{{AFX_MSG_MAP(CButtonST)
- ON_WM_CAPTURECHANGED()
- ON_WM_SETCURSOR()
- ON_WM_KILLFOCUS()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CButtonST::SetIcon(int nIconInId, int nIconOutId, BYTE cx, BYTE cy)
- {
- HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId),
- RT_GROUP_ICON);
- // Set icon when the mouse is IN the button
- m_hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
-
- // Set icon when the mouse is OUT the button
- m_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
-
- m_cxIcon = cx;
- m_cyIcon = cy;
- RedrawWindow();
- } // End of SetIcon
- BOOL CButtonST::SetBtnCursor(int nCursorId)
- {
- HINSTANCE hInstResource;
- // Destroy any previous cursor
- if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
- m_hCursor = NULL;
- // If we want a cursor
- if (nCursorId != -1)
- {
- hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId),
- RT_GROUP_CURSOR);
- // Load icon resource
- m_hCursor = (HCURSOR)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
- // If something wrong then return FALSE
- if (m_hCursor == NULL) return FALSE;
- }
- return TRUE;
- } // End of SetBtnCursor
- void CButtonST::SetFlat(BOOL bState)
- {
- m_bIsFlat = bState;
- Invalidate();
- } // End of SetFlat
- BOOL CButtonST::GetFlat()
- {
- return m_bIsFlat;
- } // End of GetFlat
- void CButtonST::SetAlign(int nAlign)
- {
- switch (nAlign)
- {
- case ST_ALIGN_HORIZ:
- m_nAlign = ST_ALIGN_HORIZ;
- break;
- case ST_ALIGN_VERT:
- m_nAlign = ST_ALIGN_VERT;
- break;
- }
- Invalidate();
- } // End of SetAlign
- int CButtonST::GetAlign()
- {
- return m_nAlign;
- } // End of GetAlign
- void CButtonST::DrawBorder(BOOL bEnable)
- {
- m_bDrawBorder = bEnable;
- } // End of DrawBorder
- const char* CButtonST::GetVersionC()
- {
- return "2.3";
- } // End of GetVersionC
- const short CButtonST::GetVersionI()
- {
- return 23; // Divide by 10 to get actual version
- } // End of GetVersionI
- void CButtonST::SetShowText(BOOL bShow)
- {
- m_bShowText = bShow;
- Invalidate();
- } // End of SetShowText
- BOOL CButtonST::GetShowText()
- {
- return m_bShowText;
- } // End of GetShowText
- void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
- {
- CWnd* pWnd; // Finestra attiva
- CWnd* pParent; // Finestra che contiene il bottone
- CButton::OnMouseMove(nFlags, point);
- // If the mouse enter the button with the left button pressed
- // then do nothing
- if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;
- // If our button is not flat then do nothing
- if (m_bIsFlat == FALSE) return;
- pWnd = GetActiveWindow();
- pParent = GetOwner();
- if ((GetCapture() != this) &&
- (
- #ifndef ST_LIKEIE
- pWnd != NULL &&
- #endif
- pParent != NULL))
- {
- m_MouseOnButton = TRUE;
- //SetFocus(); // Thanks Ralph!
- SetCapture();
- Invalidate();
- }
- else
- {
- CRect rc;
- GetClientRect(&rc);
- if (!rc.PtInRect(point))
- {
- // Redraw only if mouse goes out
- if (m_MouseOnButton == TRUE)
- {
- m_MouseOnButton = FALSE;
- Invalidate();
- }
- // If user is NOT pressing left button then release capture!
- if (!(nFlags & MK_LBUTTON)) ReleaseCapture();
- }
- }
- } // End of OnMouseMove
- void CButtonST::OnKillFocus(CWnd * pNewWnd)
- {
- CButton::OnKillFocus(pNewWnd);
- // If our button is not flat then do nothing
- if (m_bIsFlat == FALSE) return;
- if (m_MouseOnButton == TRUE)
- {
- m_MouseOnButton = FALSE;
- Invalidate();
- }
- } // End of OnKillFocus
- void CButtonST::OnCaptureChanged(CWnd *pWnd)
- {
- if (m_MouseOnButton == TRUE)
- {
- ReleaseCapture();
- Invalidate();
- }
- // Call base message handler
- CButton::OnCaptureChanged(pWnd);
- } // End of OnCaptureChanged
- void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- #ifdef ST_USE_MEMDC
- CDC *pdrawDC = CDC::FromHandle(lpDIS->hDC);
- CMemDC memDC(pdrawDC);
- CDC *pDC = &memDC;
- #else
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
- #endif
- CPen *pOldPen;
- BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
- BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);
- BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
- CRect itemRect = lpDIS->rcItem;
- if (m_bIsFlat == FALSE)
- {
- if (bIsFocused)
- {
- CBrush br(RGB(0,0,0));
- pDC->FrameRect(&itemRect, &br);
- itemRect.DeflateRect(1, 1);
- }
- }
- // Prepare draw... paint button's area with background color
- COLORREF bgColor;
- if ((m_MouseOnButton == TRUE) || (bIsPressed))
- bgColor = GetActiveBgColor();
- else
- bgColor = GetInactiveBgColor();
- CBrush br(bgColor);
- pDC->FillRect(&itemRect, &br);
- // Disegno lo sfondo del bottone
- //CBrush br(GetSysColor(COLOR_BTNFACE));
- //pDC->FillRect(&itemRect, &br);
- // Draw pressed button
- if (bIsPressed)
- {
- if (m_bIsFlat == TRUE)
- {
- if (m_bDrawBorder == TRUE)
- {
- CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
- CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Grigio scuro
- // Disegno i bordi a sinistra e in alto
- // Dark gray line
- pOldPen = pDC->SelectObject(&penBtnShadow);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.left, itemRect.top);
- pDC->LineTo(itemRect.right, itemRect.top);
- // Disegno i bordi a destra e in basso
- // White line
- pDC->SelectObject(penBtnHiLight);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.top-1);
- //
- pDC->SelectObject(pOldPen);
- }
- }
- else
- {
- CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
- pDC->FrameRect(&itemRect, &brBtnShadow);
- }
- }
- else // ...else draw non pressed button
- {
- CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
- CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
- CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
- CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
- if (m_bIsFlat == TRUE)
- {
- if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
- {
- // Disegno i bordi a sinistra e in alto
- // White line
- pOldPen = pDC->SelectObject(&penBtnHiLight);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.left, itemRect.top);
- pDC->LineTo(itemRect.right, itemRect.top);
- // Disegno i bordi a destra e in basso
- // Dark gray line
- pDC->SelectObject(penBtnShadow);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.top-1);
- //
- pDC->SelectObject(pOldPen);
- }
- }
- else
- {
- // Disegno i bordi a sinistra e in alto
- // White line
- pOldPen = pDC->SelectObject(&penBtnHiLight);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.left, itemRect.top);
- pDC->LineTo(itemRect.right, itemRect.top);
- // Light gray line
- pDC->SelectObject(pen3DLight);
- pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
- pDC->LineTo(itemRect.left+1, itemRect.top+1);
- pDC->LineTo(itemRect.right, itemRect.top+1);
- // Disegno i bordi a destra e in basso
- // Black line
- pDC->SelectObject(pen3DDKShadow);
- pDC->MoveTo(itemRect.left, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
- pDC->LineTo(itemRect.right-1, itemRect.top-1);
- // Dark gray line
- pDC->SelectObject(penBtnShadow);
- pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
- pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
- pDC->LineTo(itemRect.right-2, itemRect.top);
- //
- pDC->SelectObject(pOldPen);
- }
- }
- // Read the button title
- CString sTitle;
- GetWindowText(sTitle);
- // If we don't want the title displayed
- if (m_bShowText == FALSE) sTitle.Empty();
- CRect captionRect = lpDIS->rcItem;
- // Draw the icon
- if (m_hIconIn != NULL)
- {
- DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
- }
- // Write the button title (if any)
- if (sTitle.IsEmpty() == FALSE)
- {
- // Disegno la caption del bottone
- // Se il bottone e' premuto muovo la captionRect di conseguenza
- if (bIsPressed)
- captionRect.OffsetRect(1, 1);
-
- // ONLY FOR DEBUG
- // Evidenzia il rettangolo in cui verra' centrata la caption
- //CBrush brBtnShadow(RGB(255, 0, 0));
- //pDC->FrameRect(&captionRect, &brBtnShadow);
- #ifdef ST_USE_MEMDC
- // Get dialog's font
- CFont *pCurrentFont = GetFont();
- CFont *pOldFont = pDC->SelectObject(pCurrentFont);
- #endif
- if ((m_MouseOnButton == TRUE) || (bIsPressed))
- {
- pDC->SetTextColor(GetActiveFgColor());
- pDC->SetBkColor(GetActiveBgColor());
- }
- else
- {
- pDC->SetTextColor(GetInactiveFgColor());
- pDC->SetBkColor(GetInactiveBgColor());
- }
- // Center text
- CRect centerRect = captionRect;
- pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
- captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
- /* RFU
- captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
- captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
- */
- pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
- TRUE, 0, (CBrush*)NULL);
- #ifdef ST_USE_MEMDC
- pDC->SelectObject(pOldFont);
- #endif
- }
- if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
- {
- // Draw the focus rect
- if (bIsFocused)
- {
- CRect focusRect = itemRect;
- focusRect.DeflateRect(3, 3);
- pDC->DrawFocusRect(&focusRect);
- }
- }
- } // End of DrawItem
- void CButtonST::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
- {
- CRect iconRect = rcItem;
- switch (m_nAlign)
- {
- case ST_ALIGN_HORIZ:
- if (title->IsEmpty())
- {
- // Center the icon horizontally
- iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
- }
- else
- {
- // L'icona deve vedersi subito dentro il focus rect
- iconRect.left += 3;
- captionRect->left += m_cxIcon + 3;
- }
- // Center the icon vertically
- iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
- break;
- case ST_ALIGN_VERT:
- // Center the icon horizontally
- iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
- if (title->IsEmpty())
- {
- // Center the icon vertically
- iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
- }
- else
- {
- captionRect->top += m_cyIcon;
- }
- break;
- }
-
- // If button is pressed then press the icon also
- if (IsPressed) iconRect.OffsetRect(1, 1);
- // Ole'!
- pDC->DrawState(iconRect.TopLeft(),
- iconRect.Size(),
- (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut,
- (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
- (CBrush*)NULL);
- } // End of DrawTheIcon
- void CButtonST::PreSubclassWindow()
- {
- // Add BS_OWNERDRAW style
- SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
- CButton::PreSubclassWindow();
- } // End of PreSubclassWindow
- void CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint)
- {
- m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE);
- if (bRepaint == TRUE) Invalidate();
- } // End of SetDefaultInactiveBgColor
- void CButtonST::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint)
- {
- m_crInactiveBg = crNew;
- if (bRepaint == TRUE) Invalidate();
- } // End of SetInactiveBgColor
- const COLORREF CButtonST::GetInactiveBgColor()
- {
- return m_crInactiveBg;
- } // End of GetInactiveBgColor
- void CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint)
- {
- m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT);
- if (bRepaint == TRUE) Invalidate();
- } // End of SetDefaultInactiveFgColor
- void CButtonST::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint)
- {
- m_crInactiveFg = crNew;
- if (bRepaint == TRUE) Invalidate();
- } // End of SetInactiveFgColor
- const COLORREF CButtonST::GetInactiveFgColor()
- {
- return m_crInactiveFg;
- } // End of GetInactiveFgColor
- void CButtonST::SetDefaultActiveBgColor(BOOL bRepaint)
- {
- m_crActiveBg = ::GetSysColor(COLOR_BTNFACE);
- if (bRepaint == TRUE) Invalidate();
- } // End of SetDefaultActiveBgColor
- void CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
- {
- m_crActiveBg = crNew;
- if (bRepaint == TRUE) Invalidate();
- } // End of SetActiveBgColor
- const COLORREF CButtonST::GetActiveBgColor()
- {
- return m_crActiveBg;
- } // End of GetActiveBgColor
- void CButtonST::SetDefaultActiveFgColor(BOOL bRepaint)
- {
- m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT);
- if (bRepaint == TRUE) Invalidate();
- } // End of SetDefaultActiveFgColor
- void CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
- {
- m_crActiveFg = crNew;
- if (bRepaint == TRUE) Invalidate();
- } // End of SetActiveFgColor
- const COLORREF CButtonST::GetActiveFgColor()
- {
- return m_crActiveFg;
- } // End of GetActiveFgColor
- void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
- {
- m_bDrawFlatFocus = bDrawFlatFocus;
-
- // Repaint the button
- if (bRepaint == TRUE) Invalidate();
- } // End of SetFlatFocus
- BOOL CButtonST::GetFlatFocus()
- {
- return m_bDrawFlatFocus;
- } // End of GetFlatFocus
- BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- // If a cursor was specified then use it!
- if (m_hCursor != NULL)
- {
- ::SetCursor(m_hCursor);
- return TRUE;
- }
- return CButton::OnSetCursor(pWnd, nHitTest, message);
- } // End of OnSetCursor
- #undef ST_USE_MEMDC
- #undef ST_LIKE
复制代码
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|