想繼承CEdit類﹐自己寫個類似子網掩碼的輸入框類 主要要重寫哪些函數﹐給點意見 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先当然是OnChar了。还有OnKeyDown #pragma onceclass NewMaskEdit : public CEdit{public: NewMaskEdit(); virtual ~NewMaskEdit(); void SetWindowText(LPCTSTR lpszString); void setText(LPCTSTR pszText); CString getText(); void setMask(LPCTSTR pszMask, char cPlaceHolder = '_'); CString getMask(); //{{AFX_VIRTUAL(NewMaskEdit) //}}AFX_VIRTUALprotected: char m_cPlaceHolder; CString m_strMaskText; CString m_strSaveText; bool m_bLastKeyWasDelete; bool m_bLastKeyWasBackspace; bool isMaskChar(int nPos); int gotoNextEntryChar(int nPos); bool getReplacementChar(int nPos, char cWant, char& cGet); //{{AFX_MSG(NewMaskEdit) afx_msg void OnUpdate(); afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); //}}AFX_MSG DECLARE_MESSAGE_MAP()};inline CString NewMaskEdit::getMask(){ return m_strMaskText;} // NewMaskEdit.cpp : implementation file//#include "stdafx.h"#include "NewMaskEdit.h"#include <ctype.h>#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifNewMaskEdit::NewMaskEdit(){ m_bLastKeyWasDelete = false; m_bLastKeyWasBackspace = false; m_cPlaceHolder = '_';}NewMaskEdit::~NewMaskEdit(){}void NewMaskEdit::setMask(LPCTSTR pszMask, char cPlaceHolder){ m_strMaskText = pszMask; m_strSaveText.Empty(); int nIndex = 0; while (nIndex < m_strMaskText.GetLength()) { if (isMaskChar(nIndex)) m_strSaveText += m_strMaskText[nIndex]; else m_strSaveText += m_cPlaceHolder; nIndex++; } CWnd::SetWindowText(m_strSaveText);}void NewMaskEdit::SetWindowText(LPCTSTR pszText){ setText(pszText);}void NewMaskEdit::setText(LPCTSTR pszText){ CString strText(pszText); m_strSaveText.Empty(); int nMaskIndex = 0; int nTextIndex = 0; while (nMaskIndex < m_strMaskText.GetLength()) { if (!isMaskChar(nMaskIndex)) { if (nTextIndex < strText.GetLength()) m_strSaveText += strText[nTextIndex++]; else m_strSaveText += m_cPlaceHolder; } else m_strSaveText += m_strMaskText[nMaskIndex]; nMaskIndex++; } CWnd::SetWindowText(m_strSaveText);}CString NewMaskEdit::getText(){ CString strText; int nMaskIndex = 0; while (nMaskIndex < m_strMaskText.GetLength()) { if (!isMaskChar(nMaskIndex)) strText += m_strSaveText[nMaskIndex]; nMaskIndex++; } return strText;}BEGIN_MESSAGE_MAP(NewMaskEdit, CEdit) //{{AFX_MSG_MAP(NewMaskEdit) ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate) ON_WM_KEYDOWN() //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// NewMaskEdit message handlersbool NewMaskEdit::isMaskChar(int nPos){ ASSERT(nPos < m_strMaskText.GetLength()); return (m_strMaskText[nPos] != '#' && m_strMaskText[nPos] != '?'); // Add new masks as needed}bool NewMaskEdit::getReplacementChar(int nPos, char cWant, char& cGet){ if (nPos >= m_strMaskText.GetLength()) return false; switch (m_strMaskText[nPos]) { case '#': if (0 == isdigit(cWant)) return false; cGet = cWant; break; case '?': if (0 == isalpha(cWant)) return false; cGet = cWant; break; // Add new masks as needed default: cGet = m_strMaskText[nPos]; break; } return true;}int NewMaskEdit::gotoNextEntryChar(int nPos){ if (m_bLastKeyWasBackspace) { nPos--; while (nPos >= 0 && isMaskChar(nPos)) nPos--; nPos++; } else { while (nPos < m_strMaskText.GetLength() && isMaskChar(nPos)) nPos++; } return nPos;}void NewMaskEdit::OnUpdate() { int nStart, nEnd; GetSel(nStart, nEnd); CString strEditText; GetWindowText(strEditText); if (strEditText == m_strSaveText) return; if (m_bLastKeyWasBackspace) { m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder); CWnd::SetWindowText(m_strSaveText); int nNext = gotoNextEntryChar(nStart); SetSel(nNext, nNext); } else if (m_bLastKeyWasDelete) { m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder); CWnd::SetWindowText(m_strSaveText); int nNext = gotoNextEntryChar(nStart + 1); SetSel(nNext, nNext); } else { if (nStart - 1 < 0) return; char cWanted = strEditText[nStart - 1]; char cReplace = 0; if (getReplacementChar(nStart - 1, cWanted, cReplace)) { if (m_strSaveText.GetLength() < nStart) m_strSaveText.GetBufferSetLength(nStart); m_strSaveText.SetAt(nStart - 1, cReplace); m_strSaveText.ReleaseBuffer(); } CWnd::SetWindowText(m_strSaveText); int nNext = gotoNextEntryChar(nStart); SetSel(nNext, nNext); }}void NewMaskEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { m_bLastKeyWasDelete = (VK_DELETE == nChar); m_bLastKeyWasBackspace = (VK_BACK == nChar); CEdit::OnKeyDown(nChar, nRepCnt, nFlags);} 最好是自绘,在wm_paint事件中画内容 CMDIFrameWnd改成CMDIFrameWndEx,跳出了个冲突 请教高手,一个关于VC程序运行无法响应的问题 做一个图片处理的程序,“打开”不好用 寻求一个用户身份验证的完整解决方案 请求VC高手看看我的程序,留下您的联系方式,谢谢了 variant变量的使用问题 主窗口之外的模态对话框如何显示图标在任务栏? 请高手提供思路或相关例子,如何实现这样的按钮功能? 大家讨论什么样的软件人才比较吃香 哪位高手能给出从摄像头(视频聊天用的)下载图像的例子(高分) 请问这段代码是什么意思? 在IE中,ActiveX控件可以打开本地文件?控件是签名过的。
{
public:
NewMaskEdit();
virtual ~NewMaskEdit(); void SetWindowText(LPCTSTR lpszString); void setText(LPCTSTR pszText);
CString getText(); void setMask(LPCTSTR pszMask, char cPlaceHolder = '_');
CString getMask(); //{{AFX_VIRTUAL(NewMaskEdit)
//}}AFX_VIRTUALprotected: char m_cPlaceHolder;
CString m_strMaskText; CString m_strSaveText; bool m_bLastKeyWasDelete;
bool m_bLastKeyWasBackspace; bool isMaskChar(int nPos); int gotoNextEntryChar(int nPos); bool getReplacementChar(int nPos, char cWant, char& cGet); //{{AFX_MSG(NewMaskEdit)
afx_msg void OnUpdate();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};inline CString NewMaskEdit::getMask()
{
return m_strMaskText;
}
//#include "stdafx.h"
#include "NewMaskEdit.h"#include <ctype.h>#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifNewMaskEdit::NewMaskEdit()
{
m_bLastKeyWasDelete = false;
m_bLastKeyWasBackspace = false; m_cPlaceHolder = '_';
}NewMaskEdit::~NewMaskEdit()
{
}void NewMaskEdit::setMask(LPCTSTR pszMask, char cPlaceHolder)
{
m_strMaskText = pszMask; m_strSaveText.Empty(); int nIndex = 0;
while (nIndex < m_strMaskText.GetLength())
{
if (isMaskChar(nIndex))
m_strSaveText += m_strMaskText[nIndex];
else
m_strSaveText += m_cPlaceHolder; nIndex++;
} CWnd::SetWindowText(m_strSaveText);
}void NewMaskEdit::SetWindowText(LPCTSTR pszText)
{
setText(pszText);
}void NewMaskEdit::setText(LPCTSTR pszText)
{
CString strText(pszText); m_strSaveText.Empty(); int nMaskIndex = 0;
int nTextIndex = 0; while (nMaskIndex < m_strMaskText.GetLength())
{
if (!isMaskChar(nMaskIndex))
{
if (nTextIndex < strText.GetLength())
m_strSaveText += strText[nTextIndex++];
else
m_strSaveText += m_cPlaceHolder;
}
else
m_strSaveText += m_strMaskText[nMaskIndex]; nMaskIndex++;
} CWnd::SetWindowText(m_strSaveText);
}CString NewMaskEdit::getText()
{
CString strText; int nMaskIndex = 0;
while (nMaskIndex < m_strMaskText.GetLength())
{
if (!isMaskChar(nMaskIndex))
strText += m_strSaveText[nMaskIndex]; nMaskIndex++;
}
return strText;
}
BEGIN_MESSAGE_MAP(NewMaskEdit, CEdit)
//{{AFX_MSG_MAP(NewMaskEdit)
ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// NewMaskEdit message handlersbool NewMaskEdit::isMaskChar(int nPos)
{
ASSERT(nPos < m_strMaskText.GetLength()); return (m_strMaskText[nPos] != '#' &&
m_strMaskText[nPos] != '?'); // Add new masks as needed
}bool NewMaskEdit::getReplacementChar(int nPos, char cWant, char& cGet)
{
if (nPos >= m_strMaskText.GetLength())
return false; switch (m_strMaskText[nPos])
{
case '#':
if (0 == isdigit(cWant))
return false; cGet = cWant;
break; case '?':
if (0 == isalpha(cWant))
return false; cGet = cWant;
break; // Add new masks as needed default:
cGet = m_strMaskText[nPos];
break;
}
return true;
}int NewMaskEdit::gotoNextEntryChar(int nPos)
{
if (m_bLastKeyWasBackspace)
{
nPos--; while (nPos >= 0 && isMaskChar(nPos))
nPos--; nPos++;
}
else
{
while (nPos < m_strMaskText.GetLength() && isMaskChar(nPos))
nPos++;
}
return nPos;
}void NewMaskEdit::OnUpdate()
{
int nStart, nEnd;
GetSel(nStart, nEnd); CString strEditText;
GetWindowText(strEditText); if (strEditText == m_strSaveText)
return; if (m_bLastKeyWasBackspace)
{
m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder); CWnd::SetWindowText(m_strSaveText);
int nNext = gotoNextEntryChar(nStart);
SetSel(nNext, nNext);
}
else if (m_bLastKeyWasDelete)
{
m_strSaveText.SetAt(nStart, isMaskChar(nStart) ? m_strMaskText[nStart] : m_cPlaceHolder); CWnd::SetWindowText(m_strSaveText);
int nNext = gotoNextEntryChar(nStart + 1);
SetSel(nNext, nNext);
}
else
{
if (nStart - 1 < 0)
return; char cWanted = strEditText[nStart - 1]; char cReplace = 0; if (getReplacementChar(nStart - 1, cWanted, cReplace))
{
if (m_strSaveText.GetLength() < nStart)
m_strSaveText.GetBufferSetLength(nStart); m_strSaveText.SetAt(nStart - 1, cReplace);
m_strSaveText.ReleaseBuffer();
} CWnd::SetWindowText(m_strSaveText);
int nNext = gotoNextEntryChar(nStart);
SetSel(nNext, nNext);
}
}void NewMaskEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
m_bLastKeyWasDelete = (VK_DELETE == nChar);
m_bLastKeyWasBackspace = (VK_BACK == nChar);
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}