在 StatusBar 项目的位置显示 ContextMenuStrip

发布于 2024-07-29 01:52:50 字数 179 浏览 5 评论 0原文

我想在 StatusStrip 中 ToolStripStatusLabel 的位置显示 ContextMenuStrip。 普通控件有 PointToScreen / PointToClient 等,但由于 ToolStripStatusLabel 是从 Component 派生的,所以没有。

任何帮助,将不胜感激。

I want to show a ContextMenuStrip at the location of a ToolStripStatusLabel in a StatusStrip. Ordinary controls have PointToScreen / PointToClient / etc, but as ToolStripStatusLabel is derived from Component it does not.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2024-08-05 01:52:50

一个非常非常晚的答案,只是因为我碰巧遇到了同样的问题并用谷歌搜索了这个问题。 我发现的最佳解决方案为迄今为止的答案添加了一个很好的转折。 如下:

    void toolStripItem_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var label = (ToolStripItem)sender;
            this.contextMenuStrip1.Show(this.mainStatusStrip, label.Bounds.X + e.X, label.Bounds.Y + e.Y);
        } 
    }

将相对于控件的鼠标坐标 (eX, eY) 添加到边界坐标中,使菜单出现在正确的位置。 忽略此选项会显示 ToolStripItem 左上角的菜单。 作为记录。

A very, very late answer, just because I happened to struggle with the same issue and googled up this question. What I found as the best solution adds one nice twist to the answers so far. Here it is:

    void toolStripItem_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var label = (ToolStripItem)sender;
            this.contextMenuStrip1.Show(this.mainStatusStrip, label.Bounds.X + e.X, label.Bounds.Y + e.Y);
        } 
    }

Adding the mouse coordinates relative to the control (e.X, e.Y) to the bounds coordinates makes the menu appear at exactly the right position. Omitting this shows the menu at the top left corner of the ToolStripItem. For the record.

红衣飘飘貌似仙 2024-08-05 01:52:50

你不能做这样的事情吗:

int x = label.Bounds.Location.X + statusStrip.Location.X;
int y = label.Bounds.Location.Y + statusStrip.Location.Y;
menu.Show(this, x, y);

Can't you just do something like this:

int x = label.Bounds.Location.X + statusStrip.Location.X;
int y = label.Bounds.Location.Y + statusStrip.Location.Y;
menu.Show(this, x, y);
书信已泛黄 2024-08-05 01:52:50

查看 Bounds 属性ToolStripStatusLabel。 像这样使用它来完成您需要做的事情:

contextMenuStrip1.Show(statusStrip1, toolStripStatusLabel2.Bounds.X, toolStripStatusLabel2.Bounds.Y);            

Check out the Bounds property of the ToolStripStatusLabel. Use it like this to do what you need to do:

contextMenuStrip1.Show(statusStrip1, toolStripStatusLabel2.Bounds.X, toolStripStatusLabel2.Bounds.Y);            
山有枢 2024-08-05 01:52:50

单击 ToolStripStatusLabel 时,需要执行几个步骤来获取鼠标在窗体上的正确位置。 正如您所提到的,ToolStripStatusLabel 没有 PointToClient 或 PointToScreen 方法,但父 StatusStrip 控件有。

尝试:

private void toolStripStatusLabel_MouseDown(object sender, MouseEventArgs e)
{
    Point p = e.Location;
    p.Offset(toolStripStatusLabel.Bounds.Location);
    myContextMenu.Show(StatusStrip.PointToScreen(p));
}

There are a couple of steps involved in getting the proper location of the mouse on the form when it's clicked on a ToolStripStatusLabel. As you mention the ToolStripStatusLabel does not have PointToClient or PointToScreen methods, but the parent StatusStrip control does.

Try:

private void toolStripStatusLabel_MouseDown(object sender, MouseEventArgs e)
{
    Point p = e.Location;
    p.Offset(toolStripStatusLabel.Bounds.Location);
    myContextMenu.Show(StatusStrip.PointToScreen(p));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文