Proper way to determine the colors of controls?
-
Sometimes I need to determine the color of different controls. I give a couple a of examples. In a MessageBox I needed a custom made gif-icon and I wanted the background color of the icon to match the color of the MessageBox itself. In another project I wanted to get rid of the little sort-array in the header of a DataGridView and to do that I had to override the Paint-method and paint the arrow with the background color. To determine the colors in these two examples I did a print screen and pasted the screen dump into Paint and used the color picker to get the RGB-values. This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer. Something tells me there must be a way to query the color to use instead of determining it by hand and then hardcoding it. How do I do this?
-
Sometimes I need to determine the color of different controls. I give a couple a of examples. In a MessageBox I needed a custom made gif-icon and I wanted the background color of the icon to match the color of the MessageBox itself. In another project I wanted to get rid of the little sort-array in the header of a DataGridView and to do that I had to override the Paint-method and paint the arrow with the background color. To determine the colors in these two examples I did a print screen and pasted the screen dump into Paint and used the color picker to get the RGB-values. This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer. Something tells me there must be a way to query the color to use instead of determining it by hand and then hardcoding it. How do I do this?
-
Gif supports transparency, can't you make the background transparent in your gif images? Would this do what you need?
-
Well, in the specific case with a gif-icon I could use transparency. However, I would still like to know how I can determine the color programmatically.
you could atempt use the folowing Bitmap bmp; // intelizing bmp if (bmp.GetPixel(x,y) == Color.White) // Color has predefined colors. SystemColor has a system setting colors as predefind { }
-
Sometimes I need to determine the color of different controls. I give a couple a of examples. In a MessageBox I needed a custom made gif-icon and I wanted the background color of the icon to match the color of the MessageBox itself. In another project I wanted to get rid of the little sort-array in the header of a DataGridView and to do that I had to override the Paint-method and paint the arrow with the background color. To determine the colors in these two examples I did a print screen and pasted the screen dump into Paint and used the color picker to get the RGB-values. This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer. Something tells me there must be a way to query the color to use instead of determining it by hand and then hardcoding it. How do I do this?
arnold_w wrote:
This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer.
That could be cousing also by monitors. Some have Color temperature (or even named as Color Tone) setting
-
arnold_w wrote:
This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer.
That could be cousing also by monitors. Some have Color temperature (or even named as Color Tone) setting
What I mean is that my DataGridView (in the example above) can look perfect (meaing the arrow is not visible because it has been filled with the correct backgound color) on my computer, but on another computer the arrow is visible, appearing with a slightly different shade of gray. Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it. I am therefore confident that this is not a monitor issue.
-
you could atempt use the folowing Bitmap bmp; // intelizing bmp if (bmp.GetPixel(x,y) == Color.White) // Color has predefined colors. SystemColor has a system setting colors as predefind { }
I don't understand how to use this. Can you please give an example how I can use it when I override the Paint-method for DataGridViewColumnHeaderCell:
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
-
What I mean is that my DataGridView (in the example above) can look perfect (meaing the arrow is not visible because it has been filled with the correct backgound color) on my computer, but on another computer the arrow is visible, appearing with a slightly different shade of gray. Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it. I am therefore confident that this is not a monitor issue.
arnold_w wrote:
Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it.
So other background is not hardcoded if you use static prop ties of class Color (Color.White or Color.Blue). Those colors are hard coded. However class SystemColor has also static proprties, (SystemColor.Window or SystemColor.Control) those properties are system specific tied to color schemes used by windows. These color schemes can be easily changed
-
I don't understand how to use this. Can you please give an example how I can use it when I override the Paint-method for DataGridViewColumnHeaderCell:
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
I actually don't remember Paint method has this many parameters. I assume you are inherited from DataGridView The method I posted was intended from thread above yours. I posted in wrong but I left it because you asked a question in that post that could be correct answer. It is better to get color from propties instead atempt to read from a point on the screan DataGridView background color is default to SystemColors.AppWorkspace. So I suggest you use to get your background color:
this.BackgroundColor
modified on Tuesday, January 5, 2010 10:57 AM
-
arnold_w wrote:
Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it.
So other background is not hardcoded if you use static prop ties of class Color (Color.White or Color.Blue). Those colors are hard coded. However class SystemColor has also static proprties, (SystemColor.Window or SystemColor.Control) those properties are system specific tied to color schemes used by windows. These color schemes can be easily changed