Ownerdraw combobox to select colors


horizontal rule

Back Home Next

Author Aart Onkenhout
Date 01-06-2003


I needed a way to let users select colors within an application. You can use the ChooseColor function of course (from PB8 on as I'm well), but by using a combobox to do so you don't have to open up another window. You can place this combobox e.g. on an existing window with other settings the user can manipulate.


The object that does a major part of the job is n_combobox_color. Within this object a combobox is created by using the CreateWindowExA Windows-API with 'COMBOBOX' as the window class. Within the style of the window CBS_OWNERDRAWFIXED is included. This makes the combobox ownerdraw and makes it possible to draw your own contents. When making the combobox ownerdraw a WM_DRAWITEM message is sent to the parent of the combobox for every item that has to be drawn. The parent for n_combobox_color is u_combobox. This object has a userevent mapped to PBM_DRAWITEM. Everytime an item has to be drawn, this event is fired. It calls the function of_DrawItem on n_combobox_color that does the actual drawing.



As you can see on the sample screen you can just show colors or colors and their names. You can switch between them by calling of_SetColorOnly on n_combobox_color.

For some reason, when selecting an item the notification of this is not sent to the direct parent of n_combobox_color but to the window u_combobox is placed upon. This means you have to add a userevent to your window and map this to PBM_COMMAND (see ue_command on w_colorcombobox for an example).


You can download a small sample application (PB8 Build 9704). Within this zip file a small dll, pbutils.dll, is included, written by Roy Kiesler and freely available at http://www.teamsybase.com/roy.kiesler/downloads/pbutils.zip:

Download colorcombobox.zip (27 Kb)