Owner drawn ListBox problem
-
I created a listbox (owner drawn) using the code given below. the problem with my application is that when i click on any item in the listbox it gets selected :confused:. how to rectify that ? Here's my code :- CChatHistory is my ListBox's class name
// ChatHistory.cpp : implementation file
//#include "stdafx.h"
#include "sock.h"
#include "ChatHistory.h"// CChatHistory
IMPLEMENT_DYNAMIC(CChatHistory, CListBox)
CChatHistory::CChatHistory()
{}
CChatHistory::~CChatHistory()
{
}BEGIN_MESSAGE_MAP(CChatHistory, CListBox)
END_MESSAGE_MAP()// CChatHistory message handlers
// Colored entries in List Box
// Override the CListBox::MeasureItem virtual function to specify the height for each item. // Override the CListBox::DrawItem virtual function to perform the painting. // Override the CListBox::CompareItem virtual function to determine the order in which the a string has to be added. This is necessary only if you wish to have a sorted list. // Note that the list box should have the LBS\_OWNERDRAWVARIABLE and LBS\_HASSTRINGS styles set. You need to select these styles for the list box when you create it using a resource editor. Otherwise, if you create the list box dynamically, specify these styles during creation. // The following CLineListBox class provides an implementation. Here the color for the text is stored as item data and retrieved during painting. First, we implement the AddItem function, which adds the string to the list box and stores the color in the 32-bit item data associated with the string:
void CChatHistory::AddItem(const CString& str, COLORREF rgbText)
{
int nIndex;
nIndex = AddString(str);
if( CB_ERR != nIndex )
SetItemData(nIndex, rgbText);
}
// Next, we override DrawItem to draw the string in the color stored in the item data:void CChatHistory::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC dc;
CRect rcItem(lpDIS->rcItem);
UINT nIndex = lpDIS->itemID;
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
dc.Attach(lpDIS->hDC);// Save these value to restore them when done drawing. COLORREF crOldTextColor = dc.GetTextColor(); COLORREF crOldBkColor = dc.GetBkColor(); CBrush br(rgbBkgnd); dc.FillRect(rcItem, &br); if( lpDIS->itemState & ODS\_FOCUS ) dc.DrawFocusRect(rcItem); if( nIndex != (UINT)-1 ) { // The text color is stored
-
I created a listbox (owner drawn) using the code given below. the problem with my application is that when i click on any item in the listbox it gets selected :confused:. how to rectify that ? Here's my code :- CChatHistory is my ListBox's class name
// ChatHistory.cpp : implementation file
//#include "stdafx.h"
#include "sock.h"
#include "ChatHistory.h"// CChatHistory
IMPLEMENT_DYNAMIC(CChatHistory, CListBox)
CChatHistory::CChatHistory()
{}
CChatHistory::~CChatHistory()
{
}BEGIN_MESSAGE_MAP(CChatHistory, CListBox)
END_MESSAGE_MAP()// CChatHistory message handlers
// Colored entries in List Box
// Override the CListBox::MeasureItem virtual function to specify the height for each item. // Override the CListBox::DrawItem virtual function to perform the painting. // Override the CListBox::CompareItem virtual function to determine the order in which the a string has to be added. This is necessary only if you wish to have a sorted list. // Note that the list box should have the LBS\_OWNERDRAWVARIABLE and LBS\_HASSTRINGS styles set. You need to select these styles for the list box when you create it using a resource editor. Otherwise, if you create the list box dynamically, specify these styles during creation. // The following CLineListBox class provides an implementation. Here the color for the text is stored as item data and retrieved during painting. First, we implement the AddItem function, which adds the string to the list box and stores the color in the 32-bit item data associated with the string:
void CChatHistory::AddItem(const CString& str, COLORREF rgbText)
{
int nIndex;
nIndex = AddString(str);
if( CB_ERR != nIndex )
SetItemData(nIndex, rgbText);
}
// Next, we override DrawItem to draw the string in the color stored in the item data:void CChatHistory::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC dc;
CRect rcItem(lpDIS->rcItem);
UINT nIndex = lpDIS->itemID;
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
dc.Attach(lpDIS->hDC);// Save these value to restore them when done drawing. COLORREF crOldTextColor = dc.GetTextColor(); COLORREF crOldBkColor = dc.GetBkColor(); CBrush br(rgbBkgnd); dc.FillRect(rcItem, &br); if( lpDIS->itemState & ODS\_FOCUS ) dc.DrawFocusRect(rcItem); if( nIndex != (UINT)-1 ) { // The text color is stored
Remove the following lines from the
DrawItem
method.if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Remove the following lines from the
DrawItem
method.if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)removing these lines causes my control to not even display a single line. now my listbox is not displaying anything in it.
Future Lies in Present. Manmohan Bishnoi
-
removing these lines causes my control to not even display a single line. now my listbox is not displaying anything in it.
Future Lies in Present. Manmohan Bishnoi
OK. Try this - Replace
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
with
COLORREF rgbBkgnd = COLOR_WINDOW;
Remove
if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);Replace
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}with
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
OK. Try this - Replace
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
with
COLORREF rgbBkgnd = COLOR_WINDOW;
Remove
if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);Replace
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}with
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)now background goes black for each line added to the listbox.
Future Lies in Present. Manmohan Bishnoi
-
OK. Try this - Replace
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
with
COLORREF rgbBkgnd = COLOR_WINDOW;
Remove
if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);Replace
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}with
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)It's solved :cool::thumbsup: just done as you said with some modification:- REPLACE
COLORREF rgbBkgnd = COLOR_WINDOW;
WITH
COLORREF rgbBkgnd = ::GetSysColor(COLOR_WINDOW);
Thanks SUPERMAN
Future Lies in Present. Manmohan Bishnoi
-
Remove the following lines from the
DrawItem
method.if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)At best, that does not prevent selection, all it attempts to do is not show the selected status... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.