Re: Form level OnMouseLeave by JezB
JezB
Tue Oct 18 08:04:34 CDT 2005
Correction: (to give a little leeway to the bounds check, moving the mouse
off slowly did not work with the previous version)
private void genericMouseLeave(object sender, EventArgs e)
{
Rectangle innerBounds = new Rectangle(this.Bounds.X + 2, this.Bounds.Y +
2, this.Width - 4, this.Height - 4);
if (!innerBounds.Contains(Cursor.Position)) this.Close();
}
"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:uxSIP890FHA.4032@TK2MSFTNGP15.phx.gbl...
> Solution appears to be along the lines you suggested, but to attach the
> same MouseLeave event to all controls on the form as well as the form
> itself. This can be done by a call to a recursive routine at the end of
> the form's constructor:
>
> public Form1()
> {
> InitializeComponent();
> AttachMouseLeaveEvents(this);
> }
> private void AttachMouseLeaveEvents(Control c)
> {
> c.MouseLeave += new EventHandler(genericMouseLeave);
> if (c.HasChildren)
> foreach (Control x in c.Controls)
> AttachMouseLeaveEvents(x);
> }
> private void genericMouseLeave(object sender, EventArgs e)
> {
> if (!this.Bounds.Contains(Cursor.Position))
> this.Close();
> }
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:Oh4Hhj80FHA.700@TK2MSFTNGP10.phx.gbl...
>>I was already thinking along similar lines Nelson, thanks.
>>
>> It will work in that it will ignore the times the MouseLeave (or
>> OnMouseLeave) fires on leaving the form's surface to go over another
>> control within the form.
>>
>> But, and this is a big BUT, I have just found that, if the default
>> position of the form at startup is such that the mouse is already over a
>> control, and that control is next to the form's edge, moving the mouse
>> off the form without leaving that control does not fire the form's
>> MouseLeave event at all ! The most obvious case is when the form contains
>> a docked panel that fills the form's surface.
>>
>> I think we have to think of a more generic solution. Or someone does !
>> Anybody ??
>>
>> "Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
>> news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
>>> hello
>>> Try this
>>>
>>> Private Sub Form_MouseLeave(ByVal sender As Object, ByVal e As
>>> System.EventArgs) Handles MyBase.MouseLeave, UltraGrid1.MouseLeave
>>>
>>> Dim p As Point = Me.MousePosition
>>>
>>> p = PointToClient(p)
>>>
>>> If p.X > DisplayRectangle.Width OrElse p.Y > DisplayRectangle.Height
>>> OrElse p.X < 0 OrElse p.Y < 0 Then
>>>
>>> Me.Close()
>>>
>>> End If
>>>
>>> End Sub
>>>
>>>
>>>
>>> the code is in VB but i think you undertand it
>>>
>>>
>>>
>>> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
>>> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>>>> I'm overriding OnMouseLeave at Form level:
>>>>
>>>> protected override void OnMouseLeave(EventArgs e)
>>>> {
>>>> this.Close();
>>>> }
>>>>
>>>> The purpose of this, as you may suspect, is to close the form when the
>>>> user moves the mouse off it. However, this seems to fire whenever the
>>>> mouse moves over any control on the form, I imagine because the mouse
>>>> is "leaving" the form's base surface.
>>>>
>>>> How can I do what I'm trying to do ?
>>>>
>>>
>>>
>>
>>
>
>