📄 smartedit.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">smartedit control </font></h3>
<hr>
<!-- author and contact details -->
<p>this article was contributed by <a href="mailto:ry@proaxis.com">rick york</a>. <!-- sample image - gif or jpg --> </p>
<p><img src="smartedit.jpg" tppabs="http://www.codeguru.com/editctrl/smartedit.jpg"
width="474" height="409" alt="smartedit and slider test"> <!-- text / source code --> </p>
<p>the code included here implements what i call a smart edit control. this began
life in a sample from the msdn called ctrltest. this control was originally called
cparsededit and it allows one to specify types of allowable characters that can be entered
into an edit box. i have changed its name to csmartedit and added more functionality
to it. it now supports more character types including numbers, characters, floating
point (with exponents), underscores, and negative signs. the biggest enhancement in
functionality is that one can associate or link an edit box with a slider to provide what
i call coordinated updates. this means that if you drag the slider around you will
see a corresponding change in the number displayed in the edit box and vice-versa. i
call the derived slider control class clinkslider. </p>
<p>as an added bonus, i have included some bitmapped button images that came with the
original sample and some that i drew myself. the bitmaps i drew are for the disabled
states of ok and cancel and for all four states of the apply and help buttons. the
four button states are up, down, focused, and disabled. the names of bitmaps end in
u, d, f, and x for the four states. </p>
<p>it is very easy to use the csmartedit control. these are the steps :
<ul>
<li>declare a member variable of type csmartedit in the afx_data section of the dialog.</li>
<li>add a ddx_control statement in the afx_data_map to associate the resource to the member.</li>
<li>in oninitdialog set the type of the control with setparsetype.</li>
</ul>
<p>as you probably know, the class wizard can do the first two steps for you. note
that the resource style of the edit box does not have to be anything special. </p>
<p>it is also very easy to use the clinkslider control. these are the steps :
<ul>
<li>add a csmartedit control as in steps 1 and 2 above.</li>
<li>add a clinkslider control similar to steps 1 and 2 above.</li>
<li>in oninitdialog link the slider and edit box by calling setslidelink and pass the
resource id of the slider.</li>
<li>also in oninitdialog, set the minimum and maximum values and the number of ticks for the
slider with setparams there are two versions of this function, one for integers and
one for floating point doubles. the floating point version also takes a format
string that specifies how the value will be displayed.</li>
</ul>
<p>here is a code snippet that illustrates using a smart edit box and two linked
slider-edit boxes, one integer and one floating point. <!-- start a block of source code --> </p>
<pre><tt><font color="#990000">
// dialog data in dialog class declaration
//{{afx_data(ctestslidersdlg)
enum { idd = idd_slide_dlg };
csmartedit m_edit1;
csmartedit m_edit2;
csmartedit m_edit3;
clinkslider m_slider1;
clinkslider m_slider2;
//}}afx_data
...
// in dialog's dodataexchange function
cdialog::dodataexchange(pdx);
//{{afx_data_map(ctestslidersdlg)
ddx_control(pdx, idc_edit1, m_edit1);
ddx_control(pdx, idc_edit2, m_edit2);
ddx_control(pdx, idc_edit3, m_edit3);
ddx_control(pdx, idc_slider1, m_slider1);
ddx_control(pdx, idc_slider2, m_slider2);
//}}afx_data_map
...
// in dialog's oninitdialog function
cdialog::oninitdialog();
// setup first slider-edit box - integer
m_edit1.setslidelink( this, idc_slider1 );
m_edit1.setparams( -100, 100, 10 );
m_edit1.setvalue( 0 );
// setup second slider-edit box - floating point
m_edit2.setslidelink( this, idc_slider2 );
m_edit2.setparams( 0.0, 10.0, 10, "%6.3f" );
m_edit2.setvalue( 2.0 );
// setup third edit box - it is not linked and accepts only letters
m_edit3.setparsetype( ses_letters );
<!-- end the block of source code -->
</font></tt></pre>
<p>lastly, i will briefly describe how to use the bitmapped buttons.
<ul>
<li>define a button resource in the dialog that has owner draw style enabled.</li>
<li>declare a variable in the dialog of type cbitmapbutton.</li>
<li>in oninitdialog call button.autoload( buttonid, this )</li>
</ul>
<p>that's all there is to it. the one gotcha to using autoload is that the text of
the button must match the name of the bitmap. this means that for the cancel button,
its text must be cancel and for the apply button, its text must be apply. note that
case does not matter for the text of the button. see the documentation on
cbitmapbutton for more details. </p>
<p>a note about unicode : first of all, i have attempted to make this compatable with
unicode but i have not tested it with a mbcs. the principle area where it matters
use unicode-compatable functions for checking each character entered into the edit
box. please let me know of any problems encountered (and successes :) </p>
<p>the demo project is a dialog app having four dialogs. one is the choser dialog
and the others are for testing just the edit boxes, just the buttons, and one that shows
all of the controls together as depicted in the image. </p>
<p>this code was developed and tested using visual c++ v5.0 on nt v4.0 sp3. </p>
<p><a href="smartedit_demo.zip"
tppabs="http://www.codeguru.com/editctrl/smartedit_demo.zip">download demo project - 16 kb</a>
</p>
<p><a href="smartedit_src.zip" tppabs="http://www.codeguru.com/editctrl/smartedit_src.zip">download
source - 6 kb</a> </p>
<p>date posted: october 27, 1998 <!--comments--> </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -