自定义编辑框VC,可加载更改字体,添加背景图片,显示输入提示信息

搞了一天终于弄了个完整的编辑框控件出来了,

哎,,,搞界面开发还是有点复杂的。

 #pragma once

 #include "AdvEdit.h"
// CBkgEditBox class CBkgEditBox : public CEdit
{
DECLARE_DYNAMIC(CBkgEditBox) public:
CBkgEditBox();
virtual ~CBkgEditBox(); protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKillFocus(CWnd* pNewWnd); private:
BOOL m_bFirstDraw; // 第一次绘制背景标识
Gdiplus::Image *m_pPngBg; // 背景位图资源
BOOL m_bHover; // 鼠标是否在编辑框上标识
BOOL m_bAllowMouseTrack; // 鼠标移动时是否允许追踪鼠标移动
COLORREF m_clrBkgnd; // 默认背景Hover颜色
COLORREF m_clrHoverBkgnd; // 默认背景UnHover颜色
COLORREF m_clrText; // 字体颜色
CString m_strTipStr; // 输入提示信息
CBrush m_brhNull; // 空画刷对象
CFont* m_pTextFont; // 编辑框字体 public:
BOOL SetBackgndRes( UINT nResourceID, LPCTSTR lpszType=_T("PNG"));
void SetGrayTipString(LPCTSTR lpszTipString);
void SetTextColor(COLORREF clrText);
BOOL SetTextFont(int nHeight = , int nWidth = , LPCTSTR lpszFaceName = _T("宋体"));
BOOL IsEditEmpty();
};
 // MyEditBox.cpp : 实现文件
