⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 focolorbutton.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// Copyright UCanCode Software Technology Inc, All Rights Reserved
// You can contact us.
// Support@UCanCode.net
// http://www.ucancode.net
// FOColorButton.cpp: implementation of the CFOColorButton class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FOColorButton.h"

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

// CFOColorButton
IMPLEMENT_DYNAMIC(CFOColorButton, CButton)

CFOColorButton::CFOColorButton() 
{  
  m_crColor = RGB(255,255,255);
  m_crOldColor = RGB(0,0,0);
} 



CFOColorButton::~CFOColorButton()
{
} 


BEGIN_MESSAGE_MAP(CFOColorButton, CButton)
	//{{AFX_MSG_MAP(CFOColorButton)
	ON_WM_GETDLGCODE()
	ON_WM_SYSCOLORCHANGE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CFOColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel)
{
	if (!SubclassDlgItem(nID, pParent))
		return FALSE;

	m_crOldColor = FGColor;
	m_crColor = BGColor; 
	m_disabled = DisabledColor;
	m_bevel = nBevel;

	return TRUE;
} 

void CFOColorButton::SetButtonColor(COLORREF crColor)
{
	m_crColor = crColor;
	Invalidate();
}

void CFOColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	CRect rectClient;
	GetClientRect(rectClient);
	CDC dc;
	dc.CreateCompatibleDC(pDC);
	CBitmap bmpMem;
	bmpMem.CreateCompatibleBitmap(pDC, rectClient.Width(), rectClient.Height());
	CBitmap* pBmpOld = dc.SelectObject(&bmpMem);

	dc.FillSolidRect(rectClient,::GetSysColor(COLOR_BTNFACE));

	UINT state = lpDIS->itemState; 
	CRect focusRect, btnRect;
	focusRect.CopyRect(&lpDIS->rcItem); 
	btnRect.CopyRect(&lpDIS->rcItem); 
	
	//
	// Set the focus rectangle to just past the border decoration
	//
	focusRect.left += 4;
    focusRect.right -= 4;
    focusRect.top += 4;
    focusRect.bottom -= 4;
      
	//
	// Draw and label the button using draw methods 
	//
    DrawFilledRect(&dc, btnRect, m_crColor); 
    DrawFrame(&dc, btnRect, 2);

	//
	// Now, depending upon the state, redraw the button (down image) if it is selected,
	// place a focus rectangle on it, or redisplay the caption if it is disabled
	//
	if (state & ODS_FOCUS) 
	{
		DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
		if (state & ODS_SELECTED)
		{ 
    		DrawFilledRect(&dc, btnRect, m_crColor); 
    		DrawFrame(&dc, btnRect, -1);
			DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
		}
	}
	pDC->BitBlt(rectClient.left, rectClient.top,	rectClient.Width(), rectClient.Height(), &dc, 0, 0, SRCCOPY);
	
	dc.SelectObject(pBmpOld);
} 


void CFOColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
{ 
	Inset;
	DC->Draw3dRect(R,RGB(255,255,255),RGB(0,0,0));
	R.DeflateRect(1,1,1,1);
	DC->Draw3dRect(R,gfxData.m_crBtnFace,gfxData.m_crBtnShadow);
}



void CFOColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{ 
	R.DeflateRect(4,4,4,4);

	CPoint ptCenter = R.CenterPoint();
	CRect rc;
	rc = R;
	rc.bottom = ptCenter.y;

	CBrush B;
	B.CreateSolidBrush(color);
	DC->FillRect(rc, &B);
	
	CBrush B1;
	B1.CreateSolidBrush(m_crOldColor);
	rc = R;
	rc.top = ptCenter.y;
	DC->FillRect(rc,&B1);

}
 

void CFOColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
{ 
	CPen newPen;
	newPen.CreatePen(PS_SOLID, 1, color);
	CPen *oldPen = DC->SelectObject(&newPen);
	DC->MoveTo(EndPoints.left, EndPoints.top);
	DC->LineTo(EndPoints.right, EndPoints.bottom);
	DC->SelectObject(oldPen);
    newPen.DeleteObject();
}

void CFOColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
{ 
	CPen newPen;
	newPen.CreatePen(PS_SOLID, 1, color);
	CPen *oldPen = DC->SelectObject(&newPen);
	DC->MoveTo(left, top);
	DC->LineTo(right, bottom);
	DC->SelectObject(oldPen);
    newPen.DeleteObject();
}


UINT CFOColorButton::OnGetDlgCode() 
{
	return DLGC_WANTARROWS;
}

void CFOColorButton::OnSysColorChange() 
{
	CButton::OnSysColorChange();

	Invalidate ();
	UpdateWindow ();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -