I have contextmenustrip named "cmsView" in my application. When i right click the mouse button the cmsView has opened and click some where in the application cmsView gets closed. If I opened the cmsView and press Windowslogo +D(i.e, minimize the applications) and again the cmsView is not closed and in opened state whenever i click some in the application and lost it focus.
1 Answers
This is not a bug in your application or in the ContextMenuStrip
control. If anything, it's a bug in Windows with the pop-up menu control, and the WinForms controls are actually mimicking that bug so that they will behave as the user expects.
You can test this out for yourself in a simple application like Notepad. Open a new instance, right-click on the document area, and then press Win+D. The context menu will remain open and visible on the desktop, even though the application's window is gone (minimized).
So I don't recommend trying to "fix" this in your application. When in Rome, do as the Romans do...
If you absolutely had to try and fix it, you might try listening for a minimize event for your form and manually instructing the context menu to close.
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xf020;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == SC_MINIMIZE)
{
// Close the context menu strip when the form is being minimized
cmsView.Close();
}
}
base.WndProc(ref m);
}

- 1
- 1

- 239,200
- 50
- 490
- 574
-
thanks ya.. but is there any way to handle this? that is by closing manually the contextmenustrip – Tanya Aug 02 '11 at 04:50
-
Thanks cody gray.. Got an idea to how to handle it.. thank you – Tanya Aug 02 '11 at 04:55
-
@Tanya, you should mark this as the answer if it answered your question. – Christopher Currens Aug 02 '11 at 05:34
-
`protected override void WndProc(ref Message m) { if (m.Msg == WM_MENUCHAR) { if (m.WParam.ToInt64() == MF_SYSMENU) { if (cmsView!= null) cmsView.Close(); } } base.WndProc(ref m); }` I used the above code and when i press WindowsLogo key nothing happens but the window get minimized. – Tanya Aug 02 '11 at 05:51
-
@Tanya: The problem is you're checking for `MF_SYSMENU`, which isn't the same as your context menu. That's the window or system menu you get by clicking in the title bar of the app, on the icon. Also, unless you're using the `ContextMenu` class (and *not* the `ContextMenuStrip` class), you won't get `WM_MENUCHAR` messages. `ContextMenuStrip` is *not* a wrapper over the Win32 API menus. Those are drawn entirely in .NET code. – Cody Gray - on strike Aug 02 '11 at 05:56
-
oh.. okay.. then is there any way to handle this issue. – Tanya Aug 02 '11 at 06:01
-
Is there a way handle this issue in the ReSize(EventArgs e) or OnPaint event of the winForm – Tanya Aug 02 '11 at 06:06
-
@Tanya: Not in my tests... Although I don't see the context menu again until the application is restored from minimize, which is slightly different behavior from the native API menus in Notepad. I still don't think this problem is worth trying to solve. Every other app is going to behave the same way. Doing it in `OnPaint` is *way* too risky... the form gets repainted for a lot of reasons other than being restored. – Cody Gray - on strike Aug 02 '11 at 06:11
-
Ya.. But I have added a bool variable which is set to true only when the contextmenustrip is opened. and Checked the if condition if the variable is true and the window state is minimised or maximised then it will close the context menu strip. Now its working . – Tanya Aug 02 '11 at 06:33