Listenelement gewisse Zeilen färbig machen?



  • das problem ist nur das ich nicht weiß welcher index gerade dran ist.
    Ich möchte das schon machen während ich die DAten von der Datenbank hole und beim eintragen in das Listenelement vergleiche ich schon.
    zB bei Ja sollte die Zeile rot werden und bei nein blau.



  • Ist doch ganz einfach.
    Abgeleitete Klasse:

    ColorListBox.cpp

    // ColorListBox.cpp : implementation file
    
    #include "stdafx.h"
    #include "ColorListBox.h"
    #include ".\colorlistbox.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CColorListBox
    
    //-------------------------------------------------------------------
    //
    CColorListBox::CColorListBox()
    //
    // Return Value:	None.
    //
    // Parameters	:	None.
    //
    // Remarks		:	Standard constructor.
    //
    {
    }	// CColorListBox
    
    //-------------------------------------------------------------------
    //
    CColorListBox::~CColorListBox()
    //
    // Return Value:	None.
    //
    // Parameters	:	None.
    //
    // Remarks		:	Destructor.
    //
    {
    }	// ~CColorListBox()
    
    BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
    	//{{AFX_MSG_MAP(CColorListBox)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CColorListBox message handlers
    
    //-------------------------------------------------------------------
    //
    void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
    //
    // Return Value:	None.
    //
    // Parameters	:	lpDIS - A long pointer to a DRAWITEMSTRUCT structure 
    //							that contains information about the type of drawing required.
    //
    // Remarks		:	Called by the framework when a visual aspect of 
    //						an owner-draw list box changes. 
    //
    {
    	if ((int)lpDIS->itemID < 0)
    		return; 
    
    	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
    
    	COLORREF crText;
    	CString sText;
    	COLORREF crNorm = (COLORREF)lpDIS->itemData;		// Color information is in item data.
    	COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm));
    
    /*
    	CFont* font = GetFont();
    	LOGFONT lf;
    	font->GetLogFont(&lf);
    	lf.lfWeight = FW_BOLD;
    	font->DeleteObject();
    	font->CreateFontIndirect(&lf);
    	pDC->SelectObject(&font);
    	font->DeleteObject();
    */
    
    	// If item has been selected, draw the highlight rectangle using the item's color.
    /*	if ((lpDIS->itemState & ODS_SELECTED) &&
    		 (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
    	{
    		CBrush brush(crNorm);
    		pDC->FillRect(&lpDIS->rcItem, &brush);
    	}
    
    	// If item has been deselected, draw the rectangle using the window color.
    	if (!(lpDIS->itemState & ODS_SELECTED) &&	(lpDIS->itemAction & ODA_SELECT))
    	{
    		CBrush brush(::GetSysColor(COLOR_WINDOW));
    		pDC->FillRect(&lpDIS->rcItem, &brush);
    	}	 	
    
    	// If item has focus, draw the focus rect.
    	if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
    		pDC->DrawFocusRect(&lpDIS->rcItem); 
    
    	// If item does not have focus, redraw (erase) the focus rect.
    	if ((lpDIS->itemAction & ODA_FOCUS) &&	!(lpDIS->itemState & ODS_FOCUS))
    		pDC->DrawFocusRect(&lpDIS->rcItem); 
    
    */
    	// Set the background mode to TRANSPARENT to draw the text.
    	int nBkMode = pDC->SetBkMode(TRANSPARENT);
    
    	// If the item's color information is set, use the highlight color
    	// gray text color, or normal color for the text.
    	if (lpDIS->itemData)		
    	{
    		//if (lpDIS->itemState & ODS_SELECTED)
    		//	crText = pDC->SetTextColor(crHilite);
    		//else if (lpDIS->itemState & ODS_DISABLED)
    		//	crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
    		//else
    			crText = pDC->SetTextColor(crNorm);
    	}
    	// Else the item's color information is not set, so use the
    	// system colors for the text.
    	else
    	{
    		if (lpDIS->itemState & ODS_SELECTED)
    			crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
    		else if (lpDIS->itemState & ODS_DISABLED)
    			crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
    		else
    			crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
    	}
    
    	// Get and display item text.
    	GetText(lpDIS->itemID, sText);
    	CRect rect = lpDIS->rcItem;
    
    	// Setup the text format.
    	UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
    	if (GetStyle() & LBS_USETABSTOPS)
    		nFormat |= DT_EXPANDTABS;
    
    	// Calculate the rectangle size before drawing the text.
    	pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
    	pDC->DrawText(sText, -1, &rect, nFormat);
    
    	pDC->SetTextColor(crText); 
    	pDC->SetBkMode(nBkMode);
    }	// DrawItem
    
    //-------------------------------------------------------------------
    //
    void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
    //
    // Return Value:	None.
    //
    // Parameters	:	lpMIS - A long pointer to a 
    //							MEASUREITEMSTRUCT structure.
    //
    // Remarks		:	Called by the framework when a list box with 
    //						an owner-draw style is created. 
    //
    {
    	// ### Is the default list box item height the same as
    	// the menu check height???
    	lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
    }	// MeasureItem
    
    //-------------------------------------------------------------------
    //
    int CColorListBox::AddString(LPCTSTR lpszItem)
    //
    // Return Value:	The zero-based index to the string in the list box. 
    //						The return value is LB_ERR if an error occurs; the 
    //						return value is LB_ERRSPACE if insufficient space 
    //						is available to store the new string.
    //
    // Parameters	:	lpszItem - Points to the null-terminated 
    //							string that is to be added.
    //
    // Remarks		:	Call this member function to add a string to a list 
    //						box. Provided because CListBox::AddString is NOT
    //						a virtual function.
    //
    {
    	return ((CListBox*)this)->AddString(lpszItem);
    }	// AddString
    
    //-------------------------------------------------------------------
    //
    int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
    //
    // Return Value:	The zero-based index to the string in the list box. 
    //						The return value is LB_ERR if an error occurs; the 
    //						return value is LB_ERRSPACE if insufficient space 
    //						is available to store the new string.
    //
    // Parameters	:	lpszItem - Points to the null-terminated 
    //							string that is to be added.
    //						rgb - Specifies the color to be associated with the item.
    //
    // Remarks		:	Call this member function to add a string to a list 
    //						box with a custom color.
    //
    {
    	int nItem = AddString(lpszItem);
    	if (nItem >= 0)
    		SetItemData(nItem, rgb);
    	return nItem;
    }	// AddString
    
    //-------------------------------------------------------------------
    //
    int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem)
    //
    // Return Value:	The zero-based index of the position at which the 
    //						string was inserted. The return value is LB_ERR if 
    //						an error occurs; the return value is LB_ERRSPACE if 
    //						insufficient space is available to store the new string.
    //
    // Parameters	:	nIndex - Specifies the zero-based index of the position
    //							to insert the string. If this parameter is –1, the string
    //							is added to the end of the list.
    //						lpszItem - Points to the null-terminated string that 
    //							is to be inserted.
    //
    // Remarks		:	Inserts a string into the list box.	Provided because 
    //						CListBox::InsertString is NOT a virtual function.
    //
    {
    	return ((CListBox*)this)->InsertString(nIndex, lpszItem);
    }	// InsertString
    
    //-------------------------------------------------------------------
    //
    int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb)
    //
    // Return Value:	The zero-based index of the position at which the 
    //						string was inserted. The return value is LB_ERR if 
    //						an error occurs; the return value is LB_ERRSPACE if 
    //						insufficient space is available to store the new string.
    //
    // Parameters	:	nIndex - Specifies the zero-based index of the position
    //							to insert the string. If this parameter is –1, the string
    //							is added to the end of the list.
    //						lpszItem - Points to the null-terminated string that 
    //							is to be inserted.
    //						rgb - Specifies the color to be associated with the item.
    //
    // Remarks		:	Inserts a colored string into the list box.
    //
    {
    	int nItem = ((CListBox*)this)->InsertString(nIndex,lpszItem);
    	if (nItem >= 0)
    		SetItemData(nItem, rgb);
    	return nItem;
    }	// InsertString
    
    //-------------------------------------------------------------------
    //
    void CColorListBox::SetItemColor(int nIndex, COLORREF rgb)
    //
    // Return Value:	None.
    //
    // Parameters	:	nIndex - Specifies the zero-based index of the item.
    //						rgb - Specifies the color to be associated with the item.
    //
    // Remarks		:	Sets the 32-bit value associated with the specified
    //						item in the list box.
    //
    {
    	SetItemData(nIndex, rgb);	
    	RedrawWindow();
    }	// SetItemColor
    

    ColorListBox.h

    #if !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)
    #define AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_
    
    #if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000
    
    class CColorListBox : public CListBox
    {
    // Construction
    public:
    	CColorListBox();
    
    // Attributes
    public:
    
    // Operations
    public:
    	int AddString(LPCTSTR lpszItem);											// Adds a string to the list box
    	int AddString(LPCTSTR lpszItem, COLORREF rgb);						// Adds a colored string to the list box
    	int InsertString(int nIndex, LPCTSTR lpszItem);						// Inserts a string to the list box
    	int InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb);	// Inserts a colored string to the list box
    	void SetItemColor(int nIndex, COLORREF rgb);							// Sets the color of an item in the list box
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CColorListBox)
    	public:
    	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    	virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    	//}}AFX_VIRTUAL
    
    // Implementation
    public:
    	virtual ~CColorListBox();
    
    	// Generated message map functions
    protected:
    	//{{AFX_MSG(CColorListBox)
    	//}}AFX_MSG
    
    	DECLARE_MESSAGE_MAP()
    public:
    
    };
    
    /////////////////////////////////////////////////////////////////////////////
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)
    

    Implemtierung:

    CColorListBox m_chat;
    
    m_chat.InsertString(m_chat.GetCount(),"testfarbe",RGB(255,0,0));
    m_chat.InsertString(m_chat.GetCount(),"testfarbe1",RGB(0,0,255));
    


  • toll

    hast du das selbst geschrieben?

    wie ändere ich das Font??

    1. beim einfügen
    2. von einer selectierten zeile?

    in codeproject finde ich sowas nicht 😞

    sprich

    farbe + font ändern

    und so tief in die materie will ich auch nicht einsteigen, falls es schon was fertiges gibt.



  • ich brauche also nur das SetItemData?
    also wenn ich das richtig verstanden habe brauche ich das nur so machen -->

    if(mysqldatabase.field[3] == "ja")
    {
    m_iplist.InsertItem(0,mysqldatabase.field[2],0);
    m_iplist.SetItemText(0,1,mysqldatabase.field[3]);
    m_iplist.SetItemData(x, RGB(0,0,255));
    }
    else
    {
    m_iplist.InsertItem(0,mysqldatabase.field[2],0);
    m_iplist.SetItemText(0,1,mysqldatabase.field[3]);
    m_iplist.SetItemData(x, RGB(255,0,0));
    }

    Das funktioniert aber nicht.



  • Icvh habe es für meine Zwecke abgeändert.

    Was soll das X in m_iplist.SetItemData(x, RGB(255,0,0));

    Du kannst die Farbe gleich beim Insert angeben und geht nicht gibt es nicht. Es kommt immer eine Fehlermeldung wenn etwas nicht geht oder es passiert zumindest irgendwas.



  • OK *gg* geht nicht - damit meinte ich, dass sich nichts tut von der Farbe her. Keine Fehlermeldung.

    Und wie sollte ich das direkt im Insert machen?

    achja das x ist mein counter vom for (UINT x = 1;x < mysqldatabase.nrdaten();x++)



  • Weißt du auch wie man den Hintergrund färbig macht für die eine Zeile?



  • habe gerade gesehen das du ein listcontrol meinst.
    meine klasse ist für eine listbox.
    Es bleibt dir dann nichgts anderes übrig eine klasse zu schreiben oder bei codeproject codeguru zu schauen.



  • OK danke ... dort habe ich vorher schon gesucht ....

    Andere Frage habe ich auch noch. Du hast ja gesehen, dass ich mit der Datenbank arbeite. Wenn ich nun einen dAtensatz änderen muss ich die Datenbank neu abfragen. OK soweit ok nur gibt es jetzt eine Möglichkeit, dass wenn ich mit das item also die Zeile merke, dass ich dann damit wieder auf die Zeile springe wo ich vorher war?



  • Verstehe nur Bahnhof.
    Wenn du einen Datensatz updates muss die die Datenbank nicht abfragen.
    Es gibt keine Zeilen in einer Datenbank außer wenn du einen Datensatzbezeichner hast.

    AutoInc oder ähnliches.


Anmelden zum Antworten