Thursday, December 18, 2008

Modify the width and height of the drop down list in a combo box

In MFC, you can simply subclass the combo-box and override the WM_CTLCOLOR message handler. This handler is used because Windows sends a message to set the colors for each of the children of the combo (the edit and the list-box), and you can easily grab the list-box HWND at this time.

HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

if (nCtlColor == CTLCOLOR_LISTBOX)

{

HWND hwndList = pWnd->GetSafeHwnd() ;

if ( hwndList != NULL )

{

CRect rectList ;

::GetWindowRect( hwndList, rectList ) ;

::MoveWindow( hwndList, rectList.left, rectList.top,

(rectList.right - rectList.left + EXTRA_WIDTH),

rectList.bottom - rectList.top + EXTRA_HEIGHT, TRUE ) ;

}

}

return CComboBox::OnCtlColor( pDC, pWnd, nCtlColor ) ;

}

No comments: