OnCtlColor



  • Hi,

    in einer Anwendung würde ich gerne den Text in einem Editcontrol
    je nach gedrücktem Button andersfarbig darstellen lassen.

    Mein Versuch klappte nur teilweise. Und zwar wird nur beim Start der
    Anwendung die Textfarbe beeinflusst. Nicht aber, wenn ich z.B. Button1
    drücke.

    HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    if(nCtlColor == CTLCOLOR_EDIT)
    {
    	if(farbe=="1")//farbe ist ein CString
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT2) 
    		{
    			pDC->SetTextColor(RGB(10,110,255));
    		}
    	}else
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT2) 
    		{
    
    			pDC->SetTextColor(RGB(255,10,55));
    		}
    	}
    }
    
    return hbr;
    
    }
    

    In meinem Buttonaufruf setze ich einfach die Variable der Klasse (CString farbe) auf z.B."0".
    Die Textfarbe ändert sich aber leider nicht. OnCtlColor wird doch, wie ich gelesen habe nicht von mir aufgerufen oder?



  • Hallo!

    Wenn du einen Button drückst, wird kein OnPaint Event o.ä. für andere Fenster aufgerufen. Du musst dann deinem CEdit mitteilen, dass es sich neu zeichnen soll.

    Könntest beispielsweise ein Invalidate an das CEdit schicken.

    Gruß Jochen



  • Hi Jochen, danke das funktioniert. Allerdings wird meine Combobox
    nicht mit der neuen Farbe überzeichnet.

    Hier mal der ganze Code aus der OnCtlColor:

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    if(nCtlColor == CTLCOLOR_EDIT)
    {
    	if(farbe=="1")
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT1) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT2) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    	}else
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT1) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    		if (pWnd->GetDlgCtrlID() == IDC_EDIT2) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    	}
    }
    
    if(nCtlColor == CTLCOLOR_LISTBOX)
    {
    
    	if(farbe=="1")
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO1) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO2) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO3) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO4) 
    		{
    			pDC->SetTextColor(RGB(0,51,255));
    		}
    
    	}else
    	{
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO1) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO2) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO3) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    		if (pWnd->GetDlgCtrlID() == IDC_COMBO4) 
    		{
    			pDC->SetTextColor(RGB(204,0,51));
    		}
    
    	}
    
    }
    
    return hbr;
    


  • Ist denn CTLCOLOR_LISTBOX überhaupt richtig bei ner ComboBox?


  • Mod

    Ja! Eine ComboBox versendet zwei Arten von CtlColor Nachricten. Einmal für das Edit-Control und einmal für das List-Control.



  • Hi Martin,

    dann frag ich mich, warum sich bei mir der Text in der Combo farblich nicht
    verändert. Ich starte den Aufruf über

    GetDlgItem(IDC_COMBO1)->Invalidate(1);
    

  • Mod

    Edit: Antwort gelöscht, weil irreleant... sorry!



  • Hi Martin,

    leider auch ohne die "1" keine Veränderung. Meine Comboboxen werden textmäßig
    nicht eingefärbt.



  • Sry, aber ich habe es noch nicht zum laufen gebracht. *push*
    In der Hoffnung jemand von euch kann helfen.



  • Martin Richter schrieb:

    Ja! Eine ComboBox versendet zwei Arten von CtlColor Nachricten. Einmal für das Edit-Control und einmal für das List-Control.

    Ich meine mich düster zu erinnern, daß die beiden Teile ihre eigenen IDs bekommen.

    @Shark: Kannst du mal mit TRACE verfolgen, für welche Controls es aufgerufen wird?

    PS: Was ich in der MSDN gefunden habe, könnte auch weiterhelfen:

    OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create a CComboBox with an override of OnCtlColor that checks for CTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, the SetBkColor member function must be used to set the background color for the text.



  • Hi CStoll,

    wenn ich es richtig verstanden habe soll ich es also so versuchen:

    if(nCtlColor == CTLCOLOR_LISTBOX)
    {
    pDC->SetBkColor(RGB(204,0,51)); 
    }
    

    Auch hier gibts leider keine Veränderung. TRACE prüfe ich gleich mal...


  • Mod

    Nein! Du musst eine eigene CComboBox Klasse erzeugen die OnCtlColor behandelt und diese musst Du für den subclass verwenden.



  • Wobei mir subclassen noch etwas fremd ist...Gibt's dazu hier ein kleines
    Tutorial?


Anmelden zum Antworten