Owner Drawn Combo Box

Creating ownerdrawn combo box
By default, items in a combo box consist of strings of text. Should you need a combo box that displays graphical images instead of text, you can create an owner-drawn combo box. To create the owner-drawn combo box a class is derived from CComboBox. An object of the derived class is created and CComboBox::Create( ) is called to create the combo box. CComboBox::MeasureItem( ) and CComboBox::DrawItem( ) are overridden. When an item in an owner-drawn combo box needs to be drawn (or redrawn), Windows sends the combo box's parent a WM_DRAWITEM message with a pointer to a DRAWITEMSTRUCT structure containing all the information required to do the drawing, including a device context handle and a 0-based index identifying the item to be drawn. Before the first WM_DRAWITEM message arrives, the combo box's parent receives one or more WM_MEASUREITEM messages requesting the height of the items in the combo box. In this program an array of colors is created which is used to draw the combo box items when the window receives WM_DRAWITEM message. The DeflateRect( ) function decides the gap between two items. A rectangle is drawn around the item on which the cursor is placed. The color for highlighted item is retrieved using ::GetSysColor( ) function.


No comments: