Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Proper way to determine the colors of controls?

Proper way to determine the colors of controls?

Scheduled Pinned Locked Moved C#
questiondatabasedata-structuresregex
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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?

    P S 2 Replies Last reply
    0
    • A arnold_w

      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?

      P Offline
      P Offline
      petercrab
      wrote on last edited by
      #2

      Gif supports transparency, can't you make the background transparent in your gif images? Would this do what you need?

      A 1 Reply Last reply
      0
      • P petercrab

        Gif supports transparency, can't you make the background transparent in your gif images? Would this do what you need?

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • A arnold_w

          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.

          S Offline
          S Offline
          Saksida Bojan
          wrote on last edited by
          #4

          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 { }

          A 1 Reply Last reply
          0
          • A arnold_w

            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?

            S Offline
            S Offline
            Saksida Bojan
            wrote on last edited by
            #5

            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

            A 1 Reply Last reply
            0
            • S Saksida Bojan

              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

              A Offline
              A Offline
              arnold_w
              wrote on last edited by
              #6

              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.

              S 1 Reply Last reply
              0
              • S Saksida Bojan

                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 { }

                A Offline
                A Offline
                arnold_w
                wrote on last edited by
                #7

                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)

                S 1 Reply Last reply
                0
                • A arnold_w

                  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.

                  S Offline
                  S Offline
                  Saksida Bojan
                  wrote on last edited by
                  #8

                  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

                  A 1 Reply Last reply
                  0
                  • A arnold_w

                    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)

                    S Offline
                    S Offline
                    Saksida Bojan
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • S Saksida Bojan

                      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

                      A Offline
                      A Offline
                      arnold_w
                      wrote on last edited by
                      #10

                      SystemColor seems to be exactly what I was looking for. A big thank you for your help!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups