vencese
asked on
Black listBox with white items ...
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.
vencese
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.
vencese
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(LPMEASUREITEMS TRUCT lpMeasureItemStruct);
};
void COwnerDrawListBox::Measure Item(LPMEA SUREITEMST RUCT lpMeasureItemStruct)
{
LOGFONT LogFont;
GetParent()->GetFont()->Ge tLogFont( &LogFont );
int nHeight= abs( LogFont.lfHeight ) + 2;
lpMeasureItemStruct->itemH eight = nHeight;
}
void COwnerDrawListBox::DrawIte m(LPDRAWIT EMSTRUCT 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->rcItem.top, 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.
class COwnerDrawListBox : public CListBox
{ public:
// Operations
// Implementation
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
virtual void MeasureItem(LPMEASUREITEMS
};
void COwnerDrawListBox::Measure
{
LOGFONT LogFont;
GetParent()->GetFont()->Ge
int nHeight= abs( LogFont.lfHeight ) + 2;
lpMeasureItemStruct->itemH
}
void COwnerDrawListBox::DrawIte
{
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->rcItem.top, 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.
ASKER
I tried the way you suggested, but at runtime I got an error:
"Debug Assertion Failed
file strcore.cpp, line 499:
LPSTR CString::GetBufferSetLengt h(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!
vencese
"Debug Assertion Failed
file strcore.cpp, line 499:
LPSTR CString::GetBufferSetLengt
{ 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!
vencese
ASKER
Can you send me the code to perform this task?
(I increased points to 200 and I'll grade you A)
Thanks in advance!
vencese