Click here to Skip to main content
15,891,253 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is MAX_INT for? Pin
Garth J Lancaster21-Mar-10 11:27
professionalGarth J Lancaster21-Mar-10 11:27 
AnswerRe: What is MAX_INT for? Pin
Adam Roderick J21-Mar-10 18:57
Adam Roderick J21-Mar-10 18:57 
GeneralRe: What is MAX_INT for? Pin
Vaclav_22-Mar-10 3:48
Vaclav_22-Mar-10 3:48 
GeneralRe: What is MAX_INT for? Pin
Joe Woodbury22-Mar-10 6:30
professionalJoe Woodbury22-Mar-10 6:30 
GeneralRe: What is MAX_INT for? Pin
Adam Roderick J22-Mar-10 7:08
Adam Roderick J22-Mar-10 7:08 
GeneralRe: What is MAX_INT for? Pin
Joe Woodbury22-Mar-10 7:16
professionalJoe Woodbury22-Mar-10 7:16 
GeneralRe: What is MAX_INT for? Pin
Adam Roderick J23-Mar-10 5:38
Adam Roderick J23-Mar-10 5:38 
QuestionOwner drawn ListBox problem Pin
Manmohan2921-Mar-10 5:25
Manmohan2921-Mar-10 5:25 
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 | :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 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);
			}

			// Reset the background color and the text color back to their
			// original values.
			dc.SetTextColor(crOldTextColor);
			dc.SetBkColor(crOldBkColor);

			dc.Detach();
		}
//		Then, we have to override MeasureItem to tell Windows how high each item is. (Note that the items could each be of different height if we wanted.)

void CChatHistory::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) 
		{
			// Set the item height. Get the DC, select the font for the
			// list box, and compute the average height.
			CClientDC   dc(this);
			TEXTMETRIC   tm;
			CFont* pFont = GetFont();
			CFont* pOldFont = dc.SelectObject(pFont);
			dc.GetTextMetrics(&tm);
			dc.SelectObject(pOldFont);
			lpMIS->itemHeight = tm.tmHeight + 4;   
		}
//		Finally, we support sorting by overriding CompareItem:

int CChatHistory::CompareItem(LPCOMPAREITEMSTRUCT lpCIS) 
		{
			CString str1, str2;
			GetText(lpCIS->itemID1, str1);
			GetText(lpCIS->itemID2, str2);
			return str1.Compare(str2);
		}

Future Lies in Present.
Manmohan Bishnoi

AnswerRe: Owner drawn ListBox problem Pin
«_Superman_»21-Mar-10 6:40
professional«_Superman_»21-Mar-10 6:40 
GeneralRe: Owner drawn ListBox problem Pin
Manmohan2921-Mar-10 7:04
Manmohan2921-Mar-10 7:04 
GeneralRe: Owner drawn ListBox problem Pin
«_Superman_»21-Mar-10 7:10
professional«_Superman_»21-Mar-10 7:10 
GeneralRe: Owner drawn ListBox problem Pin
Manmohan2921-Mar-10 7:27
Manmohan2921-Mar-10 7:27 
GeneralRe: Owner drawn ListBox problem Pin
Manmohan2921-Mar-10 8:06
Manmohan2921-Mar-10 8:06 
AnswerRe: Owner drawn ListBox problem Pin
Luc Pattyn21-Mar-10 11:18
sitebuilderLuc Pattyn21-Mar-10 11:18 
QuestionOpenGL Pin
MrMcIntyre21-Mar-10 4:45
MrMcIntyre21-Mar-10 4:45 
AnswerRe: OpenGL Pin
Richard MacCutchan21-Mar-10 6:09
mveRichard MacCutchan21-Mar-10 6:09 
QuestionGetAsyncKeyState() and GetKeyState() [Solved] Pin
Rozis21-Mar-10 2:05
Rozis21-Mar-10 2:05 
AnswerRe: GetAsyncKeyState() and GetKeyState() Pin
CPallini21-Mar-10 2:48
mveCPallini21-Mar-10 2:48 
GeneralRe: GetAsyncKeyState() and GetKeyState() Pin
Rozis21-Mar-10 3:12
Rozis21-Mar-10 3:12 
GeneralRe: GetAsyncKeyState() and GetKeyState() Pin
CPallini21-Mar-10 3:23
mveCPallini21-Mar-10 3:23 
GeneralRe: GetAsyncKeyState() and GetKeyState() Gathered answers [modified] Pin
Rozis21-Mar-10 4:23
Rozis21-Mar-10 4:23 
AnswerRe: GetAsyncKeyState() and GetKeyState() Pin
Adam Roderick J21-Mar-10 4:53
Adam Roderick J21-Mar-10 4:53 
GeneralRe: GetAsyncKeyState() and GetKeyState() Pin
Rozis21-Mar-10 5:08
Rozis21-Mar-10 5:08 
GeneralRe: GetAsyncKeyState() and GetKeyState() Pin
Adam Roderick J21-Mar-10 6:11
Adam Roderick J21-Mar-10 6:11 
GeneralRe: GetAsyncKeyState() and GetKeyState() Pin
Rozis21-Mar-10 10:44
Rozis21-Mar-10 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.