0

我在 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);

    }

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

工具条按钮

我需要做什么才能获得正确的坐标?

4

1 回答 1

2

你快到了。您可以使用更简单的变体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,它不仅仅是按钮)。

在此处输入图像描述

于 2020-04-09T19:51:55.137 回答