Re: Channel 9 video: Visual C++ 10 is the new 6
On Mon, 24 Nov 2008 09:32:09 +0100, "Giovanni Dicanio"
<giovanniDOTdicanio@REMOVEMEgmail.com> wrote:
"Doug Harrison [MVP]" <dsh@mvps.org> ha scritto nel messaggio
news:13nji4dnntjghjo496uhjrbfo7hd3agap0@4ax.com...
Normally you would use ClassWizard and the message map to implement
LB_INSERTSTRING and other messages, and the only way the message map gets
used is by calling SendMessage and friends. So the answer I had in mind
was
to use SendMessage instead of calling the member function directly. That
way, the virtual function GetMessageMap is called, and the message map
search begins in the most derived object that has a message map.
Overriding the virtual WindowProc method as I wrote above, allows the code
you posted (i.e. calling the member function directly) to just work, without
call to SendMessage:
One shortcoming to overriding WindowProc is that it doesn't provide an
overt function OnInsertString for derived classes that implement
LB_INSERTSTRING to call up to.
I may have confused things by presenting two closely related but different
examples. I gave the InsertString example only because I once read a paper
in which the author made the mistake of falsely overriding
CListBox::InsertString or similar function, explained that the bogus
override wouldn't get called through a pointer returned by GetDlgItem(),
hence you shouldn't use GetDlgItem. I thought this was an even subtler
mistake than calling w.OnSomeMessage(), and it came to mind first.
The point I was trying to make is that ordinary OnXXX handlers get called
only through SendMessage, so I wish you had used my second example. :)
Indeed, CListBox::InsertString is just a simple wrapper around SendMessage,
and focusing only on my first example, the answer is to *not* write your
own InsertString but implement LB_INSERTSTRING instead. Then it's fine to
call lb.InsertString(), which is just SendMessage by another name.
Regard the implementation of LB_INSERTSTRING with ClassWizard, as you
suggested, I can't find how to do it on VS2008... what am I missing here?
Offhand, I have no idea if ClassWizard supports LB_INSERTSTRING, which is
partly why I added "and other messages". Irrespective of ClassWizard
support for the message, I would use the message map, even if I had to use
ON_MESSAGE, which I think is more "the MFC way" than overriding WindowProc.
If you start using WindowProc as an alternative to the message map, then
chaining to the base class handler from derived classes becomes very dodgy;
those that call a base class OnXXX could skip intermediate bases that use
WindowProc, and a WindowProc user that chains to its base class's
WindowProc could skip intermediate bases that implement OnXXX. Best to keep
things consistent and use the message map for everything.
--
Doug Harrison
Visual C++ MVP