Change border color windows forms
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.
Answered by:
Question
I am trying to change the Windows Form’s border color to Red — I know it is not as easy as just changing the color property but I am not seeing anyone implemented solution for this. Can someone from Microsoft help us getting this implemented? The end result should look something like below image.
I hope this is doable 🙂
- Moved by Amy Peng Microsoft employee Wednesday, August 5, 2015 2:27 AM
Answers
Hi Chinetan — Strings,
As you want to draw the border line out of system’s border, I think it is complex if you want to redraw the system’s border.
I suggest you could hide the system’s border by setting the form’s FormBorderStyle property to None, the system border will not show. You need to draw the border with GUI.
Then use a panel or others container to replace the border with the icon. then you could add some buttons to replace the «Red X»,»Minimize» and «Maximize». set the backcolor of container to the color you want.
Regards,
Youjun Tang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Change border color and appearance for WinForms control
When the user will hover the label, it will add a top-border and bottom-border to the label.
Something similar to this: http://ianlunn.github.io/Hover/ -> Border Transitions -> Underline From Center.
However, I only know how to define the normal border. I can’t even change the color of the border, his width and more.
This is what I achieved so far:
designer.cs:
cs:
1 Answer 1
At first I suggest you to use the label1_MouseEnter() event instead of the label1_MouseHover() event. Since the hover event constantly fires while you are hovering label1 . Thats unnecessary because you will change the looking of label1 just when you enter the label and change it back to the default one, once you leave it. So MouseEnter() is your preferred choice.
We will define a bool variable to check if a surroinding border shell be drawn around label1 . We change it’s value in the MouseEnter() and MouseLeave() event.
In those events we will call label1.Refresh() , which redraws the control, so it’s Paint() event will be fired.
Now we validate if a new special border shell be drawn or not. If so we use the ControlPaint class with it’s DrawBorder() method to draw a custom border of the size of label1 ‘s Rectangle . You can modify this border as you wish to. If we don’t want to draw this special border, we draw a default border. What simply has the color of the Form ‘s Backcolor, so it seems like there isn’t any border around label1 .
In the method parameters for ControlPaint.DrawBorder() be aware of the 0 instead of the 2 . The parameters of ControlPaint.DrawBorder() for the rectangles width are as followed:
- int leftWidth
- int topWidth
- int rightWidth
- int bottowmWidth
That’s why I set as the first and third the value 0 so no border will be drawn on the right and left side. You can adjust that according to your needs.
How to change the form border color c#?
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?
6 Answers 6
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.
Override it with:
Workaround — Just follow these steps:
- Set FormBorderStyle to 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
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
- Add 4 panels
- 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.
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.