简体   繁体   English

如何更改表格边框颜色 c#?

[英]How to change the form border color c#?

I would like to change window form border color (the border with the form title).我想更改 window 表单边框颜色(带有表单标题的边框)。 The example I found in codeplex is too much and confusing.我在codeplex中找到的示例太多且令人困惑。 Can any help me on something simpler?任何人都可以帮助我做一些更简单的事情吗?

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 而不是标准表单来构建表单,它应该可以工作 - 您真的不需要在代码中做任何不同的事情。

Workaround - Just follow these steps:解决方法 - 只需按照以下步骤操作:

  • Set FormBorderStyle to None .FormBorderStyle设置为None
  • Cover the form with a panel and leave some space for border.用面板覆盖表单并为边框留出一些空间。
  • Set the color you want for the border as the form back color.将您想要的边框颜色设置为表单背景颜色。

Now, the panel serves as the main container and you can change the background as you want and the form serves as the border.现在,面板充当主容器,您可以根据需要更改背景,表单充当边框。

The Final Result最终结果

在此处输入图片说明

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.我发现了一个相当简单的技巧,没有很多其他限制。

  • Create a form创建表格
  • Set FormBorderStyle to None将 FormBorderStyle 设置为无
  • Add 4 panels添加4个面板
  • Set the background color of the panels to the color of the border you want将面板的背景颜色设置为您想要的边框颜色
  • Anchor one each to the top, bottom, left, and right在顶部、底部、左侧和右侧各锚一个
  • Set the height(top/bottom) or width (left/right) to the thickness of the border you want.将高度(顶部/底部)或宽度(左/右)设置为所需边框的厚度。 2 or 3 looks really good. 2 或 3 看起来非常好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM