Creating a Custom Color Selection Dialog

Environment: Win32 API (no MFC)

This is a GPL/LGPL color picker demo based on a custom color selector. I coded it as a part of a Win32 server that I use in my graphics apps. It's pure Win32 API C++ and it should compile with VC++ 6.0. For your convenience, there are precompiled binaries in the 'bin' folder.

To Implement the Picker

  1. Build 'server_win.lib' and 'server_util.lib' (with the appropriate DLLs).
  2. Include 'server_win.h' and 'server_util.h' into your source code.
  3. Make a reference to the picker's global instance:
  4.     "c_colorpick& <yourpicker> = colorpick;"
  5. Call the picker where you want it to appear:
  6.     "COLORREF rgb =
                  <yourpicker>.getcolor(<handle to owner window>);"
    
    The function above returns -1 when 'Cancel' is pressed.

To Get the Picker's Settings

  1. Declare an instance of color-info structure:
  2.     "_colorinfo <yourinfo>;"
    This structure will hold the color space type, palette saturation range, and custom colors.
  3. When the picker returns, obtain the above values by calling:
  4.     "<yourpicker>.getinfo(<yourinfo>);"
  5. Then you can save these values into a file so that all settings can be loaded when the app starts again. How it could be done is shown in the "bool writeini()" function in 'colpick_main.cpp'.

To Initialize the Picker to the Last-Saved Settings

  1. Load the file you`ve saved in the previous section and fill in the <yourinfo> structure.
  2. Before you call the picker, make a call:
  3.     "<yourpicker>.setinfo(<yourinfo>);"
  4. Your picker should be initialized. (For details, look at "bool readini()" and "WinMain()" in 'colpick_main.cpp'.)

Known Bug

When using numeric input of HSL or HSV, the entered value sometimes skips down by 1. For example, if you insert 180, the value skips to 179. This is probably due to running the entered value through the whole conversion routine; that might be inexact.

oscar@mail.viapvt.sk

Downloads

Download demo project (Self extracting EXE) - 158 Kb


About the Author

Oto Spal

If you are really interested in who i am, you can visit me at www.armego.sk

Comments

  • best of the bunch...

    Posted by Legacy on 01/12/2004 12:00am

    Originally posted by: Frank Evans

    Thanks for a well polished utility. Numerous color pickers floating around. Yours showed the most love. Good layout, etc.

    Reply
  • This is wonderful.

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: Li Zhaoming

    Just by looking at your title pic, I know this is a wonderful article, and the excellent code!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds