简体   繁体   中英

.NET C# - WinForm border

I have a WinForm without a border (borderless). How can I add a 1px black border to the form?

    public MainForm()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 10, 10)); // adjust these parameters to get the lookyou want.
    }


    [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
     );

I need a borderless form but I want to add a 1px border.

In the Paint event handler of the form, add this code:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0, Width - 1, Height - 1));
}

Good luck!

You could add a fully docked Panel , and another fully docked Panel as a child control. Set the padding of the outer Panel to 1 and the background color of the outer Panel to black.

Then set the background color of the inner Panel to SystemColors.Control .

If you don't want to paint,

Add 4 panels width or height 2 or 4 and black background colour after dock them in 4 sides differently on top, right, bottom, left respectively

        this.FormBorderStyle = FormBorderStyle.None;

        Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.Green };
        this.Controls.Add(pnlTop);

        Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.Green };
        this.Controls.Add(pnlRight);

        Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.Green };
        this.Controls.Add(pnlBottom);

        Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.Green };
        this.Controls.Add(pnlLeft);

You can also change their mouse pointer to resize icons also you can resize the form writing some code on mouse events.

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