温馨提示×

winform窗体边框颜色怎么设置

小亿
310
2024-02-26 10:44:50
栏目: 智能运维

要设置Winform窗体的边框颜色,可以通过自定义样式或者绘制边框来实现。以下是两种方法:

1、自定义样式:可以通过Winform的FormBorderStyle属性来设置窗体的边框样式。在FormBorderStyle属性中选择None,然后在窗体的Paint事件中绘制自定义的边框颜色。具体代码如下:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
}

2、绘制边框:可以通过重写窗体的WndProc方法,在WM_NCPAINT消息中绘制窗体的边框颜色。具体代码如下:

protected override void WndProc(ref Message m)
{
    const int WM_NCPAINT = 0x85;
    base.WndProc(ref m);
    if (m.Msg == WM_NCPAINT)
    {
        IntPtr hdc = GetWindowDC(m.HWnd);
        if ((int)hdc != 0)
        {
            using (Graphics g = Graphics.FromHdc(hdc))
            {
                Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
                g.DrawRectangle(new Pen(Color.Red, 2), rect);
            }
            ReleaseDC(m.HWnd, hdc);
        }
    }
}

通过以上两种方法,可以自定义设置Winform窗体的边框颜色。

0