📄 ip_addr_masked_edit.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">ip address masked edit</font></h3>
<hr align="center">
<p>this article was contributed by <a href="mailto:dziedzic@ma.ultranet.com">joseph a.
dziedzic</a>. this code actually creates a custom control, instead of using an edit
control. <a href="ip_addr_masked_edit.zip"
tppabs="http://www.codeguru.com/editctrl/ip_addr_masked_edit.zip">download sample</a>. </p>
<p><img src="ip_addr_masked_edit.gif"
tppabs="http://www.codeguru.com/editctrl/ip_addr_masked_edit.gif" width="284" height="175"></p>
<p>iptest.exe demonstrates an mfc-based custom control which is very similar to the one
used in the windows nt 4.0 "network" applet in control panel. the custom control
can be used in a dialog or a property page; i haven't tried it in a form. </p>
<p>the control is provided free of charge with the caveat that no warranty is expressed or
implied. feel free to email me if you discover any nasty bugs. <br>
joseph a. dziedzic <br>
dziedzic@ma.ultranet.com </p>
<h3>changes from the original (september 1997) version</h3>
<ol>
<li>the control now validates fields as data is entered for values greater than 255. if you
want to defer validation until ddx time (the old mode of operation), specify the style bit
ipas_novalidate (defined in ipaddr.h) when creating the control. </li>
<li>the ddx_ipaddr routine has been moved into ipaddr.cpp. you can use this default
implementation, or write your own. there is a version that works with an array of bytes as
well as a string of "."-separated fields. </li>
<li>a few tweaks were made so the control matches the one used in windows 95/nt's network
control panel applet. specifically, space is now accepted as an indication to move to the
next address field and focus is advanced to the next address field as soon as three
numeric characters have been entered in a field. </li>
<li>the control now has a create method to allow dynamic creation. </li>
<li>a number of bugs were fixed including: handling of the delete key and use of the control
on a property page. </li>
<li>the structure named ipaddr (used to pass ip addresses in windows messages) was renamed
to avoid a conflict with a typedef in the include file ras.h; the structure is now called
ipa_addr. (most users of the control won't notice this change.) </li>
<li>the messages to and from the control were renamed from ipmxxx to ipamxxx to avoid
conflicts with the ie 4 common controls dll. </li>
</ol>
<h3>how to use the ip address custom control</h3>
<p>(see iptestdlg.h and iptestdlg.cpp for the code outlined below) </p>
<h4>add the files ipaddr.cpp and ipaddr.h to your project </h4>
<p>these files are the only ones in iptest.zip that you'll need. </p>
<h4>add a custom control to a dialog resource </h4>
<p>size the control to 90 x 13 dialog units. set the class to "ipaddr" and the
style to 0x50810000 (ws_visible | ws_child | ws_border | ws_tabstop). </p>
<h4>edit the dialog class definition (.h) file as follows:</h4>
<p>add a member variable after the closing }}afx_data line: </p>
<pre><tt><font color="#990000">
unsigned char m_naddr[4];
</font></tt></pre>
<p>add the following lines after the declare_message_map macro: </p>
<pre><tt><font color="#990000">
private:
cipaddrctl* m_pipaddrctl;
</font></tt></pre>
<h4>edit the dialog class implementation (.cpp) file as follows:</h4>
<p>add the following lines after the closing }}afx_data_init line: </p>
<pre><tt><font color="#990000">
m_pipaddrctl = null;
memset(m_naddr, 0, 4);
</font></tt></pre>
<p>add the following lines after the call to cdialog::dodataexchange in the dialog class
implementation of ::dodataexchange: </p>
<pre><tt><font color="#990000">
// can't hack ddx_control'ing the ip address control since the control code subclasses the control behind our back
// fortunately, that means we can do *one* getdlgitem call and salt away the returned cwnd-derived
// object pointer for use during the entire dialog
if (null == m_pipaddrctl)
m_pipaddrctl = (cipaddrctl*) getdlgitem(idc_ipaddr);
assert(null != m_pipaddrctl);
</font></tt></pre>
<p>add the following line after the closing }}afx_data_map line: </p>
<pre><tt><font color="#990000">
ddx_ipaddr(pdx, idc_ipaddr, m_naddr);
</font></tt></pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -