MFC-自绘控件(CStatic与CEdit)

示例图

MFC-自绘控件(CStatic与CEdit)_第1张图片

一、CStatic

主要是在主窗口中的OnCtlColor函数中进行自绘

HBRUSH CSelfDrawDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor == CTLCOLOR_STATIC) {
		pDC->SetBkMode(TRANSPARENT);
		if (pWnd->GetDlgCtrlID() == IDC_STATIC1) {
			pDC->SetTextColor(RGB(200, 200, 100));
		}
		return (HBRUSH)GetStockObject(NULL_BRUSH);
	}
	// TODO:  如果默认的不是所需画笔,则返回另一个画笔
	return hbr;
}

二、check box 与 radio box

改变背景透明

HBRUSH CSelfDrawDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	if (nCtlColor == CTLCOLOR_BTN || nCtlColor == CTLCOLOR_STATIC) {
		pDC->SetBkMode(TRANSPARENT);
		hbr = (HBRUSH) ::GetStockObject(NULL_BRUSH);

		UINT id = pWnd->GetDlgCtrlID();
		if (id == IDC_RADIO1 || id == IDC_CHECK1)
		{
			CRect rc;
			pWnd->GetWindowRect(&rc);
			ScreenToClient(&rc);
			CDC* dc = GetDC();
			//把父窗口背景图片先画到按钮上
			pDC->BitBlt(0, 0, rc.Width(), rc.Height(), dc, rc.left, rc.top, SRCCOPY);
			ReleaseDC(dc);
			return hbr;
		}
		if (nCtlColor == CTLCOLOR_STATIC) {
			pDC->SetTextColor(RGB(255, 200, 0));
		}
		return hbr;
	}
	
	return hbr;
}


三、CEdit

主要是继承CEdit这个类,简单的自绘一下边框的颜色

CSkinEdit.h

#pragma once
#include "afxwin.h"
class CSkinEdit :
	public CEdit
{
public:
	CSkinEdit();
	~CSkinEdit();
	DECLARE_DYNAMIC(CSkinEdit)
protected:
	DECLARE_MESSAGE_MAP()
public:
	BOOL m_bTracking;
	BOOL m_bHover;
	BOOL m_bFocus;
	CBrush m_ClientBrush;
	CBrush m_hOrderBrush;
	CBrush m_HotOrderBrush1;
	CBrush m_HotOrderBrush2;
public:
	LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
	LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
	void OnMouseMove(UINT nFlags, CPoint point);
	void OnSetFocus(CWnd* pOldWnd);
	void OnKillFocus(CWnd* pNewWnd);
	void OnNcPaint();
	void OnPaint();
	void DrawBorder();
};


CSkinEdit.cpp

#include "stdafx.h"
#include "SkinEdit.h"

IMPLEMENT_DYNAMIC(CSkinEdit, CEdit)

CSkinEdit::CSkinEdit()
{
	m_bTracking = m_bHover = m_bFocus = FALSE;
	m_ClientBrush.CreateSolidBrush(RGB(0,0,200));
	m_hOrderBrush.CreateSolidBrush(RGB(0, 100, 200));
	m_HotOrderBrush1.CreateSolidBrush(RGB(255, 255, 0));
	m_HotOrderBrush2.CreateSolidBrush(RGB(255, 200, 0));
}

CSkinEdit::~CSkinEdit()
{
	DeleteObject(m_ClientBrush);
	DeleteObject(m_hOrderBrush);
	DeleteObject(m_HotOrderBrush1);
	DeleteObject(m_HotOrderBrush2);
}


BEGIN_MESSAGE_MAP(CSkinEdit, CEdit)
	ON_WM_NCPAINT()
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
	ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
	ON_WM_MOUSEMOVE()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
END_MESSAGE_MAP()



// CSkinEdit 消息处理程序
void CSkinEdit::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bTracking)
	{
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(tme);
		tme.hwndTrack = m_hWnd;
		tme.dwFlags = TME_LEAVE | TME_HOVER;
		tme.dwHoverTime = 50;
		m_bTracking = (bool)_TrackMouseEvent(&tme);
	}

	CEdit::OnMouseMove(nFlags, point);
}
LRESULT CSkinEdit::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
	m_bTracking = FALSE;
	m_bHover = FALSE;
	DrawBorder();
	return 0;
}

LRESULT CSkinEdit::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
	m_bHover = TRUE;
	DrawBorder();
	return 0;
}
void CSkinEdit::OnSetFocus(CWnd* pOldWnd)
{
	CEdit::OnSetFocus(pOldWnd);
	m_bFocus = TRUE;
	DrawBorder();
}

void CSkinEdit::OnKillFocus(CWnd* pNewWnd)
{
	CEdit::OnKillFocus(pNewWnd);
	m_bFocus = FALSE;
	DrawBorder();
}
void CSkinEdit::OnNcPaint()
{
	CEdit::OnNcPaint();
	DrawBorder();
}
void CSkinEdit::OnPaint()
{
	CEdit::OnPaint();
	DrawBorder();
}

void CSkinEdit::DrawBorder()
{
	CDC * dc = GetWindowDC();
	CRect rc;
	GetWindowRect(&rc);
	rc = CRect(0, 0, rc.right - rc.left, rc.bottom - rc.top);
	if (m_bHover) {
		dc->FrameRect(rc, &m_HotOrderBrush1);
		rc.InflateRect(-1, -1);
		dc->FrameRect(rc, &m_HotOrderBrush2);
	}
	else if (m_bFocus) {
		dc->FrameRect(rc, &m_ClientBrush);
		rc.InflateRect(-1, -1);
		dc->FrameRect(rc, &m_hOrderBrush);
	}
	else {
		dc->FrameRect(rc, &m_ClientBrush);
		rc.InflateRect(-1, -1);
		dc->FrameRect(rc, &m_hOrderBrush);
	}
	ReleaseDC(dc);
	dc = NULL;
}


你可能感兴趣的:(CC++)