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

来源:互联网 发布:宏编程左键双击录制 编辑:程序博客网 时间:2024/04/30 07:19

 

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 windowclass CMyButton : public CButton{// Constructionpublic:CMyButton();// Attributespublic:    BOOL m_bOverControl;    UINT m_nTimerID;// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CMyButton)public:virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementationpublic:virtual ~CMyButton();// Generated message map functionsprotected://{{AFX_MSG(CMyButton)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg void OnTimer(UINT nIDEvent);//}}AFX_MSGDECLARE_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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMyButtonCMyButton::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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyButton message handlersvoid 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 handlerCButton::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);elsepDC->TextOut(pt.x, pt.y, strText);        pDC->SetBkMode(nMode);    }}void CMyButton::PreSubclassWindow() {CButton::PreSubclassWindow();ModifyStyle(0, BS_OWNERDRAW);// make the button owner drawn}


 

原创粉丝点击