显示ContextMenuStrip而不显示在任务栏中

Cet*_*tra 7 .net c++ winforms

我发现当我为contextmenustrip(右键菜单)执行show()方法时,如果位置超出它所属的表单的位置,它也会显示在任务栏上.

有人在点击通知图标时尝试创建一个右键菜单,但由于菜单悬停在系统托盘上方而不是在窗体内(因为右键单击时窗体可以最小化),它会显示在任务栏上奇怪的原因

这是我目前的代码:

private: System::Void notifyIcon1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {

if(e->Button == System::Windows::Forms::MouseButtons::Right) {

        this->sysTrayMenu->Show(Cursor->Position);

    }
}
Run Code Online (Sandbox Code Playgroud)

我需要设置哪些其他选项,以便它不会在任务栏上显示空白进程.

Gro*_*kys 7

尝试将菜单分配给NotifyIcon的ContextMenuStrip属性,而不是在鼠标单击处理程序中显示它.


小智 6

没有反思的最好和正确的方法是:

{
  UnsafeNativeMethods.SetForegroundWindow(new HandleRef(notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle));
  notifyIcon.ContextMenuStrip.Show(Cursor.Position);
}
Run Code Online (Sandbox Code Playgroud)

其中UnsafeNativeMethods.SetForegroundWindow是:

public static class UnsafeNativeMethods
{
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern bool SetForegroundWindow(HandleRef hWnd);
}
Run Code Online (Sandbox Code Playgroud)