开发者

Event bubbling - Mousehover event for child controls

开发者 https://www.devze.com 2023-02-24 21:11 出处:网络
I have a winform with some controls on it. While the cursor is enters the form (form boundaries,this includes form\'s content) I want to trigger specific task.

I have a winform with some controls on it.

While the cursor is enters the form (form boundaries, this includes form's content) I want to trigger specific task. When cursor leavs the form, i want to trigger another task.

Obviously seeting these events on form won't work: (because, for instance MouseLeave will bw triggered when i move from form itself to other control)

this.MouseEnter += (send开发者_C百科er, e) =>
{
   //do stuff
};

this.MouseLeave += (sender, e) =>
{
   //do stuff
};

I laso tried IMessageFilter interface as appears in similar questions, but none gives me the needed result! The problem is to detect when mouse leave the form completely.

 public bool PreFilterMessage(ref Message m)
 {
    switch (m.Msg)
    {
        case WM_MOUSEMOVE: //or other messages
        bool z = myForm.Bounds.Contains(Cursor.Position); //This didn't help
    }
    eturn false;
}

There is also problem with detection within the form content (when i move between controls).

What am I missing?

update: i don't want to use timers!


You could get the current x,y coordinates of the pointer and see if they are within the form. If they aren't, then perform the off-form task.


I pulled a trick like this once to keep tabs on all forms' ancestors:

Forms have a ControlAdded event. You could hook yourself into that (probably before InitializeForm. Inside that event handler can hook yourself into the ControlAdded and MouseXXX events of all children and grandchildren. You could send all the events of the same type to the same handler.

With that methodology, I suppose that the easiest solution will then be to watch the MouseLeave and MouseEnter events of all ancestors and then check the mouse cursor position to see if it's in the form's screen rectangle (your event handlers will be mashed full of events firing off as your mouse moves around the form). Anyway, then you'll have it solved with zero timers!

0

精彩评论

暂无评论...
验证码 换一张
取 消