更改 Windows 窗体上的边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2177373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:52:05  来源:igfitidea点击:

Changing a border color on a Windows Form

c#windowswinforms

提问by yeahumok

Does anybody know how to change the border color of a datagridview in a windows form?

有人知道如何在 Windows 窗体中更改 datagridview 的边框颜色吗?

回答by Hans Passant

You can't, it is drawn with the color that the user selected in her preferred theme, selected in Control Panel's Display applet. Overriding the user preference is risky, but you can do so by drawing it yourself. Set the DGV's BorderStyle property to None and draw a border yourself in the form's OnPaintBackground() method. For example:

你不能,它是用用户在她的首选主题中选择的颜色绘制的,在控制面板的显示小程序中选择。覆盖用户偏好是有风险的,但您可以通过自己绘制来实现。将 DGV 的 BorderStyle 属性设置为 None 并在窗体的 OnPaintBackground() 方法中自己绘制边框。例如:

protected override void OnPaintBackground(PaintEventArgs e) {
  base.OnPaintBackground(e);
  Rectangle rc = new Rectangle(dataGridView1.Left - 1, dataGridView1.Top - 1,
    dataGridView1.Size.Width + 1, dataGridView1.Size.Height + 1);
  e.Graphics.DrawRectangle(Pens.Fuchsia, rc);
}