By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

Hover effects, IMessageFilter not working as expected

P: n/a
Hi,
I'm developping a form that renders differently when hovered and trigger
some processing when not hovered (leaved). The thing is, when I leave my
form and another form from my application is underneath, it works fine, but
if I leave my form and anything that is not from my application is
underneath, the Leave event does not fire until I hover something from my
application.

Here's my filtered code for the IMessageFilter (I did not pasted the
declarations, I just pasted my message proc) (at the end of the post)

Can someone tell me what's wrong with this code?

thanks

ThunderMusic

Expand|Select|Wrap|Line Numbers
  1. public bool PreFilterMessage(ref Message m)
  2. {
  3. // Everything is done with WM_MOUSEMOVE
  4. if (m.Msg == WM_MOUSEMOVE)
  5. {
  6. // look if the target is our control or contained by our control
  7. Control ctrl = Control.FromHandle(m.HWnd);
  8. if (ctrl != null && (ctrl == this._container ||
  9. this._container.Contains(ctrl)))
  10. {
  11. // if this message WM_MOUSEMOVE is the first we get,
  12. // we must trigger the MouseEnter event
  13. if (!this._isEntered)
  14. {
  15. // trigger MouseEnter
  16. this.OnMouseEnter();
  17. this._isEntered = true;
  18. }
  19. // Get point (using target control coordinates)
  20. Point pt = new Point(m.LParam.ToInt32());
  21. // convert to controls coordinates if necessary
  22. Point containerPoint;
  23. if (ctrl != this._container)
  24. containerPoint =
  25. this._container.PointToClient(ctrl.PointToScreen(pt));
  26. else
  27. containerPoint = pt;
  28. // Trigger MouseMove
  29. this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0,
  30. containerPoint.X, containerPoint.Y, 0));
  31. }
  32. else
  33. {
  34. // If the target is not a child and MouseLeaves has not been
  35. triggered
  36. // we must trigger MouseLeave
  37. if (this._isEntered)
  38. {
  39. // Trigger MouseLeave
  40. this.OnMouseLeave();
  41. this._isEntered = false;
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47.  

Oct 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Have you tried using the Activated and Deactivated events instead? See
this from MSDN help:

The Enter and Leave events are suppressed by the Form class. The
equivalent events in the Form class are the Activated and Deactivate
events. The Enter and Leave events are hierarchical and will cascade up
and down the parent chain until the appropriate control is reached. For
example, assume you have a Form with two GroupBox controls, and each
GroupBox control has one TextBox control. When the caret is moved from
one TextBox to the other, the Leave event is raised for the TextBox and
GroupBox, and the Enter event is raised for the other GroupBox and
TextBox.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com


"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl:
Hi,
I'm developping a form that renders differently when hovered and trigger
some processing when not hovered (leaved). The thing is, when I leave my
form and another form from my application is underneath, it works fine, but
if I leave my form and anything that is not from my application is
underneath, the Leave event does not fire until I hover something from my
application.

Here's my filtered code for the IMessageFilter (I did not pasted the
declarations, I just pasted my message proc) (at the end of the post)

Can someone tell me what's wrong with this code?

thanks

ThunderMusic

Expand|Select|Wrap|Line Numbers
  1. public bool PreFilterMessage(ref Message m)
  2. {
  3.     // Everything is done with WM_MOUSEMOVE
  4.     if (m.Msg == WM_MOUSEMOVE)
  5.     {
  6.         // look if the target is our control or contained by our control
  7.         Control ctrl = Control.FromHandle(m.HWnd);
  8.         if (ctrl != null && (ctrl == this._container ||
  9. this._container.Contains(ctrl)))
  10.         {
  11.             // if this message WM_MOUSEMOVE is the first we get,
  12.             // we must trigger the MouseEnter event
  13.             if (!this._isEntered)
  14.             {
  15.                 // trigger MouseEnter
  16.                 this.OnMouseEnter();
  17.                 this._isEntered = true;
  18.             }
  19.             // Get point (using target control coordinates)
  20.             Point pt = new Point(m.LParam.ToInt32());
  21.             // convert to controls coordinates if necessary
  22.             Point containerPoint;
  23.             if (ctrl != this._container)
  24.                 containerPoint =
  25. this._container.PointToClient(ctrl.PointToScreen(pt));
  26.             else
  27.                 containerPoint = pt;
  28.             // Trigger MouseMove
  29.             this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0,
  30. containerPoint.X, containerPoint.Y, 0));
  31.         }
  32.         else
  33.         {
  34.             // If the target is not a child and MouseLeaves has not been
  35. triggered
  36.             // we must trigger MouseLeave
  37.             if (this._isEntered)
  38.             {
  39.                 // Trigger MouseLeave
  40.                 this.OnMouseLeave();
  41.                 this._isEntered = false;
  42.             }
  43.         }
  44.     }
  45.     return false;
  46. }
  47.  
