In Visual Studio, please suggest how to make custom color for a control and add it to property-> backColor Section.?
You can type a comma separated RGB value into the BackColor value in the property grid, eg:
150, 250, 70
You can use this method:
Color clr = Color.FromArgb(int alpha, int red, int green, int blue)
If you want this done using some User interface:
You could find out where the custom colors are stored for the current user, and then add one.
Or, create your own UITypeEditor, that contains the colors you want. Probably you have to inherit from Form, and override the BackColor property to add the correct attributes.
Much easier is to just set it in code, using the Argb code from the other answers.
You can not.
What you see is a default editor for Color type (create own control, add there public property of Color type and it will also uses it). Web and System tabs working fine. First tab is a sort of custom color pickup part from standard color pickup dialog.
I think MS fails to make popup editor to show modal dialog (because popup will get closed). =D
Perhaps colors there are taken from Windows color dialog, so you have to arrange it there (perhaps you can use winapi to do that). /shrug
Related
I am writing an application in .NET that has a plugin interface. That plugin interface provides several ways to draw information (controls) onto the surface of the application window. While there are several reasons why I am doing this, the main reason is to provide custom colorization to text, either through the use of a graphic or directly manipulating the color of the text based on the background color. I do this through the use of a "text mask" which is a black and white bitmap that works as an "alpha" map to let the Paint method know where to apply the texture/color changes.
The plugin developer has the option of using regular text (such as with a label), mask text (which is drawn to the mask rather than as a regular control), OR letting the user decide. To go along with this, I have provided a modified label class that can either be drawn "normally' (when the text mask is not set for the control), or to the text mask when the User OR Developer decides (depending on what the plugin developer wishes to offer to the user). Here is the class's code so that you understand how this is being done:
public class MaskingLabel : Label
{
private static readonly SolidBrush maskBrush = new SolidBrush(Color.White);
public Bitmap Mask { get; set; }
public MaskingLabel() : base() { }
protected override void OnPaint(PaintEventArgs e)
{
if (Mask == null)
base.OnPaint(e);
else
{
Graphics g = Graphics.FromImage(Mask);
g.DrawString(Text, Font, maskBrush, Location);
}
}
}
The problem I am running into is that this approach requires that I handle controls in a very specific order so that the form is drawn correctly. I need to find the most efficient approach to get the tasks listed below done in the order given. I have thought of three possibilities discussed further down. For reference, this is the order in which tasks must be done:
All "MaskingLabel" controls that have the bitmap object set to the mask must be drawn first so that the mask is created before the next step.
The mask is applied to the background picture.
The resulting Bitmap is drawn in a way similar to the way a background would be drawn (except that it is modified first).
The rest of the controls are drawn as normal.
Is there a way for me to insure this happens without separating the controls manually? My first guess is no. As such, I have a few guesses below about how I should go about this. I was hoping someone with more in depth knowledge of GDI+ could offer some insight.
One idea that has occurred to me is to draw the masked controls during the OnPaintBackground method. However, I don't want to waste time by painting the controls twice. This means I would need to filter out which controls are drawn during the main Paint method which effectively leads us to option 2 (FAIK):
I can manually filter out the controls which draw to the mask so that they don't get added to the control. My question here though is would they get drawn at all? Can I manually force them to invoke the OnPaint method?
If doing that wouldn't work, then perhaps I can create a separate derived panel control to serve as a "backdrop" child control that acts as the background picture which can be forced to be drawn first?
EDIT (With Part of the answer):
I realized after posting this that I already have part of the solution built into my project. Still, I think it is a legitimate question to ask, so if anyone can add insight beyond what I have done in my description below, it is welcome.
Specifically, my project has only two controls that are added to the "root" form: a bar that goes to the top (docked at the top when it is shown), and a transparent panel that occupies the rest of the space (with a dock style set to fill). So my solution would be to add the mask controls to the main form and add all the rest to the panel. This only leaves one remaining issue to be resolved: How do I make sure that the panel and the bar are drawn last? (As part of step 4 in the first list?)
First of all, sorry about the messy title...
I'll try my best to explain.
So.. I'm currently using for the BackColor property in Designer, the following:
this.BackColor = System.Drawing.SystemColors.Control;
When I publish the application and use in other machine (Older windows... different theme, etc..). The color that was defined before, its not the same.
My question is, is there another way to apply a color to avoid this kind of situation?
Thank you very much!
The problem you are encountering is that you have selected a system color which will change depending on what system you run it on.
If you want to have colors stay the same no matter what system they are run on then you must explicitly set the color yourself.
this.BackColor = System.Drawing.Color.FromArgb(255, 0, 0); //Red
This will ensure the color stays red on any system.
Simply replace the FromArgb parameters with the correct RGB values for the desired color.
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).
I have made a control in silverlight that has a SolidColorBrush property. In XAML, you can set this to be preexisting Colors such as Black, Yellow, White, etc. I assume there is a TypeConverter that takes the string "Black" and converts it to a Color object and creates a new SolidColorBrush based on this object. I want this functionality to remain, but with the added capability to also accept hex strings to set the color. I would need a TypeConverter for this, correct? Is there a way I can extend from the current one so that I can still pass in color names?
XAML already accepts hex. Eg:
Background="#FF9D661C"
Having the VS Properties pane open while on a XAML element helps indicate possibilities thats aren't necessarily obvious via intelisence (Eg setting Background as hex or a gradiant brush).
As #Ricibob said, the existing converter alreadys works how you want it to. In addition, it looks like all of the built in TypeConverters are sealed, so you can't inherit from them (although there's nothing keeping you from using composition to solve the problem).
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.