Reputation: 389
I would like to change window form border color (the border with the form title). The example I found in codeplex is too much and confusing. Can any help me on something simpler?
Upvotes: 17
Views: 102166
Reputation: 2285
FormBorderStyle
to 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.
For dragging the window around see this post: Make a borderless form movable?
Upvotes: 11
Reputation: 33
This works for me in Windows 10 and 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;
const int DWWMA_BORDER_COLOR = 34;
const int DWMWA_TEXT_COLOR = 36;
public void CustomWindow(Color captionColor, Color fontColor, Color borderColor, IntPtr handle)
{
IntPtr hWnd = handle;
//Change caption color
int[] caption = new int[] { int.Parse(ToBgr(captionColor), System.Globalization.NumberStyles.HexNumber) };
DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, caption, 4);
//Change font color
int[] font = new int[] { int.Parse(ToBgr(fontColor), System.Globalization.NumberStyles.HexNumber) };
DwmSetWindowAttribute(hWnd, DWMWA_TEXT_COLOR, font, 4);
//Change border color
int[] border = new int[] { int.Parse(ToBgr(borderColor), System.Globalization.NumberStyles.HexNumber) };
DwmSetWindowAttribute(hWnd, DWWMA_BORDER_COLOR, border, 4);
}
Example:
CustomWindow(Color.Red, Color.Blue, Color.Green, Handle);
Upvotes: 1
Reputation: 9
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.
Upvotes: -1
Reputation: 1003
Override it with:
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.[your_color], ButtonBorderStyle.Solid);
}
Upvotes: 21
Reputation: 1
Below "ForeColor" their should be a setting called "FormBorderStyle" You can edit it with that in VisualStudio 2015. Or you can go in control panel path should be something like this "Control Panel\Appearance and Personalization\Personalization" their will be a second setting called "Color" can change that to be what color you want it will change the color of the boarder in all of the programs to the color you set.
Upvotes: -2
Reputation: 564891
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
The CodePlex Project for Drawing Custom Borders makes this very easy, though. 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.
Upvotes: 11
Reputation: 5
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
}
Upvotes: -8