简体   繁体   中英

Adding a border color on a form in visual C#

Ok, i have made a form with this code: this.FormBorderStyle = FormBorderStyle.None; Ok, i have also added a border radius with this code:

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
     );

    public Form4()
    {
        InitializeComponent();
        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
    }

So, what i need is the add a small black border around the form that curves with the border-radius. How do i do that?

Ok, i added this, it works, but it does not go with the border, it just goes strait: e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0, Width - 1, Height - 1)); and this:

ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);

Override the form's OnPaintBackground() method and simply draw the border with the Graphics methods, using the passed e.Graphics object.

Note that you don't have to pinvoke when you use the Region(GraphicsPath) constructor. That same GraphicsPath will also be handy to draw the border.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM