.NET C#-WinForm边框

Ste*_*Ste 1 .net c# visual-studio winforms

我有一个没有边框的WinForm(无边界)。如何在表单中添加1px黑色边框?

    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
     );
Run Code Online (Sandbox Code Playgroud)

我需要一个无边界的表单,但是我想添加一个1px的边界。

Hom*_*mam 5

Paint表单的事件处理程序中,添加以下代码:

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

祝好运!

  • 是的,但是改写OnPaint()。 (2认同)
  • @Stecya:问题出在哪里?,在表单中添加`1 px` Padding。会没事的。 (2认同)