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 :

Problème de récupération de l'event DropDown d'une CComboBox


Sujet :

MFC

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2008
    Messages : 54
    Par défaut Problème de récupération de l'event DropDown d'une CComboBox
    Bonjour à tous,

    Je suis en train de sub-classer la classe CComboBox pour réajuster automatiquement la DropDownList à la string la plus longue. La fonction ainsi créée fonctionne très bien. Le problème se situe dans la récupération de l'event du Drop Down.

    Je vous montre les fichiers .cpp et .h

    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
    #pragma once
     
     
    // CExtComboBox
     
    class CExtComboBox : public CComboBox
    {
    	DECLARE_DYNAMIC(CExtComboBox)
     
    public:
    	CExtComboBox();
    	virtual ~CExtComboBox();
     
    protected:
    	void CorrectWidthDropDownBox();
    	DECLARE_MESSAGE_MAP()
    public:
    	virtual BOOL PreTranslateMessage(MSG* pMsg);
    };

    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    // ExtComboBox.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "Application.h"
    #include "ExtComboBox.h"
     
     
    // CExtComboBox
     
    IMPLEMENT_DYNAMIC(CExtComboBox, CComboBox)
     
    CExtComboBox::CExtComboBox()
    {
     
    }
     
    CExtComboBox::~CExtComboBox()
    {
    }
     
     
    BEGIN_MESSAGE_MAP(CExtComboBox, CComboBox)
    END_MESSAGE_MAP()
     
     
     
    // CExtComboBox message handlers
     
     
     
    BOOL CExtComboBox::PreTranslateMessage(MSG* pMsg)
    {
    	// Notify Enter key to the parent window
    	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
    	{
    		NMHDR hdr;
    		hdr.hwndFrom = GetSafeHwnd();
    		hdr.idFrom   = GetDlgCtrlID();
    		hdr.code     = WM_USER_ENTERKEYPRESSED;//WM_CHAR;
    		GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&hdr);
    	}
    	// Correct the DropDownList's width if necessary.
    	// CB_SHOWDROPDOWN this message didn't work.
    	if(pMsg->message == WM_LBUTTONDOWN)
    		CorrectWidthDropDownBox();
     
    	return CComboBox::PreTranslateMessage(pMsg);
    }
    void CExtComboBox::CorrectWidthDropDownBox()
    {
    	// Find the longest string in the combo box.
    	CString    str;
    	CSize      sz;
    	int        dx = 0;
    	TEXTMETRIC tm;
    	CDC*       pDC = GetDC();
    	CFont*     pFont = GetFont();
     
    	// Select the listbox font, save the old font
    	CFont* pOldFont = pDC->SelectObject(pFont);
    	// Get the text metrics for avg char width
    	pDC->GetTextMetrics(&tm);
     
    	for (int i = 0; i < GetCount(); i++)
    	{
    	   GetLBText(i, str);
    	   sz = pDC->GetTextExtent(str);
     
    	   // Add the avg width to prevent clipping
    	   sz.cx += tm.tmAveCharWidth;
     
    	   if (sz.cx > dx)
    		  dx = sz.cx;
    	}
    	// Select the old font back into the DC
    	pDC->SelectObject(pOldFont);
    	ReleaseDC(pDC);
     
    	// Adjust the width for the vertical scroll bar and the left and right border.
    	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);
     
    	// Set the width of the list box so that every item is completely visible.
    	SetDroppedWidth(dx);
    }
    Comme vous pouvez le remarquer, j'intercepte actuellement le message WM_LBUTTONDOWN qui fonctionne mais qui ne correspond pas à ce que j'attend...

    J'ai aussi utilisé le logiciel Spy de Microsoft pour voir les messages qui sont envoyés. Le screen shot se trouve en pièce jointe.

    Si quelqu'un a une idée, elle serait la bienvenue, merci pour toute réponse.

  2. #2
    Rédacteur
    Avatar de farscape
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2003
    Messages
    9 055
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2003
    Messages : 9 055
    Par défaut
    salut,
    pourquoi ne pas intercepter le message reflected =CBN_DROPDOWN ?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2008
    Messages : 54
    Par défaut
    Bonjour, et merci de ta réponse Farscape.

    J'ai remplacé dans le pretranslate message le WM_LBUTTONDOWN par CBN_DROPDOWN mais cela ne change rien...

    J'ai utilisé cette classe sur une combobox et j'ai demandé de rajouter la fonction event OnDropDown. En remettant le WM_LBUTTONDOWN, celui-ci est intercepté par le pretranslate message avant d'arriver dans l'event OnDropDown de la combobox. (Cela parait logique mais bon c'est juste pour info...)

    Est-ce bien à cet endroit que je dois traiter le message? Ou y aurait-il une autre issue?

    Merci d'avance pour toute réponse.

  4. #4
    Rédacteur
    Avatar de farscape
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2003
    Messages
    9 055
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2003
    Messages : 9 055
    Par défaut
    le message doit être placé avec l'assistant , sinon on se trompe ....
    le code généré ressemble à ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    BEGIN_MESSAGE_MAP(MyComboBox, CComboBox)
        ON_CONTROL_REFLECT(CBN_DROPDOWN, &MyComboBox::OnCbnDropdown)
    END_MESSAGE_MAP()
    void MyComboBox::OnCbnDropdown()
    {
        // TODO : ajoutez ici le code de votre gestionnaire de notification de contrôle
        TRACE("\ndropdown");
    }
    je suis sûr que ça fonctionne...

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    54
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2008
    Messages : 54
    Par défaut
    Super ça fonctionne !!!

    Je ne savais pas qu'on pouvait travailler de cette manière, grand merci Farscape et bonne journée.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 24/08/2009, 12h03
  2. Réponses: 14
    Dernier message: 20/11/2007, 18h28
  3. Réponses: 1
    Dernier message: 10/07/2006, 13h40
  4. [VBA-E]problème de récupération de variables et d'event
    Par zenix dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 24/04/2006, 12h53
  5. Problème de récupération de texte de formulaire
    Par bigourson dans le forum Langage
    Réponses: 4
    Dernier message: 15/09/2004, 16h27

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