📄 inplace_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">inplace edit control</font></h3>
<hr>
<p>this code was contributed by <a href="mailto:marioc@computer.org">mario contestabile</a>.
</p>
<p>have you ever wanted to allow users of your mfc application to rename strings contained
in various controls? if so, you probably included a "rename" button next to your
control. take a listbox for example. when "rename" is pressed you retrieve the
current selection from the listbox, display it in a dialog for the user to modify, and
then modify the string in the listbox to the new string. not a very aesthetic or efficient
method. </p>
<p>a better solution is to dynamically create an edit control on top of the area where the
displayed string is, preferably following a double-click of the string in the listbox. you
could re-use this edit class throughout your application, thus removing the need for
"rename" buttons. this is common technique. </p>
<p>one method our smartedit can use to notify its parent that the user is done with it
(<enter> was pressed for example) is to send its parent a custom message. but how
does the parent (in our example, a dialog with a list control) know what new string the
user typed? we could pass "this" to our smartedit, which in turn could call a
dialog function passing it the new string. although convenient, this method would not make
our smartedit very re-usable, since it would have to know a) the type of cdialog our
parent is and b) every different parent dialog would need to implement this same function
to accept the new string. </p>
<p>the method i chose was to post a message to the parent, essentially telling it
"hay, the user possibly modified this string you sent me. here it is, do as you
please." but right before posting this message to its parent, smartedit places the
text on the clipboard. this way, smartedit doesn't care what kind of window its parent is,
and the parent can handle it as it seems fit. </p>
<p>||||| <b>figure 1</b> "mydialog.cpp lbn_dblclk handler" </p>
<pre><tt><font color="#990000">
void mydialog::ondblclkstringinlistbox()
{
const int nindex = m_ctl.getcursel();
if(nindex == lb_err) return;
cstring string;
m_ctl.gettext(nindex, string);
rect rect;
int result = m_ctl.getitemrect(nindex, &rect);
if(result==lb_err) return;
smartedit* pedit = new smartedit;
rect.bottom += 4;
pedit->create(ws_child | ws_visible | ws_border | es_autohscroll,
rect, &m_ctl, (uint)-1);
pedit->setwindowtext(string);
pedit->setfocus();
pedit->limittext(mymax_len); // user defined maximum length of string
}
</font></tt></pre>
<p>figure 1 shows the on_lbn_dblclk handler from the mydialog class. this is the handler
called when the user double-clicks a string in the listbox. we first determine which, if
any, of the listbox strings is currently selected. note getcursel() can only be used with
a single-selection listbox. the actual string is then retrieved from the listbox. the
dimension of the item's rectangle is determined since this will be the size and position
of our smartedit. the smartedit is created on the heap, and its create() function called
which creates the windows edit control and attaches it to the cedit object. </p>
<p>||||| <b>figure 2</b> "smartedit.cpp implementation" </p>
<pre><tt><font color="#990000">
smartedit::smartedit() : bescapekey(false)
{
}
begin_message_map(smartedit, cedit)
//{{afx_msg_map(smartedit)
on_wm_killfocus()
//}}afx_msg_map
end_message_map()
void smartedit::onkillfocus(cwnd*)
{
postmessage(wm_close, 0, 0);
if(!bescapekey){
cstring str;
getwindowtext(str);
coledatasource *pds = new coledatasource;
ptchar cp = (ptchar)globalalloc(gmem_fixed, (str.getlength() *
sizeof(tchar)) + sizeof(tchar));
_tcscpy(cp, str);
pds->cacheglobaldata(cf_text, cp);
pds->setclipboard();
getowner()->postmessage(editclassmsg);
trace1("smartedit::onkillfocus posting message to my owner, i have put
[%s] on the clipboard for him\n", cp);
}
}
// "override" base class member function
void smartedit::postncdestroy()
{
delete this;
}
// "augment" base class member function
bool smartedit::pretranslatemessage(msg* pmsg)
{
if(pmsg->wparam == vk_return){
postmessage(wm_close, 0, 0);
return true;
}else if(pmsg->wparam == vk_escape){
postmessage(wm_close, 0, 0);
return bescapekey = true;
}
return cedit::pretranslatemessage(pmsg);
}
</font></tt></pre>
<p>figure 2 shows smartedit's implementation. it is straightforward, but perhaps the
postncdestroy function needs further explanation. remember this object was created on the
heap with "new" in mydialog. that means someone must "delete" it.
since postncdestroy() is called after the window has been destroyed, it is a perfect time
to delete the object. modeless dialogs use this method of self-destruction. </p>
<p>the most important action occurs in its onkillfocus() handler. when the smartedit loses
focus by a means other than the <escape> key, it retrieves the new string with getwindowtext().
the string is then placed on the clipboard using a coledatasource. coledatasource is a
source actor in ole data transfer. notice i used ptchar, and _tcscpy() to allow the code
to be transparantly compiled in both ansi and unicode applications. after the string is
placed on the clipboard, it sends editclassmsg (simply #defined as wm_app + 100) to its
parent. </p>
<p>||||| <b>figure 3</b> "mydialog.cpp editclassmsg handling code" </p>
<pre><tt><font color="#990000">
bool mydialog::pretranslatemessage(msg* pmsg)
{
if(editclassmsg == pmsg->message){
coledataobject data;
hclipboard()){
if(data.isdataavailable(cf_text)){
hglobal hg;
if(hg = data.getglobaldata(cf_text)){
cstring str = (lpctstr)globallock(hg);
trace1("mydialog::pretrans(editclassmsg) this is on the clipboard [%s]\n", str);
globalunlock(hg);
newstring(str); // appropriate action
}
}
data.release();
}
return true;
}
return cdialog::pretranslatemessage(pmsg);
}
</font></tt></pre>
<p>fogure 3 shows what mydialog does when handling a editclassmsg message. it knows
there's a string on the clipboard, so uses coledataobject to retrieve it. coledataobject
is the destination actor in ole data transfer. the string from the clipboard is simply
passed to a helper function, new string() in this case, which will decide a course of
action. to replace the string that was 'underneath' the string double-clicked in the
listbox, you would keep the item index obtained in ondblclkstringinlistbox(). for example,
"const int nindex = m_ctl.getcursel();" could be replaced by "m_nindex =
m_ctl.getcursel();" after which newstring() would use mydialog's member variable
'm_nidex' as the index for the new string. </p>
<p>we now have a fully functional smartedit capable of notifying its parent (using ole)
that it has accepted a string. one problem still remains. can you spot it? since the
smartedit object 'kills' itself using 'delete' in postncdestroy(), it *must* be created on
the heap using 'new'. if we don't enforce this, someone will inevitably use our smartedit
in a ddx_control(). this will cause serious havoc when the "delete this;" line
is executed. so what can we do? </p>
<p>the answer is simple. make the ~smartedit destructor protected. this still allows the
class to be used in inheritance, but prevents its usage on the stack. </p>
<p>posted: march, 8, 98 </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -