// TODO: Return a non-NULL brush if the parent's handler should not be called
//set text color pDC->SetTextColor(m_crText); //set the text's background color pDC->SetBkColor(m_crBackGnd); //return the brush used for background this sets control background return m_brBackGnd; } void CHotEdit::SetBackColor(COLORREF rgb) { //set background color ref (used for text's background) m_crBackGnd = rgb;
//free brush if (m_brBackGnd.GetSafeHandle()) m_brBackGnd.DeleteObject(); //set brush to new color m_brBackGnd.CreateSolidBrush(rgb);
//redraw Invalidate(TRUE); } void CHotEdit::SetTextColor(COLORREF rgb) { //set text color ref m_crText = rgb; //redraw Invalidate(TRUE); } BEGIN_MESSAGE_MAP(CHotEdit, CEdit) //{{AFX_MSG_MAP(CHotEdit) ON_WM_PAINT() ON_WM_SETFOCUS() ON_WM_KILLFOCUS() ON_WM_MOUSEMOVE() ON_WM_TIMER() ON_WM_NCMOUSEMOVE() ON_WM_SYSCOLORCHANGE() ON_WM_CTLCOLOR_REFLECT() //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CHotEdit message handlersvoid CHotEdit::OnPaint() { // call the default message handler to repaint the text etc. Default(); // now redraw the border if (m_fGotFocus) { DrawBorder(); } else { DrawBorder(false); } }void CHotEdit::OnSetFocus(CWnd* pOldWnd) { CEdit::OnSetFocus(pOldWnd);
// set internal flag and redraw border m_fGotFocus = true; DrawBorder(); }void CHotEdit::OnKillFocus(CWnd* pNewWnd) { CEdit::OnKillFocus(pNewWnd); // set internal flag and redraw border m_fGotFocus = false; DrawBorder(false); }void CHotEdit::OnMouseMove(UINT nFlags, CPoint point) { // no point setting the timer if we already have focus, the border will be drawn all the time if // we have focus if (!m_fGotFocus) { // if the timer isn't already set, set it. if (!m_fTimerSet) { DrawBorder(); SetTimer(1, 10, NULL); m_fTimerSet = true; } } CEdit::OnMouseMove(nFlags, point); }// The timer is used to periodically check whether the mouse cursor is within // this control. Once it isn't, the timer is killed void CHotEdit::OnTimer(UINT nIDEvent) { POINT pt; GetCursorPos(&pt); CRect rcItem; GetWindowRect(&rcItem); // if the mouse cursor within the control? if(!rcItem.PtInRect(pt)) { KillTimer(1); m_fTimerSet = false; if (!m_fGotFocus) { DrawBorder(false); } return; } CEdit::OnTimer(nIDEvent); }// Scroll bars are actually in the non-client area of the window // so we want to check for movement here too void CHotEdit::OnNcMouseMove(UINT nHitTest, CPoint point) { if (!m_fTimerSet) { DrawBorder(); SetTimer(1, 10, NULL); m_fTimerSet = true; }
CEdit::OnNcMouseMove(nHitTest, point); }// When the user has changed the system colours we want to // update the member variables accordingly void CHotEdit::OnSysColorChange() { CEdit::OnSysColorChange();
m_clr3DFace = GetSysColor(COLOR_3DFACE); m_clr3DLight = GetSysColor(COLOR_3DLIGHT); m_clr3DHilight = GetSysColor(COLOR_3DHILIGHT); m_clr3DShadow = GetSysColor(COLOR_3DSHADOW); m_clr3DDkShadow = GetSysColor(COLOR_3DDKSHADOW); }// This is the guts of this control. It draws the border of the control // as flat when the control is not active. When the control becomes active // the border is drawn according to the styles applied to it // If the control is disabled, the border is drawn irrespective of whether // the control is hot or not. void CHotEdit::DrawBorder(bool fHot) { CRect rcItem; DWORD dwExStyle = GetExStyle(); CDC* pDC = GetDC(); COLORREF clrBlack; int nBorderWidth = 0; int nLoop; GetWindowRect(&rcItem); ScreenToClient(&rcItem); clrBlack = RGB(0, 0, 0); if (!IsWindowEnabled()) { fHot = true; } if (dwExStyle & WS_EX_DLGMODALFRAME) { nBorderWidth += 3; } if (dwExStyle & WS_EX_CLIENTEDGE) { nBorderWidth += 1; } if (dwExStyle & WS_EX_STATICEDGE && !(dwExStyle & WS_EX_DLGMODALFRAME)) { nBorderWidth ++; } POINT pt ; pt.x = 7; pt.y = 7; // blank the border
#define AFX_HOTEDIT_H__BE9A5170_B912_11D2_B882_0020182B6AB8__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// HotEdit.h : header file
///////////////////////////////////////////////////////////////////////////////
// CHotEdit windowclass CHotEdit : public CEdit
{
// Construction
public:
CHotEdit();// Attributes
public:
// Operations
public:
COLORREF m_clrBK;
COLORREF m_clrFG;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHotEdit)
//}}AFX_VIRTUAL// Implementation
public:
void SetTextColor(COLORREF rgb);
void SetBackColor(COLORREF rgb);
void SetBorderColor(COLORREF rgb1,COLORREF rgb2);
virtual ~CHotEdit(); // Generated message map functions
protected:
virtual void DrawBorder(bool fHot = true);
COLORREF m_clr3DHilight;
COLORREF m_clr3DLight;
COLORREF m_clr3DDkShadow;
COLORREF m_clr3DShadow;
COLORREF m_clr3DFace; COLORREF m_crText;
COLORREF m_crBackGnd;
//background brush
CBrush m_brBackGnd;
bool m_fTimerSet;
bool m_fGotFocus;
//{{AFX_MSG(CHotEdit)
afx_msg void OnPaint();
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
afx_msg void OnSysColorChange();
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HOTEDIT_H__BE9A5170_B912_11D2_B882_0020182B6AB8__INCLUDED_)
//
// If you need any help with this edit control, email me: [email protected]#include "stdafx.h"
#include "HotEdit.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CHotEditCHotEdit::CHotEdit()
{
m_clr3DFace = GetSysColor(COLOR_3DFACE);
m_clr3DLight = GetSysColor(COLOR_3DLIGHT);
// m_clr3DHilight = GetSysColor(COLOR_3DHILIGHT);
m_clr3DHilight = GetSysColor(RGB(49,106,197));
m_clr3DShadow = GetSysColor(COLOR_3DSHADOW);
// m_clr3DDkShadow = GetSysColor(COLOR_3DDKSHADOW);
m_clr3DDkShadow = GetSysColor(RGB(255,0,0)); m_fGotFocus = false;
m_fTimerSet = false;
m_crText = RGB(255,0,0);
// m_crBackGnd = RGB(255,255,255);
// SetWindowText("ffffgggggggg");
// GetDlgItem(
}CHotEdit::~CHotEdit()
{
if (m_brBackGnd.GetSafeHandle())
m_brBackGnd.DeleteObject();
}
HBRUSH CHotEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Return a non-NULL brush if the parent's handler should not be called
//set text color
pDC->SetTextColor(m_crText);
//set the text's background color
pDC->SetBkColor(m_crBackGnd); //return the brush used for background this sets control background
return m_brBackGnd;
}
void CHotEdit::SetBackColor(COLORREF rgb)
{
//set background color ref (used for text's background)
m_crBackGnd = rgb;
//free brush
if (m_brBackGnd.GetSafeHandle())
m_brBackGnd.DeleteObject();
//set brush to new color
m_brBackGnd.CreateSolidBrush(rgb);
//redraw
Invalidate(TRUE);
}
void CHotEdit::SetTextColor(COLORREF rgb)
{
//set text color ref
m_crText = rgb; //redraw
Invalidate(TRUE);
}
BEGIN_MESSAGE_MAP(CHotEdit, CEdit)
//{{AFX_MSG_MAP(CHotEdit)
ON_WM_PAINT()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_NCMOUSEMOVE()
ON_WM_SYSCOLORCHANGE()
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CHotEdit message handlersvoid CHotEdit::OnPaint()
{
// call the default message handler to repaint the text etc.
Default(); // now redraw the border
if (m_fGotFocus)
{
DrawBorder();
}
else
{
DrawBorder(false);
}
}void CHotEdit::OnSetFocus(CWnd* pOldWnd)
{
CEdit::OnSetFocus(pOldWnd);
// set internal flag and redraw border
m_fGotFocus = true;
DrawBorder();
}void CHotEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd); // set internal flag and redraw border
m_fGotFocus = false;
DrawBorder(false);
}void CHotEdit::OnMouseMove(UINT nFlags, CPoint point)
{
// no point setting the timer if we already have focus, the border will be drawn all the time if
// we have focus
if (!m_fGotFocus)
{
// if the timer isn't already set, set it.
if (!m_fTimerSet)
{
DrawBorder();
SetTimer(1, 10, NULL);
m_fTimerSet = true;
}
} CEdit::OnMouseMove(nFlags, point);
}// The timer is used to periodically check whether the mouse cursor is within
// this control. Once it isn't, the timer is killed
void CHotEdit::OnTimer(UINT nIDEvent)
{
POINT pt;
GetCursorPos(&pt);
CRect rcItem;
GetWindowRect(&rcItem); // if the mouse cursor within the control?
if(!rcItem.PtInRect(pt))
{
KillTimer(1); m_fTimerSet = false; if (!m_fGotFocus)
{
DrawBorder(false);
}
return;
} CEdit::OnTimer(nIDEvent);
}// Scroll bars are actually in the non-client area of the window
// so we want to check for movement here too
void CHotEdit::OnNcMouseMove(UINT nHitTest, CPoint point)
{
if (!m_fTimerSet)
{
DrawBorder();
SetTimer(1, 10, NULL);
m_fTimerSet = true;
}
CEdit::OnNcMouseMove(nHitTest, point);
}// When the user has changed the system colours we want to
// update the member variables accordingly
void CHotEdit::OnSysColorChange()
{
CEdit::OnSysColorChange();
m_clr3DFace = GetSysColor(COLOR_3DFACE);
m_clr3DLight = GetSysColor(COLOR_3DLIGHT);
m_clr3DHilight = GetSysColor(COLOR_3DHILIGHT);
m_clr3DShadow = GetSysColor(COLOR_3DSHADOW);
m_clr3DDkShadow = GetSysColor(COLOR_3DDKSHADOW);
}// This is the guts of this control. It draws the border of the control
// as flat when the control is not active. When the control becomes active
// the border is drawn according to the styles applied to it
// If the control is disabled, the border is drawn irrespective of whether
// the control is hot or not.
void CHotEdit::DrawBorder(bool fHot)
{
CRect rcItem;
DWORD dwExStyle = GetExStyle();
CDC* pDC = GetDC();
COLORREF clrBlack;
int nBorderWidth = 0;
int nLoop; GetWindowRect(&rcItem);
ScreenToClient(&rcItem); clrBlack = RGB(0, 0, 0); if (!IsWindowEnabled())
{
fHot = true;
} if (dwExStyle & WS_EX_DLGMODALFRAME)
{ nBorderWidth += 3;
}
if (dwExStyle & WS_EX_CLIENTEDGE)
{
nBorderWidth += 1;
}
if (dwExStyle & WS_EX_STATICEDGE && !(dwExStyle & WS_EX_DLGMODALFRAME))
{
nBorderWidth ++;
}
POINT pt ;
pt.x = 7;
pt.y = 7;
// blank the border
CRect rcItem1(rcItem.left-1,rcItem.top-1,rcItem.right+1,rcItem.bottom+1);
for (nLoop = 0; nLoop < 1; nLoop++)
{
pDC->Draw3dRect(rcItem1, m_clrBK, m_clrBK);
rcItem1.DeflateRect(1, 1);
}
rcItem1.InflateRect(1, 1); if (fHot)
{
{
CBrush br(m_clrFG);
CRect rcItem1(rcItem.left-1,rcItem.top-1,rcItem.right+1,rcItem.bottom+1);
pDC->FrameRect(rcItem1,&br);
rcItem1.InflateRect(1, 1);
}
}
/* CRgn rgn;
CRect rc;
GetWindowRect(&rc);
ScreenToClient(&rc);
rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,5,5);
SetWindowRgn(rgn,TRUE);*/
ReleaseDC(pDC);
}
void CHotEdit::SetBorderColor(COLORREF rgb1,COLORREF rgb2)
{
m_clrFG=rgb2;
m_clrBK=rgb1;
}
////////////////////我想在这个类中实现去编辑框是圆角的功能