【问题标题】:Show a ContextMenuStrip without it showing in the taskbar显示 ContextMenuStrip 而不在任务栏中显示
【发布时间】:2008-09-25 11:01:08
【问题描述】:

我发现当我为 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);

    }
}

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

【问题讨论】:

  • 同上,我遇到了同样的问题/错误。

标签: .net c++ winforms


【解决方案1】:

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

【讨论】:

  • 啊!你真是个天才!我不知道他们有这方面的财产!我再次以“艰难”的方式做事:)
  • 很多其他控件也有这个属性。
  • 我认为这也会为我解决这个问题,但我对这一切有点陌生。有人可以解释我如何将菜单“分配”给 ContextMenuStrip 吗?我认为一旦我看到它就很明显了,但现在我没有任何线索:)
  • 没关系,我知道怎么做。对于遇到此问题的其他人:msdn.microsoft.com/en-us/library/…
【解决方案2】:

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

{
  UnsafeNativeMethods.SetForegroundWindow(new HandleRef(notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle));
  notifyIcon.ContextMenuStrip.Show(Cursor.Position);
}

其中 UnsafeNativeMethods.SetForegroundWindow 是:

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

【讨论】:

  • 谢谢!我试图为鼠标左键和右键显示不同的 ContextMenuStrip,但是没有 SetForegroundWindow,ContextMenuStrip 在任务栏中创建了一个丑陋的图标,并且 ContextMenuStrip 的焦点不能很好地工作。
【解决方案3】:

假设您有 2 个上下文菜单项:ContextMenuLeftContextMenuRight。默认情况下,您已经从 NotifyIcon 属性中分配了其中一个。在调用Left Button Click 之前,只需更改它们,显示上下文菜单,然后再次更改它们。

NotifyIcon.ContextMenuStrip = ContextMenuLeft; //let's asign the other one
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(NotifyIcon, null);
NotifyIcon.ContextMenuStrip = ContextMenuRight; //switch back to the default one

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    我的问题是我的菜单可以通过双击通知图标获得。

    右键单击通知图标时,没有任务栏按钮,但是当我手动 Show(Cursor.Position) 时,它会显示一个任务栏按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多