Oct 27 '06 #2

P: n/a
hi, thanks for your answer... unfortunately, it's not exactly what I
need... Activate and Deactivate are triggered when a user clicks on a form
(activated) or click on another form or minimize (Deactivate)... The
behavior I need is when the user move his mouse over the form or out of the
form...

I agree that the MouseEnter and MouseLeave of the form are hierarchical,
that's why I did implement IMessageFilter.... Now the thing is, I must
discover how to receive the message (or event) so the program can tell when
the mouse cursor went out of the form's boundaries.

Thanks

ThunderMusic

"Bryan Phillips" <bp*******@nospam.crowechizek.com.spammenotwrote in
message news:O7****************@TK2MSFTNGP05.phx.gbl...
Have you tried using the Activated and Deactivated events instead? See
this from MSDN help:

The Enter and Leave events are suppressed by the Form class. The
equivalent events in the Form class are the Activated and Deactivate
events. The Enter and Leave events are hierarchical and will cascade up
and down the parent chain until the appropriate control is reached. For
example, assume you have a Form with two GroupBox controls, and each
GroupBox control has one TextBox control. When the caret is moved from one
TextBox to the other, the Leave event is raised for the TextBox and
GroupBox, and the Enter event is raised for the other GroupBox and
TextBox.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com


"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl:
>Hi,
I'm developping a form that renders differently when hovered and trigger
some processing when not hovered (leaved). The thing is, when I leave my
form and another form from my application is underneath, it works fine,
but
if I leave my form and anything that is not from my application is
underneath, the Leave event does not fire until I hover something from my
application.

Here's my filtered code for the IMessageFilter (I did not pasted the
declarations, I just pasted my message proc) (at the end of the post)

Can someone tell me what's wrong with this code?

thanks

ThunderMusic

Expand|Select|Wrap|Line Numbers
  1. public bool PreFilterMessage(ref Message m)
  2. {
  3.     // Everything is done with WM_MOUSEMOVE
  4.     if (m.Msg == WM_MOUSEMOVE)
  5.     {
  6.         // look if the target is our control or contained by our control
  7.         Control ctrl = Control.FromHandle(m.HWnd);
  8.         if (ctrl != null && (ctrl == this._container ||
  9. this._container.Contains(ctrl)))
  10.         {
  11.             // if this message WM_MOUSEMOVE is the first we get,
  12.             // we must trigger the MouseEnter event
  13.             if (!this._isEntered)
  14.             {
  15.                 // trigger MouseEnter
  16.                 this.OnMouseEnter();
  17.                 this._isEntered = true;
  18.             }
  19.             // Get point (using target control coordinates)
  20.             Point pt = new Point(m.LParam.ToInt32());
  21.             // convert to controls coordinates if necessary
  22.             Point containerPoint;
  23.             if (ctrl != this._container)
  24.                 containerPoint =
  25. this._container.PointToClient(ctrl.PointToScreen(pt));
  26.             else
  27.                 containerPoint = pt;
  28.             // Trigger MouseMove
  29.             this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0,
  30. containerPoint.X, containerPoint.Y, 0));
  31.         }
  32.         else
  33.         {
  34.             // If the target is not a child and MouseLeaves has not been
  35. triggered
  36.             // we must trigger MouseLeave
  37.             if (this._isEntered)
  38.             {
  39.                 // Trigger MouseLeave
  40.                 this.OnMouseLeave();
  41.                 this._isEntered = false;
  42.             }
  43.         }
  44.     }
  45.     return false;
  46. }

Oct 27 '06 #3

P: n/a
Hi,

Here is your problem. You are using MouseMove event which is based on
WM_MOUSEMOVE. This message as any other mouse messages are posted to the
window under the mouse cursor. Because you use message filter you receive
this message if the mouse is over any form (control) that belongs to your
application. However when the mouse moves over window that is not part of
your application we just stop receiving mouse messages and there is no
notification when this happens they just stop. Windows have three solutions
to the problem (at least these are the ones that I can come up with at the
moment): installing window hook WH_MOUSE_LL, using TrackMouseEvent API and
setting mouse capture.

