I have written a flashy custom context menu using a new form with layeredwindows for my application, I want the context menu form to close when the mouse leaves the form, or when its deactivated. But I can't seem to get it to happen. I have tried the Close() method in the deactivated event handler, in the Mouse leave event, in the form leave event. I'm just not sure how to go about it.

i tried to create a nativewindow class that would watch for the capturechanged message and close the form, but the form never receives that message.

Any pointers or workarounds?

I created a timer in the deactivated event that calls close and it works, but i have to use a boolean to find out if its the first time its called because it calls it as soon as its created for some reason.

also, I have one of the buttons on the context menu form call a method on the main form that creates a new dialog window, and for some reason it opens and closes immediately for what appears to be no reason.

the function is a
mydialogwindw mdw = new mydialogwindow();
if(mdw.ShowDialog() == DialogResults.OK)
{

}

but the window closes imediatly after opening...

breakpoints aren't helping, because I can't seem to keep extra deactivated events from firing when the break happens.

Ideas anyone?

I created a form with 2 buttons on it showing a message when clicked.
Implemented a mouseleave like this:

private void Form1_MouseLeave(object sender, EventArgs e)
    {
        this.button1.Dispose();
    }

Button1 is gone when the mouse leaves the form, but my form is not deactivated I can still click the second button. Can you reach the menuform from within your "main" form?

that does in fact work, not yet raising the question about how mousing over controls calls the leave event and checking if it containsFocus will keep it displayed until you focus somthing else then reenter and leave the child form

That works great on a new project containing just 2 forms, one as a main and the other as a faux contex menu. but when using layerd windows, this doesn't seem to work at all. I am going to try later to see if I added a mouseLeave event to the layered window form as well as the control form under it as well might do something, but as its all very complicated Its unclear how it will react.

I will post back when I have something, Thank's for the help. I didn't think about testing it in a new project to see if it was a conflict with something else I did, but of course it is. hopefully I can get this one resolved.

the control housing form of my context menu's mouse leave event was never being fired, still isn't. but the layered window overlay form for it's mouse leave event is being fired. I am using a heavily modified version of the codeproject example of the autoformtranformer control, It has been heavily worked over by me to make it fit everything I need it for, but essentially it has a control that drives from a panel, you set it to fill, then you create an image with an alpha layer, where you want your nice transparent edges on the outside and then you cut out a hole in the middle for your controls, set that image to the background and the control makes that image a new layered window above your form locking them together to move at the same time, and recreates the region of the main form to fit in the hole. but then the leave event is fired when you enter the hole, which is were the controls are held so I imported the getCursorPos user32 method and with some an ifstatment loaded with pointtoscreen conversions on some points. I come up keep track of if the mouse was inside the hole ore outside the form

//Create point to hold mouse position
        Point defPnt = new Point();
        //get cursor position
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(ref Point lpPoint);
        void m_lwin_MouseLeave(object sender, EventArgs e)
        {
            GetCursorPos(ref defPnt);

            Point P = new Point(2,2);
            Point PW = new Point(98, 0);//width of form minus 2
            Point PH = new Point(0,259);//height of form - 2
          
            if (this.PointToScreen(P).X > defPnt.X || this.PointToScreen(PW).X < defPnt.X || this.PointToScreen(P).Y > defPnt.Y || this.PointToScreen(PH).Y < defPnt.Y)
            {
                ParentForm.Close();
            }
        }

thanks ddanbe, you really helped me realize how to get to the root of this problem. Creating a new project clear of bugs and bug causes helps weed out the issues.

Glad to be of help! I often use this techique of making the problem as small as possible. It doesn't always work, but can be a great aid sometimes.
Btw. GetCursorPos(ref defPnt); works but you could also use this static property: defPnt = Cursor.Position;

This question has already been answered. Start a new discussion instead.