Is there a way in which I can change the button color and preserve the Windows VisualStyle? I want to create a button that looks like checkBox2 (color and style)
this.button2.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.button2.UseVisualStyleBackColor = false; //if true, BackColor gets ignored
No, not really. This kind of button background is drawn by VisualStyleRenderer.DrawBackground(). Which in turns pinvokes DrawThemeBackground(). These methods don't take a color. None is needed because the color is already specified in the theme.
Simulating the appearance with a LinearGradientBrush is your only real hope. Note that custom drawing a button is quite difficult, all the code is internal and no owner-draw is provided.
Consider using an image.
Related
I'm just messing about with the Visual Styles offered by .NET using Winforms and I have a problem. I am simply executing this code, called from the OnPaint method of a custom Panel, nothing special.
private void DrawBox(PaintEventArgs e)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics, e.ClipRectangle, ComboBoxState.Normal);}
My problem is that the Background Color of the Button is grey while the Background Color of a true ComboBox is white (on my Computer anyway).
Two questions here, why the Color change and how do I make my Button render using the same Background Color as a true ComboBox?
Thanks
Danny
ClipRectangle is not what you want. Set the actual rectangle you want to draw. That being said, yeah, using VisualStyles is going to make you mad. This will get you close:
VisualStyleRenderer vsr = new VisualStyleRenderer("EDIT", 1, 1);
vsr.DrawBackground(e.Graphics, controlRectangle);
vsr.SetParameters("COMBOBOX", 7, 1);
vsr.DrawBackground(e.Graphics, arrowRectangle);
I'm writing a custom Button control as part of a (soon to be) free Control suite, and I would like to base my (default) Control colors on the corresponding Windows System colors. So, after looking up "default windows system colors" online I could not find information on the System Colors for Windows controls (especially not Button controls).
Is there a way to get this color information (e.g. Button Border Color, Button Highlight Color, Button Hover Color, Button Clicked Background Color, etc) in .NET?
Yes. In fact, there is an entire class dedicated to this:
The SystemColors class.
...or for WPF (thanks #ORMapper), The System.Windows.SystemColors class.
There is a System Color Class out, which will provide you the Colors.
For WinForms use:
System.Drawing.SystemColors
For WPF use:
System.Windows.SystemColors
You can use also the GetSysColor function api function.
Valter
You could use the Win API, GetSysColor function...
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetSysColor(int nIndex);
The function returns the red, green, blue (RGB) color value of the given element.
To display the component of the RGB value, use the GetRValue, GetGValue, and GetBValue macros.
System colors for monochrome displays are usually interpreted as shades of gray.
To paint with a system color brush, an application should use GetSysColorBrush(nIndex), instead of CreateSolidBrush(GetSysColor(nIndex)), because GetSysColorBrush returns a cached brush, instead of allocating a new one.
I am wanting the same thing. My approach is to, upon initialization, create a temporary window with the background color specified in GetSysColor(COLOR_BTNFACE), the "standard" background color for dialog boxes. Then, I create a button with no text and get the colors. This temporary window is never displayed, and is destroyed immediately (WM_CREATE exit code = -1).
Ok, I'm doing this:
UIButton btReturn;
btReturn = UIButton.FromType (UIButtonType.RoundedRect);
btReturn.Frame = btnBounds;
btReturn.SetTitle (Caption, UIControlState.Normal);
btReturn.Layer.CornerRadius = 14;
btReturn.ClipsToBounds = true;
btReturn.TouchUpInside += touchHandler;
btReturn.BackgroundColor = UIColor.FromRGB (0xe4, 0x96, 0x37);
return btReturn;
Which begs the question... Why the heck is it come up as WHITE background??? I originally tried to set it to clear with a background image, but that doesn't work either...
What could be wrong?
This is because the RoundedRect type of UIButton doesn't allow for any other colour background apart from white.
You'll have to use a UIButtonType.Custom to change the background colour (but you'll lose the nice rounded button and end up with a square button - you'll have to design your own fancy looking button in your favourite graphics package :)
Try like this,
btReturn.BackgroundView = null;
btReturn.BackgroundColor = UIColor.Black;
Also refer the following links,
How to change background color of UIButton in Monotouch
How to set the background color on a DialogViewController (Monotouch.Dialog)
Look here: is-it-even-possible-to-change-a-uibuttons-background-color there is an example aswell on how to create your own button, as you cannot modify the rounded button.
Im trying to create a button in C# with the image as the button, I don't really want to use a picture box as i Require the "Label" bit of the button as the text for the buttons aren't in the image, This is what it looks like
If anyone can help me fix that white frame it would be much appreciated, Thanks!
EDIT: I found what was causing it but still no fix :( The BackColor on the actual form is where the white is coming from but you can't set the BackColor of a form to Transparent :/
Your image must have transparent background. Also see #IAbstract's suggestion.
As I answered here to remove the border you should set the FlatAppearance.BorderColor to transparent. The whole code to remove completely the border looks like:
customButton.TabStop = false;
//it's the best thing set flatstyle to flat when dealing with a custom button
customButton.FlatStyle = FlatStyle.Flat;
customButton.FlatAppearance.BorderSize = 0;
//set the border color to transparent by setting the alpha to 0 (it doesn't support Color.Transparent)
customButton.FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
I think a Background.Color = Transparent should fix it if WinForms.
Update
If you have set border and background properties to transparent, double check the white border to be transparent in the actual graphic?
I have a form (see screenshot):
As you can see, its a pretty basic form, with a save button. I have programmed it so that if any of the text fields get changed, the "SAVE" button changes color so that its obvious that I haven't clicked save and don't forget to. Unfortunately, simply changing the BackColor of the button to red isn't enough, because its UGLY as sin.
What can I do to change the color of the button to red, but not as ugly. As you can see, the "BackColor" doesn't change the entire button, just the inner piece. The border is still the same old fashioned transparent grey.
A little bit of a LinearGradientBrush can go a long way to soften the harshness of a pure red button.
button1.ForeColor = Color.White;
Bitmap bmp = new Bitmap(button1.Width, button1.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
using (LinearGradientBrush br = new LinearGradientBrush(
r,
Color.Red,
Color.DarkRed,
LinearGradientMode.Vertical)) {
g.FillRectangle(br, r);
}
}
then you can just assign the image to the button's BackgroundImage property:
button1.BackgroundImage = bmp;
Result:
Note: Assigning a background image will lose the mouse hover coloring of the button.
Another solution would be to add an Icon (e.g. exclamation mark) to the button instead to inform the user that the changes haven't been saved yet.
There are many tutorials online on how to create nice buttons with c#. For example this one allows you to create Vista like buttons. Have a look here: http://www.codeproject.com/Articles/19318/Vista-Style-Button-in-C
For basic colors visit this SO question:
C#: Changing Button BackColor has no effect
You can just use one of the button's properties, "FlatStyle". By default, it is standard. But if you switch it to "Popup", your background color will be extended to the area of the button. You can compare the followings:
left-standard, right-popup
This won't work in WinForms, but you might want to switch over to WPF.
It's much more convenient because you can configure EVERYTHING. (Even the color of a progressbar)
Edit: The OP doesn't have to entirely rewrite his application.
He just needs to redo the layout of it, the code can be C&P'd over to the new WPF project, fyi
Edit²: You don't even need to use code to change the color of a WPF button, you can just define a red overlay with 30% opacity in the XAML file. It's that easy, really.