【问题标题】:How do I draw a border around a ToolStripButton如何在 ToolStripButton 周围绘制边框
【发布时间】:2020-04-09 19:40:13
【问题描述】:

我在 ToolStrip 中有一个 ToolStripButton,我想在它周围画一个边框。这是我正在使用的代码:

    private void tsbtnSearch_Paint(object sender, PaintEventArgs e)
    {
        ToolStripButton btn = (ToolStripButton)sender;

        ControlPaint.DrawBorder(e.Graphics, btn.Bounds,
               Color.Red, 3, ButtonBorderStyle.Outset,
               Color.Red, 3, ButtonBorderStyle.Outset,
               Color.Red, 3, ButtonBorderStyle.Outset,
               Color.Red, 3, ButtonBorderStyle.Outset);

    }

如下图所示正在绘制边框:

我需要怎么做才能得到正确的坐标?

【问题讨论】:

  • 尝试改用btn.ContentRectangle
  • 工作就像一个魅力!

标签: c# winforms toolstrip toolstripbutton


【解决方案1】:

你快到了。 您可以使用DrawBorder 的更简单变体,并使用按钮宽度和高度手动定义边框的矩形:

       ToolStripButton btn = (ToolStripButton)sender;
                ControlPaint.DrawBorder(
                       e.Graphics, 
                       new Rectangle(0, 0, btn.Width, btn.Height),
// or as @LarsTech commented, this works fine too!
//  btn.ContentRectangle,
                       Color.Red,
                       ButtonBorderStyle.Solid);            

在这种情况下,边框的矩形不是按钮的边界(IMO,因为ToolStripButton不是平面按钮,而是一个复杂的对象ToolStripItem,不仅仅是按钮)。

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多