Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:27:55+00:00 2026-05-26T17:27:55+00:00

According to this answer on another question, the Collapsed event of a ContextMenu is

  • 0

According to this answer on another question, the Collapsed event of a ContextMenu is only raised if it’s associated to a control before calling Show().

SInce a NotifyIcon does not count as a control, I can’t hook onto the Collapsed event to detect when the menu associated to one is hidden.

Are there any workarounds?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:27:55+00:00Added an answer on May 26, 2026 at 5:27 pm

    Under the “Remarks” section for the MSDN docs on TrackPopupMenuEx, it says:

    To display a context menu for a notification icon, the current window
    must be the foreground window before the application calls
    TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not
    disappear when the user clicks outside of the menu or the window that
    created the menu (if it is visible). If the current window is a child
    window, you must set the (top-level) parent window as the foreground
    window.

    So that could mean that when the ContextMenu is visible, the window on the NotifyIcon will be the foreground window. You can see from by looking at NotifyIcon.ShowContextMenu() that it is indeed the case:

        private void ShowContextMenu()
        {
            if (this.contextMenu != null || this.contextMenuStrip != null)
            {
                NativeMethods.POINT pOINT = new NativeMethods.POINT();
                UnsafeNativeMethods.GetCursorPos(pOINT);
                UnsafeNativeMethods.SetForegroundWindow(new HandleRef(this.window, this.window.Handle));
                if (this.contextMenu != null)
                {
                    this.contextMenu.OnPopup(EventArgs.Empty);
                    SafeNativeMethods.TrackPopupMenuEx(new HandleRef(this.contextMenu, this.contextMenu.Handle), 72, pOINT.x, pOINT.y, new HandleRef(this.window, this.window.Handle), null);
                    UnsafeNativeMethods.PostMessage(new HandleRef(this.window, this.window.Handle), 0, IntPtr.Zero, IntPtr.Zero);
                    return;
                }
                if (this.contextMenuStrip != null)
                {
                    this.contextMenuStrip.ShowInTaskbar(pOINT.x, pOINT.y);
                }
            }
        }
    

    Using ILSpy I then noticed NotifyIcon has a private member window, which refers to a private class with base type NativeWindow. Therefore, you can check like this:

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetForegroundWindow();
    
    ...
    
    FieldInfo notifyIconNativeWindowInfo = typeof(NotifyIcon).GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
    NativeWindow notifyIconNativeWindow = (NativeWindow)notifyIconNativeWindowInfo.GetValue(notifyIcon1);
    
    bool visible = notifyIcon1.Handle == GetForegroundWindow();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another way to ask this question is: what is Inversion of Control according to
According to this answer , I can make C++ mode think that underscores are
According to this question it seems like you can do this for Methods. What
This might be a noob question, but can't find an answer anywhere. I have
another database structuring question, hope you guys dont mind :D i am having this
This question is similar to another one, concerning pyCURL I have the following error
According to this question (and many many google search results), when we're in a
According to the answer on this post it states: Did you know that ReadUncommitted
This is by design, according to the answers in this SO question But I
Ok, I know the answer for this question may not be objective but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.