IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

Probleme pour changer la couleur du texte d'un controle edit


Sujet :

MFC

  1. #1
    Membre averti
    Inscrit en
    Mars 2008
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 16
    Par défaut Probleme pour changer la couleur du texte d'un controle edit
    Bonjour,

    Je n'arrive tout simplement pas à changer la couleur du texte que j'écris dans mon EDIT.

    Voici le morceau de code "chargé" de faire cela.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    LRESULT CALLBACK msgProcess(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static HWND hEdit;
    static HDC  hdcEdit;
     
       switch(msg)
       {  
          //message perso demandant la mise a jour du texte du controle EDIT
          case WM_SET_TEXT:
             //message avec argument texte NULL, on reset le contenu du label
             if (lParam == (LPARAM)NULL)
             {
                SetWindowText(hEdit, "");
             }
             //sinon on ajoute le message a la suite du contenu du label
             else
             {
                long int mallocSize = 0;
                char* szDebug = NULL;
                COLORREF color;
     
                //calcul de la taille de la memoire a allouer
                mallocSize  = GetWindowTextLength(hEdit);
                mallocSize += strlen((char*)lParam) + 1;
                szDebug = malloc(mallocSize);
     
                GetWindowText(hEdit, szDebug, mallocSize);
                sprintf(szDebug, "%s%s", szDebug, (char*)lParam);
     
                printf("hdc : %x\n",hdcEdit);
                SetTextColor(hdcEdit, RGB(0,0,255));
     
                SetWindowText(hEdit, szDebug);
     
                free(szDebug);
             }
             break;
          //message informant de la creation de la fenetre parente
          case WM_CREATE:
     
             //creation du controle EDIT contenant les informations de debug
             hEdit = CreateWindow("EDIT",
                                "",
                                WS_CHILD | WS_VISIBLE | WS_VSCROLL | 
                                ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 
                                0, 0, WIN_WIDTH-12, WIN_HEIGHT,
                                hwnd, NULL,
                                (HINSTANCE)GetModuleHandle(NULL),
                                NULL);
     
             //recuperation du HDC associe au controle que l'on vient de creer
             hdcEdit = GetDC(hEdit);
             break;
    rien n'y fait. Je ne dois pas avoir le bon HDC. J'ai essayé au cas où en recuperant le HDC de la fenetre parente, mais ça ne fonctionne pas non plus.
    La ligne printf("hdc : %x\n",hdcEdit); m'affiche bien une valeur, je recupere donc bien un HDC valide.

    Merci pour votre aide.

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 147
    Par défaut
    Pourquoi tu ne surcharges pas CEdit et la fonction DrawItem ?

  3. #3
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2010
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Mai 2010
    Messages : 248
    Par défaut
    Le plus simple est d'hériter de CEdit.

    Voici un code que j'utilise et qui fonctionne.
    header:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    typedef enum eBkColor {	
    				BK_WHITE		= 0xFFFFFF,
    				BK_BLACK		= 0x000000,
    				BK_RED			= 0xFF0000,
    				BK_GREEN		= 0x00FF00,
    				BK_BLUE			= 0x0000FF
    				};
     
    class CCustomEdit : public CEdit
    {
    	DECLARE_DYNAMIC(CCustomEdit)
     
    public:
    	CCustomEdit();
    	virtual ~CCustomEdit();
     
    	/// \brief Change the background color
    	/// \param[in] New background color to be applied. format (hexa) : 0x00RRGGBB
    	/// Blue        : byte 0        
    	/// Green       : byte 1
    	/// Red         : byte 2        
    	/// byte 3      : unused        
    	void ChangeBKColor(UINT bkColor);
     
    	/// \brief Change the text color
    	/// \param[in] New text color to be applied. format (hexa) : 0x00RRGGBB
    	/// Blue        : byte 0        
    	/// Green       : byte 1
    	/// Red         : byte 2        
    	/// byte 3      : unused        
    	void ChangeTextColor(UINT txtColor);
     
     
    protected:
    	DECLARE_MESSAGE_MAP()
     
    	//Set the background color
    	HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
     
    	/// \brief Current background color
    	UINT m_bkColor;
    	/// \brief Current text color
    	UINT m_txtColor;
    	/// \brief Flag indicating the color should be changed
    	BOOL m_fChangeColor;
     
    	//Background brushes.
     
    	/// \brief Brush used for set the background to white color
    	CBrush* m_pBkBrush;
     
     
    };
    source:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    // CustomEdit.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "CustomEdit.h"
    // CCustomEdit
     
    IMPLEMENT_DYNAMIC(CCustomEdit, CEdit)
     
    CCustomEdit::CCustomEdit()
    {
    	//Set background to white
    	m_bkColor = BK_WHITE;
    	m_pBkBrush = new CBrush((COLORREF)m_bkColor); 
    	m_fChangeColor = FALSE;	
    }
     
    CCustomEdit::~CCustomEdit()
    {
    	delete m_pBkBrush;
    }
     
     
    BEGIN_MESSAGE_MAP(CCustomEdit, CEdit)
    //{{AFX_MSG_MAP(CCustomEdit)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
     
     
    //Change the color and call RedrawWindow which will call CtlColor.
    void CCustomEdit::ChangeBKColor(UINT bkColor)
    {
    	//Change indianess
    	m_bkColor = ((bkColor & 0x00FF0000) >> 16) | ((bkColor & 0x000000FF) << 16) | (bkColor & 0x0000FF00);
    	m_fChangeColor = TRUE;
    	this->RedrawWindow();
    }
     
    void CCustomEdit::ChangeTextColor(UINT txtColor)
    {
    	m_txtColor = ((txtColor & 0x00FF0000) >> 16) | ((txtColor & 0x000000FF) << 16) | (txtColor & 0x0000FF00);
    	this->RedrawWindow();
    }
     
    //Set the background color of the edit box
    HBRUSH CCustomEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    // TODO: Change any attributes of the DC here
     
    	// Create the brush with the new color
    	if(m_fChangeColor)
    	{
    		m_pBkBrush->DeleteObject();
    		m_pBkBrush->CreateSolidBrush((COLORREF) m_bkColor);
    	}
     
        pDC->SetBkColor((COLORREF)m_bkColor);
    	pDC->SetTextColor((COLORREF) m_txtColor);
    	UpdateData(1);
     
        return (HBRUSH)m_pBkBrush->GetSafeHandle();	
     
    }
    Exemple d'utilisation :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    m_customEdit.ChangeTextColor(BK_RED);

Discussions similaires

  1. Probleme pour changer la couleur de mon widget.
    Par Flow_75 dans le forum GTK+ avec C & C++
    Réponses: 0
    Dernier message: 18/09/2009, 21h01
  2. Réponses: 3
    Dernier message: 06/03/2008, 15h48
  3. Réponses: 3
    Dernier message: 01/06/2006, 09h49
  4. Réponses: 2
    Dernier message: 03/02/2005, 23h42
  5. Changer la couleur du texte lors passage souris sur un TD !
    Par Kokito dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 10/01/2005, 15h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo