簡體   English   中英

.NET C#-WinForm邊框

[英].NET C# - WinForm border

我有一個沒有邊框的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
     );

我需要一個無邊界的表單,但是我想添加一個1px的邊界。

在窗體的Paint事件處理程序中,添加以下代碼:

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

祝好運!

您可以添加一個完全停靠的Panel ,以及另一個完全停靠的Panel作為子控件。 設定外的填充Panel 1和外部的背景色Panel為黑色。

然后將內部Panel的背景色設置為SystemColors.Control

如果你不想畫畫

將4個寬度或高度2或4的面板和黑色背景色分別固定在頂部,右側,底部和左側的4個側面后,分別添加

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

您還可以更改其鼠標指針以調整圖標的大小,也可以調整表單的大小,以編寫有關鼠標事件的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM