[英]How to change the form border color c#?
Override it with:覆盖它:
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.[your_color], ButtonBorderStyle.Solid);
}
Unfortunately, since the form border is drawn by the Operating System, this is a complicated task .不幸的是,由于表单边框是由操作系统绘制的,因此这是一项复杂的任务。 There is no real way around that.
没有真正的解决办法。
Do NOT click the ProjectDistributor link on the CodePlex page below
请勿单击下方 CodePlex 页面上的 ProjectDistributor 链接
The CodePlex Project for Drawing Custom Borders makes this very easy, though.不过,用于绘制自定义边框的CodePlex 项目使这变得非常容易。 Just build the form using SkinnedForm from that project instead of a standard Form, and it should work - you really don't need to do anything different in your code.
只需使用该项目中的 SkinnedForm 而不是标准表单来构建表单,它应该可以工作 - 您真的不需要在代码中做任何不同的事情。
FormBorderStyle
to None
.FormBorderStyle
设置为None
。 Now, the panel serves as the main container and you can change the background as you want and the form serves as the border.现在,面板充当主容器,您可以根据需要更改背景,表单充当边框。
This works for me in Windows 10 and 11:这在 Windows 10 和 11 中适用于我:
private string ToBgr(Color c) => $"{c.B:X2}{c.G:X2}{c.R:X2}";
[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
const int DWWMA_CAPTION_COLOR = 35;
public void SetTitleBarColor(Color color, IntPtr handle)
{
IntPtr hWnd = handle;
int[] colorstr = new int[] { int.Parse(ToBgr(color), System.Globalization.NumberStyles.HexNumber) };
DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, colorstr, 4);
}
Like previously mentioned, changing the actual color of the border is difficult.如前所述,更改边框的实际颜色很困难。 The solution above with the panel has limitations like you can't resize the form.
上面的面板解决方案有一些限制,比如你不能调整表单的大小。 I found a reasonably easy trick without a lot of the other limitations.
我发现了一个相当简单的技巧,没有很多其他限制。
It looks like a border, it will resize with the window, and you can drop anything else into the form you want.它看起来像一个边框,它会随着窗口调整大小,您可以将任何其他内容放入您想要的表单中。 The limitation is, you must do this as the very first thing you add to the form.
限制是,您必须将此作为添加到表单的第一件事。
在“ForeColor”下方,它们应该是一个名为“FormBorderStyle”的设置,您可以在 VisualStudio 2015 中使用该设置进行编辑。或者您可以进入控制面板路径应该是这样的“控制面板\\外观和个性化\\个性化”,它们将是一个称为“颜色”的第二个设置可以将其更改为您想要的颜色,它将所有程序中的边界颜色更改为您设置的颜色。
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
string color = Convert.ToString(colorDialog1.Color);
MessageBox.Show("You change the color " + color);
this.BackColor = colorDialog1.Color; // BackColor is only accessible for this form
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.