CMyButton类----功能---- 鼠标经过此按钮时,按钮改变颜色

 

MyButton.h 

#if !defined(AFX_MYBUTTON_H__10CEB9E1_11F9_11D4_A2EA_0048543D92F7__INCLUDED_)
#define AFX_MYBUTTON_H__10CEB9E1_11F9_11D4_A2EA_0048543D92F7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h : header file
//

/
// CMyButton window

class CMyButton : public CButton
{
// Construction
public:
	CMyButton();

// Attributes
public:
    BOOL m_bOverControl;
    UINT m_nTimerID;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMyButton)
	public:
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
	protected:
	virtual void PreSubclassWindow();
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMyButton();

	// Generated message map functions
protected:
	//{{AFX_MSG(CMyButton)
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT nIDEvent);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYBUTTON_H__10CEB9E1_11F9_11D4_A2EA_0048543D92F7__INCLUDED_)


 

 

MyButton.cpp

// MyButton.cpp : implementation file
//

#include "stdafx.h"

#include "MyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CMyButton

CMyButton::CMyButton()
{
    m_bOverControl = FALSE;
    m_nTimerID     = 1;
}

CMyButton::~CMyButton()
{
}


BEGIN_MESSAGE_MAP(CMyButton, CButton)
	//{{AFX_MSG_MAP(CMyButton)
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CMyButton message handlers

void CMyButton::OnMouseMove(UINT nFlags, CPoint point) 
{
    if (!m_bOverControl)                    // Cursor has just moved over control
    {
        TRACE0("Entering control\n");

        m_bOverControl = TRUE;				// Set flag telling us the mouse is in
        Invalidate();                       // Force a redraw

        SetTimer(m_nTimerID, 100, NULL);    // Keep checking back every 1/10 sec
    }
	
	CButton::OnMouseMove(nFlags, point);    // drop through to default handler
}

void CMyButton::OnTimer(UINT nIDEvent) 
{
    // Where is the mouse?
    CPoint p(GetMessagePos());
    ScreenToClient(&p);

    // Get the bounds of the control (just the client area)
    CRect rect;
    GetClientRect(rect);

    // Check the mouse is inside the control
    if (!rect.PtInRect(p))
    {
        TRACE0("Leaving control\n");

        // if not then stop looking...
        m_bOverControl = FALSE;
        KillTimer(m_nTimerID);

        // ...and redraw the control
        Invalidate();
    }
	
	// drop through to default handler
	CButton::OnTimer(nIDEvent);
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rect = lpDrawItemStruct->rcItem;
	UINT state = lpDrawItemStruct->itemState;

	CString strText;
	GetWindowText(strText);

    // draw the control edges (DrawFrameControl is handy!)
	if (state & ODS_SELECTED)
        pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
    else
        pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH);

    // Fill the interior color if necessary
    
    rect.DeflateRect( CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
    if (m_bOverControl)
        pDC->FillSolidRect(rect, RGB(233, 233, 0)); // yellow

    // Draw the text
    if (!strText.IsEmpty())
    {
        CSize Extent = pDC->GetTextExtent(strText);
		CPoint pt( rect.CenterPoint().x - Extent.cx/2, rect.CenterPoint().y - Extent.cy/2 );

		if (state & ODS_SELECTED) 
            pt.Offset(1,1);

		int nMode = pDC->SetBkMode(TRANSPARENT);

		if (state & ODS_DISABLED)
			pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
		else
			pDC->TextOut(pt.x, pt.y, strText);

        pDC->SetBkMode(nMode);
    }
}

void CMyButton::PreSubclassWindow() 
{
	CButton::PreSubclassWindow();

	ModifyStyle(0, BS_OWNERDRAW);	// make the button owner drawn
}


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以创建一个CMyDialogBar,继承自CDialogBar。然后在OnInitDialog函数中,使用Create函数动态创建您所需要的按钮,根据数量动态生成。可以使用CButton或者CMyButton派生自CButton。 在按钮上修改文本颜色可以使用SetTextColor函数,修改背景可以使用SetBkColor函数,这些函数都是CButton的成员函数。 下面是一个简单的示例代码,仅供参考: ``` // MyButton.h #pragma once class CMyButton : public CButton { public: CMyButton(); virtual ~CMyButton(); void SetTextColor(COLORREF color); void SetBkColor(COLORREF color); protected: COLORREF m_textColor; COLORREF m_bkColor; afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); DECLARE_MESSAGE_MAP() }; // MyButton.cpp #include "stdafx.h" #include "MyButton.h" CMyButton::CMyButton() : m_textColor(RGB(0, 0, 0)) , m_bkColor(RGB(255, 255, 255)) { } CMyButton::~CMyButton() { } void CMyButton::SetTextColor(COLORREF color) { m_textColor = color; Invalidate(); } void CMyButton::SetBkColor(COLORREF color) { m_bkColor = color; Invalidate(); } HBRUSH CMyButton::CtlColor(CDC* pDC, UINT nCtlColor) { HBRUSH hbr = CButton::CtlColor(pDC, nCtlColor); if (nCtlColor == CTLCOLOR_BTN) { pDC->SetTextColor(m_textColor); pDC->SetBkColor(m_bkColor); hbr = CreateSolidBrush(m_bkColor); } return hbr; } BEGIN_MESSAGE_MAP(CMyButton, CButton) ON_WM_CTLCOLOR_REFLECT() END_MESSAGE_MAP() // MyDialogBar.h #pragma once class CMyDialogBar : public CDialogBar { public: CMyDialogBar(); virtual ~CMyDialogBar(); BOOL Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID); protected: virtual BOOL OnInitDialog(); DECLARE_MESSAGE_MAP() private: std::vector<CMyButton*> m_btnList; }; // MyDialogBar.cpp #include "stdafx.h" #include "MyDialogBar.h" #include "MyButton.h" CMyDialogBar::CMyDialogBar() { } CMyDialogBar::~CMyDialogBar() { for (size_t i = 0; i < m_btnList.size(); i++) { delete m_btnList[i]; } } BOOL CMyDialogBar::Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID) { BOOL bRet = CDialogBar::Create(pParentWnd, nIDTemplate, nStyle, nID); // Create buttons for (int i = 0; i < 5; i++) { CMyButton* pBtn = new CMyButton; pBtn->Create(_T("Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 0, 0), this, 100 + i); pBtn->SetTextColor(RGB(255, 0, 0)); pBtn->SetBkColor(RGB(255, 255, 0)); m_btnList.push_back(pBtn); } return bRet; } BOOL CMyDialogBar::OnInitDialog() { CDialogBar::OnInitDialog(); // Set button positions CRect rc; GetClientRect(&rc); int nBtnWidth = rc.Width() / m_btnList.size(); int nBtnHeight = rc.Height(); for (size_t i = 0; i < m_btnList.size(); i++) { m_btnList[i]->MoveWindow(i * nBtnWidth, 0, nBtnWidth, nBtnHeight); } return TRUE; } BEGIN_MESSAGE_MAP(CMyDialogBar, CDialogBar) END_MESSAGE_MAP() ``` 这个示例代码是在对话框中创建一个CMyDialogBar的对象,实现了根据数量动态生成按钮,并且按钮上面的文本颜色和背景可以修改。您可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值