📄 edit_cr.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">eating returns in a single line edit</font></h3>
<hr align="center">
<p>this code was contributed by <a href="mailto:petr.novotny@antek.cz">petr novotny</a> <!-- start --> </p>
<p>i was quite frustrated in my app - i have a dialog with a few single-line edit
controls; after updating the edits, when i pressed enter the dialog was closed (idok was
the default button). "want return" option is unfortunately valid only for
multiline edits.</p>
<p>here is my fix:</p>
<p>i override pretranslatemessage. if there is wm_keydown with vk_enter going to the edit,
i set the focus to the default button and discard the message. if the dialog has no
defualt button, i let vk_enter through - no problem this time.</p>
<p>i check if the window is an editbox by calling getclassname and comparing the return
value to "edit". there are much more possibilities - comparing the
fromhandle(hwnd) to array of cwnd*, to array of ids (comparing to getdlgitem() return
values) etc.</p>
<p>for the code to be completely correct, it should check for the edit's windows style and
it should not act if it is multi-line, want-return edit. [i haven't done that as i don't
use multiline edits.]</p>
<p>1. to your override of cdialog, add the following method:</p>
<pre><font color="#990000"><tt>
virtual bool pretranslatemessage(msg *);
</pre>
</tt></font>
<p>2. implement this function, for example:</p>
<pre><font color="#990000"><tt>
bool cmydialog::pretranslatemessage(msg *pmsg)
{
if (pmsg->message==wm_keydown && pmsg->wparam==vk_return)
{
dword def_id=getdefid();
if (def_id!=0)
{
cwnd *wnd=fromhandle(pmsg->hwnd);
// you may implement other ways of testing, e.g.
// comparing to array of cwnd*, comparing to array of ids etc.
char class_name[16];
if (getclassname(wnd->getsafehwnd(),class_name,sizeof(class_name))!=0)
{
if (strnicmp(class_name,"edit",5)==0)
{
getdlgitem(loword(def_id))->setfocus();
return true;
// discard the message!
}
}
}
}
// be a good citizen - call the base class
return cdialog::pretranslatemessage(pmsg);
}
</pre>
</tt></font><!-- end -->
<p>posted on: april 11, 98. </p>
<!--comments-->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -