Exemplo n.º 1
0
        /// <include file='doc\TrayIcon.uex' path='docs/doc[@for="NotifyIcon.ShowContextMenu"]/*' />
        /// <devdoc>
        ///     Shows the context menu for the tray icon.
        /// </devdoc>
        /// <internalonly/>
        private void ShowContextMenu()
        {
            if (contextMenu != null)
            {
                NativeMethods.POINT pt = new NativeMethods.POINT();
                UnsafeNativeMethods.GetCursorPos(pt);

                // VS7 #38994
                // The solution to this problem was found in MSDN Article ID: Q135788.
                // Summary: the current window must be made the foreground window
                // before calling TrackPopupMenuEx, and a task switch must be
                // forced after the call.

                UnsafeNativeMethods.SetForegroundWindow(new HandleRef(window, window.Handle));

                contextMenu.OnPopup(EventArgs.Empty);

                SafeNativeMethods.TrackPopupMenuEx(new HandleRef(contextMenu, contextMenu.Handle),
                                                   NativeMethods.TPM_VERTICAL,
                                                   pt.x,
                                                   pt.y,
                                                   new HandleRef(window, window.Handle),
                                                   null);

                // Force task switch (see above)
                UnsafeNativeMethods.PostMessage(new HandleRef(window, window.Handle), NativeMethods.WM_NULL, IntPtr.Zero, IntPtr.Zero);
            }
        }
Exemplo n.º 2
0
        /// <include file='doc\TrayIcon.uex' path='docs/doc[@for="NotifyIcon.ShowContextMenu"]/*' />
        /// <devdoc>
        ///     Shows the context menu for the tray icon.
        /// </devdoc>
        /// <internalonly/>
        private void ShowContextMenu()
        {
            if (contextMenu != null || contextMenuStrip != null)
            {
                NativeMethods.POINT pt = new NativeMethods.POINT();
                UnsafeNativeMethods.GetCursorPos(pt);

                // Summary: the current window must be made the foreground window
                // before calling TrackPopupMenuEx, and a task switch must be
                // forced after the call.
                UnsafeNativeMethods.SetForegroundWindow(new HandleRef(window, window.Handle));

                if (contextMenu != null)
                {
                    contextMenu.OnPopup(EventArgs.Empty);

                    SafeNativeMethods.TrackPopupMenuEx(new HandleRef(contextMenu, contextMenu.Handle),
                                                       NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTALIGN,
                                                       pt.x,
                                                       pt.y,
                                                       new HandleRef(window, window.Handle),
                                                       null);

                    // Force task switch (see above)
                    UnsafeNativeMethods.PostMessage(new HandleRef(window, window.Handle), NativeMethods.WM_NULL, IntPtr.Zero, IntPtr.Zero);
                }
                else if (contextMenuStrip != null)
                {
                    // this will set the context menu strip to be toplevel
                    // and will allow us to overlap the system tray
                    contextMenuStrip.ShowInTaskbar(pt.x, pt.y);
                }
            }
        }