- WH_MOUSE_LL are possible to be implemented in .NET, but it requires using
PInvoke and I think it is the hardest one to implement
- Setint the mouse capture means that the mouse message will be dispatched
to your window even when the mouse cursor is outside of the window. The
problem is that it works only if mouse button has been pressed when the
mouse is inside the window is held down while moving the mouse. WindowsForms
sets the mouse capture when the mouse button is pressed, so it should work
by itself, but if it doesn't work for you that means you want to register
mouse leave not only when the mouse button is down.
- TrackMouseEvent is used internaly by Windows forms and the functionality
is exposed as MouseLeave and MouseHover events. Why don't use these events
instead of implementing your own solution?
The only problem that I see is that you are going to receive mouse leave
even if the mouse is inside the form, but enters some form's child control.
This cases can be filtered our by checking the form hosting tghe control.
The WM_MOUSELEAVE used internally is posted to the message queue, thus can
be handeled with message filter
--
HTH
Stoitcho Goutsev (100)

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:Ot****************@TK2MSFTNGP02.phx.gbl...
hi, thanks for your answer... unfortunately, it's not exactly what I
need... Activate and Deactivate are triggered when a user clicks on a
form (activated) or click on another form or minimize (Deactivate)... The
behavior I need is when the user move his mouse over the form or out of
the form...

I agree that the MouseEnter and MouseLeave of the form are hierarchical,
that's why I did implement IMessageFilter.... Now the thing is, I must
discover how to receive the message (or event) so the program can tell
when the mouse cursor went out of the form's boundaries.

Thanks

ThunderMusic

"Bryan Phillips" <bp*******@nospam.crowechizek.com.spammenotwrote in
message news:O7****************@TK2MSFTNGP05.phx.gbl...
>Have you tried using the Activated and Deactivated events instead? See
this from MSDN help:

The Enter and Leave events are suppressed by the Form class. The
equivalent events in the Form class are the Activated and Deactivate
events. The Enter and Leave events are hierarchical and will cascade up
and down the parent chain until the appropriate control is reached. For
example, assume you have a Form with two GroupBox controls, and each
GroupBox control has one TextBox control. When the caret is moved from
one TextBox to the other, the Leave event is raised for the TextBox and
GroupBox, and the Enter event is raised for the other GroupBox and
TextBox.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com


"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl:
>>Hi,
I'm developping a form that renders differently when hovered and trigger
some processing when not hovered (leaved). The thing is, when I leave my
form and another form from my application is underneath, it works fine,
but
if I leave my form and anything that is not from my application is
underneath, the Leave event does not fire until I hover something from
my
application.

Here's my filtered code for the IMessageFilter (I did not pasted the
declarations, I just pasted my message proc) (at the end of the post)

Can someone tell me what's wrong with this code?

thanks

ThunderMusic

Expand|Select|Wrap|Line Numbers
  1. public bool PreFilterMessage(ref Message m)
  2. {
  3.     // Everything is done with WM_MOUSEMOVE
  4.     if (m.Msg == WM_MOUSEMOVE)
  5.     {
  6.         // look if the target is our control or contained by our control
  7.         Control ctrl = Control.FromHandle(m.HWnd);
  8.         if (ctrl != null && (ctrl == this._container ||
  9. this._container.Contains(ctrl)))
  10.         {
  11.             // if this message WM_MOUSEMOVE is the first we get,
  12.             // we must trigger the MouseEnter event
  13.             if (!this._isEntered)
  14.             {
  15.                 // trigger MouseEnter
  16.                 this.OnMouseEnter();
  17.                 this._isEntered = true;
  18.             }
  19.             // Get point (using target control coordinates)
  20.             Point pt = new Point(m.LParam.ToInt32());
  21.             // convert to controls coordinates if necessary
  22.             Point containerPoint;
  23.             if (ctrl != this._container)
  24.                 containerPoint =
  25. this._container.PointToClient(ctrl.PointToScreen(pt));
  26.             else
  27.                 containerPoint = pt;
  28.             // Trigger MouseMove
  29.             this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0,
  30. containerPoint.X, containerPoint.Y, 0));
  31.         }
  32.         else
  33.         {
  34.             // If the target is not a child and MouseLeaves has not been
  35. triggered
  36.             // we must trigger MouseLeave
  37.             if (this._isEntered)
  38.             {
  39.                 // Trigger MouseLeave
  40.                 this.OnMouseLeave();
  41.                 this._isEntered = false;
  42.             }
  43.         }
  44.     }
  45.     return false;
  46. }


Oct 27 '06 #4

P: n/a
Great, it seems to work fine now by filtering on the WM_MOUSELEAVE message
in my MessageFilter.

Thanks a lot

ThunderMusic

"Stoitcho Goutsev (100)" <10*@100.coma écrit dans le message de news:
%2******************@TK2MSFTNGP02.phx.gbl...
Hi,

Here is your problem. You are using MouseMove event which is based on
WM_MOUSEMOVE. This message as any other mouse messages are posted to the
window under the mouse cursor. Because you use message filter you receive
this message if the mouse is over any form (control) that belongs to your
application. However when the mouse moves over window that is not part of
your application we just stop receiving mouse messages and there is no
notification when this happens they just stop. Windows have three
solutions to the problem (at least these are the ones that I can come up
with at the moment): installing window hook WH_MOUSE_LL, using
TrackMouseEvent API and setting mouse capture.

