本文主要是介绍MFC自定义搜索编辑框CEdit,得到焦点提示文本消失,失去焦点,提示文本显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#pragma once// CSearchEditclass CSearchEdit : public CEdit
{DECLARE_DYNAMIC(CSearchEdit)public:CSearchEdit();virtual ~CSearchEdit();public:void ShowTip();const CString GetTip()const { return m_strTip; }private:const CString m_strTip = _T("搜索图片");protected:DECLARE_MESSAGE_MAP()afx_msg void OnEnKillfocus();afx_msg void OnEnSetfocus();afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
};
// SearchEdit.cpp : 实现文件
//#include "stdafx.h"
#include "TestDlg.h"
#include "SearchEdit.h"// CSearchEditIMPLEMENT_DYNAMIC(CSearchEdit, CEdit)CSearchEdit::CSearchEdit()
{}CSearchEdit::~CSearchEdit()
{
}void CSearchEdit::ShowTip()
{SetWindowText(m_strTip);
}BEGIN_MESSAGE_MAP(CSearchEdit, CEdit)ON_CONTROL_REFLECT(EN_KILLFOCUS, &CSearchEdit::OnEnKillfocus)ON_CONTROL_REFLECT(EN_SETFOCUS, &CSearchEdit::OnEnSetfocus)ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()// CSearchEdit 消息处理程序void CSearchEdit::OnEnKillfocus()
{CString str;GetWindowText(str);if (str == _T("")){SetWindowText(m_strTip);}
}void CSearchEdit::OnEnSetfocus()
{CString str;GetWindowText(str);if (str == m_strTip){SetWindowText(_T(""));}
}HBRUSH CSearchEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{// TODO: 在此更改 DC 的任何特性CString str;GetWindowText(str);if (str == m_strTip){pDC->SetTextColor(RGB(192, 192, 192));return (HBRUSH)GetStockObject(WHITE_BRUSH);}// TODO: 如果不应调用父级的处理程序,则返回非 null 画笔return NULL;
}
这篇关于MFC自定义搜索编辑框CEdit,得到焦点提示文本消失,失去焦点,提示文本显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!