您的位置:宽带测速网 > 网站建设 > winform窗体边框颜色怎么设置

winform窗体边框颜色怎么设置

2025-06-23 21:42来源:互联网 [ ]

要设置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窗体的边框颜色。