- WH_MOUSE_LL are possible to be implemented in .NET, but it requires
using PInvoke and I think it is the hardest one to implement
- Setint the mouse capture means that the mouse message will be dispatched
to your window even when the mouse cursor is outside of the window. The
problem is that it works only if mouse button has been pressed when the
mouse is inside the window is held down while moving the mouse.
WindowsForms sets the mouse capture when the mouse button is pressed, so
it should work by itself, but if it doesn't work for you that means you
want to register mouse leave not only when the mouse button is down.
- TrackMouseEvent is used internaly by Windows forms and the
functionality is exposed as MouseLeave and MouseHover events. Why don't
use these events instead of implementing your own solution?
The only problem that I see is that you are going to receive mouse leave
even if the mouse is inside the form, but enters some form's child
control. This cases can be filtered our by checking the form hosting tghe
control. The WM_MOUSELEAVE used internally is posted to the message queue,
thus can be handeled with message filter
--
HTH
Stoitcho Goutsev (100)

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:Ot****************@TK2MSFTNGP02.phx.gbl...
>hi, thanks for your answer... unfortunately, it's not exactly what I
need... Activate and Deactivate are triggered when a user clicks on a
form (activated) or click on another form or minimize (Deactivate)...
The behavior I need is when the user move his mouse over the form or out
of the form...

I agree that the MouseEnter and MouseLeave of the form are hierarchical,
that's why I did implement IMessageFilter.... Now the thing is, I must
discover how to receive the message (or event) so the program can tell
when the mouse cursor went out of the form's boundaries.

Thanks

ThunderMusic

"Bryan Phillips" <bp*******@nospam.crowechizek.com.spammenotwrote in
message news:O7****************@TK2MSFTNGP05.phx.gbl...
>>Have you tried using the Activated and Deactivated events instead? See
this from MSDN help:

The Enter and Leave events are suppressed by the Form class. The
equivalent events in the Form class are the Activated and Deactivate
events. The Enter and Leave events are hierarchical and will cascade up
and down the parent chain until the appropriate control is reached. For
example, assume you have a Form with two GroupBox controls, and each
GroupBox control has one TextBox control. When the caret is moved from
one TextBox to the other, the Leave event is raised for the TextBox and
GroupBox, and the Enter event is raised for the other GroupBox and
TextBox.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com


"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl:

Hi,
I'm developping a form that renders differently when hovered and
trigger
some processing when not hovered (leaved). The thing is, when I leave
my
form and another form from my application is underneath, it works fine,
but
if I leave my form and anything that is not from my application is
underneath, the Leave event does not fire until I hover something from
my
application.

Here's my filtered code for the IMessageFilter (I did not pasted the
declarations, I just pasted my message proc) (at the end of the post)

Can someone tell me what's wrong with this code?

thanks

ThunderMusic

Expand|Select|Wrap|Line Numbers
  1. public bool PreFilterMessage(ref Message m)
  2. {
  3.     // Everything is done with WM_MOUSEMOVE
  4.     if (m.Msg == WM_MOUSEMOVE)
  5.     {
  6.         // look if the target is our control or contained by our
  7. control
  8.         Control ctrl = Control.FromHandle(m.HWnd);
  9.         if (ctrl != null && (ctrl == this._container ||
  10. this._container.Contains(ctrl)))
  11.         {
  12.             // if this message WM_MOUSEMOVE is the first we get,
  13.             // we must trigger the MouseEnter event
  14.             if (!this._isEntered)
  15.             {
  16.                 // trigger MouseEnter
  17.                 this.OnMouseEnter();
  18.                 this._isEntered = true;
  19.             }
  20.             // Get point (using target control coordinates)
  21.             Point pt = new Point(m.LParam.ToInt32());
  22.             // convert to controls coordinates if necessary
  23.             Point containerPoint;
  24.             if (ctrl != this._container)
  25.                 containerPoint =
  26. this._container.PointToClient(ctrl.PointToScreen(pt));
  27.             else
  28.                 containerPoint = pt;
  29.             // Trigger MouseMove
  30.             this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0,
  31. containerPoint.X, containerPoint.Y, 0));
  32.         }
  33.         else
  34.         {
  35.             // If the target is not a child and MouseLeaves has not
  36. been
  37. triggered
  38.             // we must trigger MouseLeave
  39.             if (this._isEntered)
  40.             {
  41.                 // Trigger MouseLeave
  42.                 this.OnMouseLeave();
  43.                 this._isEntered = false;
  44.             }
  45.         }
  46.     }
  47.     return false;
  48. }



Oct 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.