-2

I want to add a different bordercolor for every side of the element, yet it's working pretty okay, the only problem I got is that my right side and bottom side of the border isn't visible.

        public static void DrawBorder
    (
        Graphics graphics, Rectangle bounds,
        Color leftColor, int leftWidth, ButtonBorderStyle leftStyle,
        Color topColor, int topWidth, ButtonBorderStyle topStyle,
        Color rightColor, int rightWidth, ButtonBorderStyle rightStyle,
        Color bottomColor, int bottomWidth, ButtonBorderStyle bottomStyle
    ){}
    Color leftColor = Color.FromArgb(65,0,0,0), rightColor = Color.FromArgb(150, 0, 0, 0), topColor = Color.FromArgb(65, 0, 0, 0), bottomColor = Color.FromArgb(150, 0, 0, 0);
    int leftWidth = 3, rightWidth = 3, topWidth = 3, bottomWidth = 3;
    ButtonBorderStyle leftStyle = ButtonBorderStyle.Solid, rightStyle = ButtonBorderStyle.Solid, topStyle = ButtonBorderStyle.Solid, bottomStyle = ButtonBorderStyle.Solid;

    private void Paint_(object sender, PaintEventArgs e)
    {
        Rectangle borderRectangle = this.ClientRectangle;
        borderRectangle.Inflate(0, 0);
        ControlPaint.DrawBorder(e.Graphics, borderRectangle,
        leftColor, leftWidth, leftStyle,
        topColor, topWidth, topStyle,
        rightColor, rightWidth, rightStyle,
        bottomColor, bottomWidth, bottomStyle);
    }

I'm using the "Paint_" function for every element you see here, so it's the same problem on every of these. Missing right and bottom border I'm pretty new to any of the "Draw" stuff, so I don't know anything which could be the problem.

specktator
  • 29
  • 4
  • 3
    Pens draw a rectangle bigger by one 1px on the bottom and right side, adjust for that. – Ray Mar 17 '18 at 20:46
  • I don't really understand what you mean, how I said I'm new to this stuff, you could say I even know nothing of this. – specktator Mar 17 '18 at 21:03
  • He meant what he wrote. You can call it a bug or a quirk or a Pen property.. See [MSDN](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-use-a-pen-to-draw-rectangles) for details..! – TaW Mar 17 '18 at 21:19

1 Answers1

1

The Draw... calls accepting a Pen instance seem (by default) to draw a rectangle bigger than 1 pixel to the right and bottom. So if you call something like DrawRectangle(Pens.Black, 0, 0, 100, 100), the rectangle on screen will look like as if it spans the coordinates 0, 0 to 101, 101.

The reasoning behind this is described here: Pixel behaviour of FillRectangle and DrawRectangle

To quickly fix your drawing, just pass coordinates smaller by 1 pixel to the right or bottom. E.g., to fix the above example, you'd call DrawRectangle(0, 0, 99, 99) to receive a rectangle spanning the coordinates 0, 0 to 100, 100 on the screen.

I personally resigned from using Pens and Draw calls for bordered rectangles and always use two FillRectangle calls (using Brush instances) as follows:

// Draws a white rectangle with black border
Rectangle rect = new Rectangle(0, 0, 100, 100);
gr.FillRectangle(Brushes.Black, rect); // Draw border
rect.Inflate(-1, -1); // Decrease the size of the rectangle by 1 pixel on all sides
gr.FillRectangle(Brushes.White, rect); // Draw background above it

I have read that FillRectangle is faster than DrawRectangle (as it can simply bitblit the area, unlike Draw which requires to draw for separate lines with all possible shenanigans internally).

Ray
  • 7,940
  • 7
  • 58
  • 90