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:
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:
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.