// #include "stdafx.h"
#include "BkgEditBox.h" // CBkgEditBox IMPLEMENT_DYNAMIC(CBkgEditBox, CEdit) CBkgEditBox::CBkgEditBox()
{
m_bFirstDraw = TRUE;
m_pPngBg = NULL;
m_bHover = FALSE;
m_bAllowMouseTrack = TRUE; m_clrBkgnd = RGB(,,);
m_clrHoverBkgnd = RGB(,,);
m_clrText = RGB(,,);
m_strTipStr = _T(""); m_brhNull.CreateStockObject(NULL_BRUSH);
m_pTextFont = NULL;
} CBkgEditBox::~CBkgEditBox()
{
if (NULL != m_pTextFont)
{
delete m_pTextFont;
m_pTextFont = NULL;
}
} BEGIN_MESSAGE_MAP(CBkgEditBox, CEdit)
ON_WM_ERASEBKGND()
ON_WM_SETFOCUS()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
ON_WM_MOUSEMOVE()
ON_WM_CTLCOLOR()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_CHAR()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP() // CBkgEditBox 消息处理程序 BOOL CBkgEditBox::OnEraseBkgnd(CDC* pDC)
{
// 图片背景与编辑框的距离顶点
int nLeftStep,nTopStep,nWidthStep,nHeightStep;
nLeftStep = ;
nTopStep = ;
nWidthStep = ;
nHeightStep = ;
CDC* pOldDC = pDC;
if (NULL != m_pPngBg)
{
pDC = GetParent()->GetDC();
CRect rect;
GetWindowRect(&rect);
this->GetParent()->ScreenToClient(&rect); int nWidth = m_pPngBg->GetWidth();
int nHeight = m_pPngBg->GetHeight(); // 不是第一次绘制时需要调整位置
if (!m_bFirstDraw)
{
rect.left -= nLeftStep;
rect.top -= nTopStep;
}else
{
MoveWindow(rect.left + nLeftStep, rect.top + nTopStep,
nWidth - nWidthStep, nHeight - nHeightStep);
} CDC MemDc;
MemDc.CreateCompatibleDC(pDC); CBitmap bitMemMap;
bitMemMap.CreateCompatibleBitmap(pDC, nWidth, nHeight);
MemDc.SelectObject(&bitMemMap); Gdiplus::Graphics graphics(MemDc.m_hDC);
graphics.DrawImage(m_pPngBg, , , nWidth, nHeight);
pDC->BitBlt(rect.left, rect.top, nWidth,nHeight, &MemDc, , ,SRCCOPY); bitMemMap.DeleteObject();
MemDc.DeleteDC();
m_bFirstDraw = FALSE; pDC = pOldDC;
} { // 内阴影
CBrush brhInnerShadow;
if (!m_bHover)
{
brhInnerShadow.CreateSolidBrush(m_clrBkgnd);
}else
{
brhInnerShadow.CreateSolidBrush(m_clrHoverBkgnd);
} CBrush* pOldBrh = pDC->SelectObject(&brhInnerShadow);
pDC->SelectStockObject(NULL_PEN);
CRect rcShadow;
GetClientRect(&rcShadow);
if (NULL != m_pPngBg)
{
rcShadow.right = rcShadow.left + m_pPngBg->GetWidth() - nWidthStep;
rcShadow.bottom = rcShadow.top + m_pPngBg->GetHeight() - nHeightStep;
}
pDC->Rectangle(&rcShadow);
if(NULL!=pOldBrh)
{
pDC->SelectObject(pOldBrh);
}
brhInnerShadow.DeleteObject();
this->UpdateWindow(); } return CEdit::OnEraseBkgnd(pDC);
} BOOL CBkgEditBox::SetBackgndRes( UINT nResourceID, LPCTSTR lpszType/*=_T("PNG")*/ )
{
return G_ImageFromIDResource(nResourceID, lpszType, m_pPngBg);
} void CBkgEditBox::OnMouseHover(UINT nFlags, CPoint point)
{
m_bHover = TRUE;
Invalidate(); CEdit::OnMouseHover(nFlags, point);
} void CBkgEditBox::OnMouseLeave()
{
// Hover结束
m_bHover = FALSE;
// 重绘背景
Invalidate(); // 鼠标离开,继续追踪HOVER消息
m_bAllowMouseTrack = TRUE; //CWnd* pWnd =this->GetParent(); //CRect rcTmp;
//GetClientRect(rcTmp);
//rcTmp.OffsetRect(3, 3);
////InvalidateRect(rcTmp);
//this->ClientToScreen(&rcTmp);
//pWnd->ScreenToClient(&rcTmp);
//pWnd->InvalidateRect(&rcTmp);
//pWnd->UpdateWindow(); CEdit::OnMouseLeave();
} void CBkgEditBox::OnMouseMove(UINT nFlags, CPoint point)
{
// 默认情况下,窗口是不响应 WM_MOUSELEAVE 和 WM_MOUSEHOVER 消息的,
// 所以要使用 _TrackMouseEvent 函数来激活这两个消息
// https://msdn.microsoft.com/en-us/library/ms645604 if (m_bAllowMouseTrack)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER|TME_LEAVE;
tme.hwndTrack = m_hWnd;
tme.dwHoverTime = ; // 超过10ms才会认为HOVER _TrackMouseEvent(&tme);
m_bAllowMouseTrack = FALSE;
} CEdit::OnMouseMove(nFlags, point);
} BOOL CBkgEditBox::IsEditEmpty()
{
CString strText;
GetWindowText(strText);
if (strText.IsEmpty())
{
return TRUE;
}
if (strText == m_strTipStr)
{
return TRUE;
}
return FALSE;
} HBRUSH CBkgEditBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CEdit::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: 在此更改 DC 的任何特性 // TODO: 如果默认的不是所需画笔,则返回另一个画笔 return hbr;
} HBRUSH CBkgEditBox::CtlColor( CDC* pDC, UINT nCtlColor )
{
// 设置透明背景
pDC->SetBkMode(TRANSPARENT); CString sText;
GetWindowText(sText);
if (sText == m_strTipStr)
{
pDC->SetTextColor(RGB(,,));
}else
{
pDC->SetTextColor(m_clrText);
} return m_brhNull;
} void CBkgEditBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
Invalidate(TRUE); CEdit::OnChar(nChar, nRepCnt, nFlags);
} void CBkgEditBox::OnSetFocus(CWnd* pOldWnd)
{
CEdit::OnSetFocus(pOldWnd); CString sText;
GetWindowText(sText);
if (sText == m_strTipStr)
{
SetWindowText(_T(""));
}
Invalidate(TRUE);
return ; }
void CBkgEditBox::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd); CString sText;
GetWindowText(sText);
if (_T("") == sText)
{
SetWindowText(m_strTipStr);
}
} void CBkgEditBox::SetGrayTipString( LPCTSTR lpszTipString )
{
m_strTipStr = lpszTipString; SetWindowText(lpszTipString);
} void CBkgEditBox::SetTextColor( COLORREF clrText )
{
m_clrText = m_clrText;
} // https://msdn.microsoft.com/zh-tw/library/vstudio/2ek64h34.aspx
BOOL CBkgEditBox::SetTextFont( int nHeight /*= 20*/, int nWidth /*= 12*/, LPCTSTR lpszFaceName /*= _T("宋体")*/ )
{
if (NULL != m_pTextFont)
{
delete m_pTextFont;
m_pTextFont = NULL;
}
m_pTextFont = new CFont;
m_pTextFont->CreateFont(
nHeight, // nHeight
nWidth, // nWidth
, // nEscapement
, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
lpszFaceName); this->SetFont(m_pTextFont);
return TRUE;
}
上一篇:Oracle笔试题库 附参考答案


下一篇:自定义UINavigationBar的背景【转】