Black listBox with white items ...

Posted on 1997-11-18
Last Modified: 2013-11-20
I'd like to change the default colors of a CListBox in a tab control in order to draw white items on a black background.
I know that the DRAWITEMSTRUCT structure can probably be used, but I don't know exactly the way to handle it.
I hope someone can send me the code to perform this task.
Many thanks in advance.

Question by:vencese
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

galkin earned 220 total points
ID: 1310200
You must handle WM_DRAWITEM and WM_MEASUREITEM(if you list box has LBS_OWNERDRAWVARIABLE  style) messages in your dialog box. If you add message handlers for these messages your will get OnDrawItem( int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct ) and afx_msg void OnMeasureItem( int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct ). First parameter in this function determins what control sends message, so if it is ODT_LISTBOX message is sent by list box. DRAWITEMSTRUCT structure passed as second parameter to OnDrawItem contains all neccessary information including handle of device context, list box item bounding rectangle, so you can draw using colors and fonts you need.

Author Comment

ID: 1310201
I like your answer, but I have problems (I'm a beginner with MFC).
Can you send me the code to perform this task?
(I increased points to 200 and I'll grade you A)
Thanks in advance!



Expert Comment

ID: 1310202
If you new to MFC I suggest you more simple solution. I derived class from CListBox and overwrote its DrawItem and MeasureItem functions( you can also do this instead of overwriting OnDrawItem and OnMeasureItem in parent dialog) and this is its implementation

class COwnerDrawListBox : public CListBox

{ public:
// Operations

// Implementation
      virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
      virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);

void COwnerDrawListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)


    LOGFONT LogFont;
    GetParent()->GetFont()->GetLogFont( &LogFont );
    int nHeight= abs( LogFont.lfHeight ) + 2;
  lpMeasureItemStruct->itemHeight = nHeight;

void COwnerDrawListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)

   CDC* pDC = CDC::FromHandle(lpDIS->hDC);
//--------- Order number of the curretn string
  int itemNumber = (int)lpDIS->itemData;  

//--------------- Now we set color of selected item
  COLORREF bkColor, textColor;
  if ( !(lpDIS->itemState & ODS_SELECTED) )  
// The item is not selected
  { bkColor =   pDC->SetBkColor( RGB( 255, 255, 255 ) );
    textColor = pDC->SetTextColor( RGB( 0, 0, 0 ) );
  else  // Selected item
  { bkColor =   pDC->SetBkColor( RGB( 0, 0, 128 ) );
    textColor = pDC->SetTextColor( RGB( 255, 255, 0 ) );
  CString rString;
  GetText(itemNumber , rString );

  pDC->ExtTextOut( lpDIS->rcItem.left, lpDIS->, ETO_CLIPPED | ETO_OPAQUE, &lpDIS->rcItem,
                   rString, rString.GetLength() + 1, NULL );
//--------- Restore base settings
  pDC->SetTextColor( textColor);
  pDC->SetBkColor( bkColor );

Now if you add list box to your document template use class wizard to add to it mmber variable associated with this list box. Class Wizard will add member variable of type CListBox, so change it to COwnerDrawListBox.

Author Comment

ID: 1310203
I tried the way you suggested, but at runtime I got an error:
"Debug Assertion Failed
 file strcore.cpp, line 499:
 LPSTR CString::GetBufferSetLength(int nNewLength)
 { ASSERT (nNewLength >= 0);

The program stops at the line: GetText(itemNumber, rString);
with nNewLength=-1.

Have I to define a DRAWITEMSTRUCT and call DrawItem(...) to add
items to the list box?
Can I add items to the list box with addString(...)?

Many thanks for your time!



Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Host to IP 7 84
CRON statement to run every 3 minutes except on Monday morning 12am to 2am 7 123
pre4 challenge 19 116
listing all the respondents to a twitter feed - Java 5 49
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question