How to read a color from registry?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Wennevik

    How to read a color from registry?

    Saving a color object to registry will set it's value to something like
    "Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
    but how do I get this back into a Color object???

    Casting it to Color only causes an exception.

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
  • CT

    #2
    Re: How to read a color from registry?

    Can you show some code, please?

    --
    Carsten Thomsen
    Enterprise Development with VS .NET, UML, and MSF

    "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
    news:oprxq49ly0 hntkfz@localhos t...
    Saving a color object to registry will set it's value to something like
    "Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
    but how do I get this back into a Color object???

    Casting it to Color only causes an exception.

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


    Comment

    • Morten Wennevik

      #3
      Re: How to read a color from registry?

      To make a registry entry in HKEY_Current_Us er\Software\Col orTest

      Color testColor = Color.FromArgb( 123, 234, 75);
      RegistryKey regKey =
      Registry.Curren tUser.CreateSub Key("Software\\ ColorTest");
      regKey.SetValue ("Color", testColor);
      regKey.Close();

      To read it I would want to use something like

      RegistryKey colorKey =
      Registry.Curren tUser.OpenSubKe y("Software\\Co lorTest");
      Color regColor = (Color)regKey.G etValue("Color" ); // this does not work
      colorKey.Close( );

      I can do it by translating the color to int and creating a new color from
      int, but I would prefer to keep the color readable in registry.

      --
      Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

      Comment

      • Ray Hsieh (Ray Djajadinata)

        #4
        Re: How to read a color from registry?

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Ah, that's because you're saving the result of ToString() method to the
        registry. Unfortunately, there's no easy way to convert it back to Color
        ~ other than examining the string--and definitely you can't cast it to
        Color because it's a string!

        It's better to save the result of calling ToArgb() to the registry. When
        you load the int value back and need to instantiate a Color object from
        that, you can just say:

        Color col = Color.FromArgb( <put the int from the Registry here>);

        Morten Wennevik wrote:
        | Saving a color object to registry will set it's value to something like
        | "Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
        | but how do I get this back into a Color object???
        |
        | Casting it to Color only causes an exception.
        |


        - --
        Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
        ray underscore usenet at yahoo dot com
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.3 (MingW32)
        Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

        iD8DBQE/nnFPwEwccQ4rWPg RAiw4AJ9FYtAbDA snn2lkNwv5XH6QN ldySACfedAF
        1OuxNwXHBgtVDRA Z55VQx7M=
        =bG0X
        -----END PGP SIGNATURE-----

        Comment

        • Sunil TG

          #5
          How to read a color from registry?

          Store the 3 numbers (r,g,b) in registry as one string
          value like "234,212,12 2" and when you retrieve this value,
          split it into 3 parts, and then use the Color.FromArgb( )
          method to recreate a color object.

          HTH
          Sunil TG
          [color=blue]
          >-----Original Message-----
          >Saving a color object to registry will set it's value to[/color]
          something like[color=blue]
          >"Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
          >but how do I get this back into a Color object???
          >
          >Casting it to Color only causes an exception.
          >
          >--
          >Using M2, Opera's revolutionary e-mail client:[/color]
          http://www.opera.com/m2/[color=blue]
          >.
          >[/color]

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: How to read a color from registry?

            Morten,

            The easiest way to do this would be to use an instance of the
            ColorConverter class. You can call the ConvertToString method on the class
            to convert your color instance to a string which you store in the registry.
            Then, later on, you can call ConvertFromStri ng on the string you get from
            the registry to get your color back.

            Hope this helps.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
            news:oprxq49ly0 hntkfz@localhos t...
            Saving a color object to registry will set it's value to something like
            "Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
            but how do I get this back into a Color object???

            Casting it to Color only causes an exception.

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


            Comment

            • Ray Hsieh (Ray Djajadinata)

              #7
              Re: How to read a color from registry?

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              I realized that using the Argb way, the name of the colour gets lost
              when you want to convert it back into a Color instance. The (better) way
              Nicholas suggested preserves it, plus it lets you read the color in the
              Registry :)

              Thanks!

              Nicholas Paldino [.NET/C# MVP] wrote:

              | Morten,
              |
              | The easiest way to do this would be to use an instance of the
              | ColorConverter class. You can call the ConvertToString method on the
              class
              | to convert your color instance to a string which you store in the
              registry.
              | Then, later on, you can call ConvertFromStri ng on the string you get from
              | the registry to get your color back.
              |
              | Hope this helps.

              - --
              Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
              ray underscore usenet at yahoo dot com
              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.2.3 (MingW32)
              Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

              iD8DBQE/nnhSwEwccQ4rWPg RApxdAJ4zBh1FJo mnA7GBXGYaPJZhT wfLSQCfYgJh
              eanTtqlfPxZhuhb iRFn8lRE=
              =a4dj
              -----END PGP SIGNATURE-----

              Comment

              • Morten Wennevik

                #8
                Re: How to read a color from registry?

                Yes, thank you, ColorConverter was rather good.
                It will still display the color name if the color has one, otherwise
                "123;234;23 1"

                --
                Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: How to read a color from registry?

                  Ray,

                  On top of that, if it is a named color, the return value from the
                  IsNamedColor property will be preserved.


                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Ray Hsieh (Ray Djajadinata)" <check@my.signa ture.com> wrote in message
                  news:e34WdlVnDH A.2512@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > -----BEGIN PGP SIGNED MESSAGE-----
                  > Hash: SHA1
                  >
                  > I realized that using the Argb way, the name of the colour gets lost
                  > when you want to convert it back into a Color instance. The (better) way
                  > Nicholas suggested preserves it, plus it lets you read the color in the
                  > Registry :)
                  >
                  > Thanks!
                  >
                  > Nicholas Paldino [.NET/C# MVP] wrote:
                  >
                  > | Morten,
                  > |
                  > | The easiest way to do this would be to use an instance of the
                  > | ColorConverter class. You can call the ConvertToString method on the
                  > class
                  > | to convert your color instance to a string which you store in the
                  > registry.
                  > | Then, later on, you can call ConvertFromStri ng on the string you get[/color]
                  from[color=blue]
                  > | the registry to get your color back.
                  > |
                  > | Hope this helps.
                  >
                  > - --
                  > Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
                  > ray underscore usenet at yahoo dot com
                  > -----BEGIN PGP SIGNATURE-----
                  > Version: GnuPG v1.2.3 (MingW32)
                  > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
                  >
                  > iD8DBQE/nnhSwEwccQ4rWPg RApxdAJ4zBh1FJo mnA7GBXGYaPJZhT wfLSQCfYgJh
                  > eanTtqlfPxZhuhb iRFn8lRE=
                  > =a4dj
                  > -----END PGP SIGNATURE-----
                  >[/color]


                  Comment

                  • Ray Hsieh (Ray Djajadinata)

                    #10
                    Re: How to read a color from registry?

                    -----BEGIN PGP SIGNED MESSAGE-----
                    Hash: SHA1

                    Oh, another thing I didn't know :) Thanks Nicholas!

                    Nicholas Paldino [.NET/C# MVP] wrote:

                    | Ray,
                    |
                    | On top of that, if it is a named color, the return value from the
                    | IsNamedColor property will be preserved.

                    - --
                    Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
                    ray underscore usenet at yahoo dot com
                    -----BEGIN PGP SIGNATURE-----
                    Version: GnuPG v1.2.3 (MingW32)
                    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

                    iD8DBQE/noR7wEwccQ4rWPg RAk5ZAJ4t1MWBra PzVgu8ONl4L/1WmR059wCdFc/J
                    zIoqLjcKWRs2VCC OsUJpYkY=
                    =XDU8
                    -----END PGP SIGNATURE-----

                    Comment

                    Working...