?? masked_edit2.shtml.htm
字號:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="author" content="zafir anjum">
<title>edit controls - table of contents</title>
<meta name="description" content="source code for various windows controls">
<meta name="keywords" content="mfc source code edit controls">
</head>
<body background="../di2001.jpg"
tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#ffffff">
<h3 align="center"><font color="#aoao99">a masked edit control with time, date, telephone
number, ip address and post code examples</font></h3>
<hr align="center">
<!-- author and contact details -->
<p>this article was contributed by <a href="mailto:jmd@jvf.co.uk">jeremy davis</a>. </p>
<p><img src="masked_edit2.gif" tppabs="http://www.codeguru.com/editctrl/masked_edit2.gif"
width="311" height="300"> <a href="masked_edit2_demo.zip"
tppabs="http://www.codeguru.com/editctrl/masked_edit2_demo.zip">download demo project and
source</a> (37k) or <a href="masked_edit2_source.zip"
tppabs="http://www.codeguru.com/editctrl/masked_edit2_source.zip">download source only</a>
(4k)</p>
<p><strong>updated : </strong><a href="#updates">12th august 1998</a><!-- the article... --></p>
<p>the original source for this code came from <a href="mailto::sam.claret@dial.pipex.com">sam
claret's</a> article <a href="masked_edit.shtml.htm"
tppabs="http://www.codeguru.com/editctrl/masked_edit.shtml">a masked edit control</a>.</p>
<p><strong>ctimeedit</strong><br>
the class that i was originally interested in was his ctimeedit that allowed the editing
of hours and minutes in an edit control. his original code only allowed a maximum 23<sup>rd</sup>
hour, and 59<sup>th</sup> minute, i.e. the standard 24 hour clock. there was no way of
specifying a 12 hour clock or even a 48 hour clock.. for this reason i have added <tt><font
color="#990000">sethours(int hrs)</font></tt>, <tt><font color="#990000">setmins(int mins)</font></tt>to
ctimeedit to allow this, and <tt><font color="#990000">settime(cstring date)</font></tt>,
and <tt><font color="#990000">cstring gettimestr()</font></tt> to extend the use with
other time functions..</p>
<p>as with sam's code... </p>
<pre><font color="#990000"><tt> #include <afxdao.h>
</pre>
</tt></font>
<p>in a header file such as stdafx.h. then create a cedit box in the dialog editor, and
using classwizard declare a cedit control variable for it such as m_editctrl.next edit the
relevant header file to change the control variable from cedit to ctimeedit. </p>
<p>in a fuction such as oninitdialog either write... </p>
<pre><font color="#990000"><tt> m_editctrl.settime(coledatetime::getcurrenttime());
</pre>
</tt></font>
<p>or </p>
<pre><font color="#990000"><tt> m_editctrl.settime("11:03");
</pre>
</tt></font>
<p>to set the time and then to set the maximum hours and minutes write... </p>
<pre><font color="#990000"><tt> m_editctrl.sethours(12);
m_editctrl.setmins(59);
</pre>
</tt></font>
<p>to return the time in a string rather than a coledatetime object write... </p>
<pre><font color="#990000"><tt> cstring string = m_editctrl.gettimestr();</tt></font></pre>
<p>the default maximum hours are 24, and the default maximum minutes are 59. </p>
<p> </p>
<p><strong>cmaskedit<br>
</strong>joe ismert asked me how he could use this masked edit class to allow entry of,
for example a telephone number. well cdateedit and ctimeedit are both sub-classes of
cmaskedit. to create your own customised masked edit control you can either create your
own sub-class of cmaskedit for your own unique mask, or use cmaskedit directly. the
following telephone number, ip address, and post code examples all use cmaskdirectly,
however can be placed a their own unique sub-class for which either cdateedit and
ctimeedit can be used as examples.</p>
<blockquote>
<p><strong>telephone number<br>
</strong>create a cmaskedit control called for example m_editphonectrl. then in
oninitdialog write...<br>
</p>
<pre><font color="#990000"><tt>
//cmaskedit - telephone initialisation
m_editphonectrl.m_bistime = false;
//for use with ctimeedit
m_editphonectrl.m_isdate = false;
//for use with cdateedit
m_editphonectrl.m_busemask = true;
//set to use cmaskedit
m_editphonectrl.m_strmask = "0000 0000000"; //the mask string
m_editphonectrl.m_strliteral = "____ _______"; //"_" char = character entry
//" " char = no character entry
m_editphonectrl.m_str = 0116 2111111"; //initial value
m_editphonectrl.m_strmaskliteral = m_editphonectrl.m_str; //backspace replace string
m_editphonectrl.setwindowtext(m_editphonectrl.m_str);
</tt></font></pre>
<p><strong>ip address<br>
</strong></p>
<p>create a cmaskedit control called for example m_editipctrl. then in oninitdialog
write...<br>
</p>
<pre><font color="#990000"><tt>
//cmaskedit - ip address initialisation
m_editipctrl.m_bistime = false;
m_editipctrl.m_isdate = false;
m_editipctrl.m_busemask = true;
m_editipctrl.m_strmask = "999.999.999.999";
m_editipctrl.m_strliteral = "___.___.___.___";
m_editipctrl.m_str = "209.66 .99 .126";
m_editipctrl.m_strmaskliteral = m_editipctrl.m_str;
m_editipctrl.setwindowtext(m_editipctrl.m_str);
</tt></font></pre>
<p><strong>post code<br>
</strong></p>
<p>create a cmaskedit control called for example m_editpcodectrl. then in oninitdialog
write...<br>
</p>
<pre><font color="#990000"><tt>
//cmaskedit - ipost code initialisation
m_editpcodectrl.m_bistime = false;
m_editpcodectrl.m_isdate = false;
m_editpcodectrl.m_busemask = true;
m_editpcodectrl.m_strmask = "ll00 0ll";
m_editpcodectrl.m_strliteral = "____ ___";
m_editpcodectrl.m_str = "le12 7at";
m_editpcodectrl.m_strmaskliteral = m_editpcodectrl.m_str;
m_editpcodectrl.setwindowtext(m_editpcodectrl.m_str);
</tt></font></pre>
</blockquote>
<blockquote>
<p align="left"><strong>cmaskedit m_strmask<br>
</strong>the member variable m_strmask specifies what type of characters can be written
and where.</p>
<table border="0" width="31%">
<tr>
<td width="20%"><strong>0</strong></td>
<td width="50%">digit only</td>
</tr>
<tr>
<td width="20%"><strong>9</strong></td>
<td width="50%">digit or space</td>
</tr>
<tr>
<td width="20%"><strong>#</strong></td>
<td width="50%">digit or space or + or -</td>
</tr>
<tr>
<td width="20%"><strong>l</strong></td>
<td width="50%">alpha only</td>
</tr>
<tr>
<td width="20%"><strong>?</strong></td>
<td width="50%">alpha or space</td>
</tr>
<tr>
<td width="20%"><strong>a</strong></td>
<td width="50%">alphanumeric</td>
</tr>
<tr>
<td width="20%"><strong>a</strong></td>
<td width="50%">alphanumeric or space</td>
</tr>
<tr>
<td width="20%"><strong>&</strong></td>
<td width="50%">all printable characters</td>
</tr>
</table>
</blockquote>
<p><a name="updates"></a><strong>updates</strong></p>
<p><strong>12th august 1998<br>
</strong> cmaskedit : added the use of backspace to replace what has already
been typed with another string (by <a href="mailto::rfujimoto@oceania.com">rodney fujimoto</a>).
the examples use the original string as a replacement, however by default the
replacement is blank, thus nothing will be replaced by the backspace key.<br>
provided separate .zip file of masked.cpp and masked.h to demo .zip file.<br>
updated demo .exe.</p>
<p><strong>7th august 1998<br>
</strong> ctimeedit : added settime(cstring date), and cstring gettimestr().<br>
cmaskedit : cured bug with "9" masks not allowing spaces.<br>
updated article and demo .exe, added examples.</p>
<p><strong>27th july 1998<br>
</strong>original version</p>
<p>last updated: 12 august 1998 </p>
<!--comments-->
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -