在派生类中重载WM_CHAR的处理函数:
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call
int oldpos=LOWORD(GetSel());
CString str;
GetWindowText(str);
if ( nChar>='0' && nChar<='9' )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
str.Delete(oldpos,1);
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
if ( LOWORD(GetSel())==4 || LOWORD(GetSel())==7)
{
oldpos=LOWORD(GetSel());
SetSel(FormatPos(oldpos+1,oldpos+1));
}
}
else
if ( oldpos==4 || oldpos==7 )
{
oldpos+=1;
SetSel(FormatPos(oldpos,oldpos));
str.Delete(oldpos,1);
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
else
if ( nChar==VK_BACK )
{
if ( (oldpos>0 && oldpos<5) || ( oldpos>5 && oldpos<8) || oldpos>8)
{
str.Insert(oldpos,' ');
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
if ( oldpos==5 || oldpos==8 )
{
SetSel(FormatPos(oldpos-1,oldpos-1));
}
}
}重载键盘消息的处理函数:
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call
CString str;
int oldpos=LOWORD(GetSel());
GetWindowText(str);
if ( nChar==VK_DELETE )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
GetWindowText(str);
if ( oldpos<7 )
str.Insert(str.Find('-',oldpos),' ');
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
}
else
if ( oldpos==4 || oldpos==7 )
return ;
}
else
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
这段代码是把一个CEDIT类扩展成一个具有时间日期格式的编辑框类。
我的问题是
if ( nChar>='0' && nChar<='9' )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
str.Delete(oldpos,1);
........................................若干代码
中的str.Delete(oldpos,1);起到什么作用。为什么把这句代码注视起来之后,在编辑框中就不能输入字符了。
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call
int oldpos=LOWORD(GetSel());
CString str;
GetWindowText(str);
if ( nChar>='0' && nChar<='9' )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
str.Delete(oldpos,1);
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
if ( LOWORD(GetSel())==4 || LOWORD(GetSel())==7)
{
oldpos=LOWORD(GetSel());
SetSel(FormatPos(oldpos+1,oldpos+1));
}
}
else
if ( oldpos==4 || oldpos==7 )
{
oldpos+=1;
SetSel(FormatPos(oldpos,oldpos));
str.Delete(oldpos,1);
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
else
if ( nChar==VK_BACK )
{
if ( (oldpos>0 && oldpos<5) || ( oldpos>5 && oldpos<8) || oldpos>8)
{
str.Insert(oldpos,' ');
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
if ( oldpos==5 || oldpos==8 )
{
SetSel(FormatPos(oldpos-1,oldpos-1));
}
}
}重载键盘消息的处理函数:
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call
CString str;
int oldpos=LOWORD(GetSel());
GetWindowText(str);
if ( nChar==VK_DELETE )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
GetWindowText(str);
if ( oldpos<7 )
str.Insert(str.Find('-',oldpos),' ');
SetWindowText(str);
SetSel(FormatPos(oldpos,oldpos));
}
else
if ( oldpos==4 || oldpos==7 )
return ;
}
else
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
这段代码是把一个CEDIT类扩展成一个具有时间日期格式的编辑框类。
我的问题是
if ( nChar>='0' && nChar<='9' )
{
if ( oldpos<4 || ( oldpos>4 && oldpos<7) || oldpos>7)
{
str.Delete(oldpos,1);
........................................若干代码
中的str.Delete(oldpos,1);起到什么作用。为什么把这句代码注视起来之后,在编辑框中就不能输入字符了。
解决方案 »
- 利用mfc Http 的 get方法传值时的中文的乱码问题
- 矢量图形编辑器的开发思路是什么。
- 求算法:已知当前时间 给出一个时间间隔(小时为单位) 如何得到结束的时间天数??
- 我是新手.ICMP校验和的校验和函数看不懂,谁能详细跟我解释一下,通俗易懂一点.
- 一段自绘按钮的代码,为什么会有内存泄露?
- 救人一命把~
- 急问:clistctrl 怎么在某一行的某一列设置一个下拉列表,并且设置下拉列表中的内容?
- 地理信息系统,到底前景如何?
- 16位的dos程序如何做到和32位的程序之间作内存共享或其他通讯方式,在window环境下
- 想在软件中增加地图的应用,请帮忙推荐下学习资料
- 我该怎么办!
- MFC 控件变量的声明
Deletes a character or characters from a string starting with the character at the given index.在你的代码中的功能是将选择的字符删除
比如说只有1,3,5,7,8,10,12月有31号,2月有可能只有29天。
再比如说满30天就进一个月,满12个月就进一年。
有什么好的实现方法呢?
关于为什么用编辑框控件,我也是被逼的。。
是不是应该在ONCHAR()中实现,或者是有别的更简单的方法?
哪位哥们能够给段代码或者发给类似控制的链接做个参考,先在这里谢谢哥几个的关注了啊!