View previous topic :: View next topic |
Author |
Message |
nanobyte
Joined: 13 Jul 2006 Posts: 2
|
Posted: Thu Jul 13, 2006 6:12 am Post subject: Message maps vs. virtual functions |
|
|
I'm a little confused about which approach is best. Proponents of message maps seem to claim that if you didn't use them, you'd have to create one virtual function per message in a base window class.
Isn't that simply ridiculous? Wouldn't it be possible to create exactly *one* virtual window procedure in a base window class, then create *only* virtual functions for the messages a derived class is interested in, for example "HandleClick" in a button class and leave all other handling to the one and only virtual window procedure?
DFL sems to use this approach, am I correct? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Thu Jul 13, 2006 10:55 am Post subject: Re: Message maps vs. virtual functions |
|
|
nanobyte wrote: | Wouldn't it be possible to create exactly *one* virtual window procedure in a base window class, then create *only* virtual functions for the messages a derived class is interested in, for example "HandleClick" in a button class and leave all other handling to the one and only virtual window procedure?
DFL sems to use this approach, am I correct? |
DFL has a virtual function for each of the events, called by the virtual window procedure. In a derived class, you can override such virtual functions you wish to handle. You also have the option to simply attach delegates to event handlers, which does not require deriving; you can even attach to events of unrelated objects.
Code: |
// Overriding virtual function:
class MyControl: Control
{
override void onClick(EventArgs ea)
{
// override virtual function
// Note: should also call super.onClick(ea) so that click events fire.
}
}
// Attaching to event:
Control ctrl = some_control;
ctrl.click ~= &mydelegate;
|
|
|
Back to top |
|
|
nanobyte
Joined: 13 Jul 2006 Posts: 2
|
Posted: Thu Jul 13, 2006 11:33 am Post subject: |
|
|
Yes. So it is basically like this (?):
Code: |
class BasicWindow
{
protected virtual void OnResize(ResizeEventArgs e) // Introduced by BasicWindow
{
// ... fire event here ...
}
public virtual void HandleMessage(Message message)
{
switch(message.osMessage)
{
case OS_RESIZE:
ResizeEventArgs e = new ResizeEventArgs(message.osParamX, message.osParamY);
OnResize(e);
break;
...
}
}
}
class MyWindow : BasicWindow
{
protected override void OnResize(ResizeEventArgs e) // Introduced by BasicWindow
{
base.OnResize(e); // let base fire event
Debug("I have been resized.");
}
protected virtual OnMouseOver(MouseOverEventArgs e) // Introduced by MyWindow
{
}
public virtual HandleMessage(Message message)
{
base.HandleMessage(message); // OS_RESIZE etc.
switch(message.osMessage)
{
case OS_MOUSEOVER: // BasicWindow not interested in this, but we and our descendents are
MouseOverEventArgs e = new MouseOverEventArgs(message.osParamX, message.osParamY);
OnMouseOver(e);
break;
...
}
}
}
|
|
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Thu Jul 13, 2006 8:35 pm Post subject: |
|
|
Affirmative |
|
Back to top |
|
|
|