VC IP地址控件(CIPAddressCtrl )的自绘

清泛原创
先看效果图:


代码:
.h
#pragma once 

class CMyIPCtrl : public CIPAddressCtrl
{
	DECLARE_DYNAMIC(CMyIPCtrl)

public:
	CMyIPCtrl();
	virtual ~CMyIPCtrl();

	//设置字体
	void SetEditFont(CFont* pFont) { m_pSetFont = pFont; }

protected:
	DECLARE_MESSAGE_MAP()

private:

	COLORREF		m_clrBackground;	//背景颜色
	COLORREF		m_clrFrame;			//边框颜色
	COLORREF		m_clrText;			//文字颜色
	
	CBrush			m_brushBkg;			//背景画刷

	CFont*			m_pfDefault;
	CFont*			m_pSetFont;

public:
	afx_msg void OnNcPaint();
	afx_msg HBRUSH OnCtlColor(CDC* /*pDC*/, CWnd* /*pWnd*/, UINT /*nCtlColor*/);
};
.cpp:
#include "stdafx.h" 
#include "MyIPCtrl.h"
#include "../../MemDC.h"
#include "../../CommonFunc.h"

#ifdef _DEBUG
#define new DEBUG_NEW 
#endif

IMPLEMENT_DYNAMIC(CMyIPCtrl, CIPAddressCtrl)
CMyIPCtrl::CMyIPCtrl() : m_pSetFont(NULL)
{
	m_clrBackground	= RGB(255, 255, 255);	//背景颜色
	m_clrFrame		= RGB(228, 228, 228);	//边框颜色
	m_clrText		= RGB(174, 174, 174);	//文字颜色

	m_brushBkg.CreateSolidBrush(m_clrBackground); //创建背景画刷

	m_pfDefault = ::GetFont(_T("微软雅黑"), 12);
}

CMyIPCtrl::~CMyIPCtrl()
{
}

BEGIN_MESSAGE_MAP(CMyIPCtrl, CIPAddressCtrl)
	ON_WM_NCPAINT()
	//ON_WM_CTLCOLOR_REFLECT()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CWEEditCtrl 消息处理程序
HBRUSH CMyIPCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT /*nCtlColor*/)
{
	m_brushBkg.DeleteObject();
	m_brushBkg.CreateSolidBrush(m_clrBackground);
	pDC->SetTextColor(m_clrText);		//设置文字颜色
	pDC->SetBkColor(m_clrBackground);	//设置背景色

	return (HBRUSH)m_brushBkg.GetSafeHandle();
}

void CMyIPCtrl::OnNcPaint()
{
	CRect	rectEdit;
	CDC*	pDC = GetWindowDC();

	//设置编辑框的区域
	GetWindowRect(rectEdit);
	ScreenToClient(rectEdit);
	rectEdit.OffsetRect(CSize(2, 2));

	CPen mypen, *oldpen;
	//判断是否获得焦点
	mypen.CreatePen(PS_SOLID, 1, m_clrFrame);
	oldpen = pDC->SelectObject(&mypen);

	CPoint point(7, 7);
	pDC->RoundRect(rectEdit, point);

	pDC->SelectObject(oldpen);

	//设置字体
	SetFont((m_pSetFont != NULL ? m_pSetFont : m_pfDefault));

	ReleaseDC(pDC);
}
和CEdit的自绘很类似,唯一的区别在于这里使用OnCtlColor,CEdit使用CtlColor。

IP地址控件 CIPAddressCtrl 自绘

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部