I wish to programatically resize a CComboBoxEx object.
Unfortunately, there are very few posts on this issue and none provide a satisfactory solution.
I should note that my requirement is to resize the entire CComboBoxEx object, much as one would resize a CEdit text box, that is displayed in a dialog bar that resizes with CMainFrame::OnSize. If I don't try to resize the CComboBoxEx window, it works just fine. But all of my attempts to resize it have resulted in it blanking out.
Has anyone encountered this problem and come up with a solution?
The combo box common control object cannot be resized vertically, it can only be resized horizontally. For that you use a simple MoveWindow()-type API. If you need to use a combo box that is resizable on all 2 axis, then you need to either make it yourself (which, I understand, is not quite easy) or use any of the ActiveX-type combo boxes.
To demonstrate the problem that I am having, I have attached an example application.
Notice that the dialog bar resizes just fine, but when I try to use the following code, the ComboBoxEx component fails to work properly, even though it resizes. Note also that this same code, when applied to a CEdit component, works as it should, resizing both the dialog bar and the CEdit component.
Code:
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
CComboBoxEx * pComboBox;
if(m_wndDlgBar1)
pComboBox = (CComboBoxEx*)m_wndDlgBar1.GetDlgItem(IDC_COMBOBOXEX1);
CRect r;
if(m_wndDlgBar1) // resize the dialog bar comboboxex window if visible
{
m_wndDlgBar1.GetWindowRect(&r);
pComboBox->MoveWindow(r.TopLeft().x, r.TopLeft().y, cx-6, r.Height()-3, TRUE);
pComboBox->CenterWindow();
RecalcLayout();
}
}
(1) You need to convert 'r' from screen to client coordinates before sending it to MoveWindow():
Code:
m_wndDlgBar1.ScreenToClient(&r);
Did you step into it with a debugger to see that the coordinates in 'r' rect are way off?
(2) What is the point of calling?
Code:
pComboBox->CenterWindow();
That messes you up, so remove it.
(3) This is not an error, but why are you doing r.TopLeft().x and r.TopLeft().y? Wouldn't it be faster to just use rectangle points instead of converting them to points?
So, save yourself a step. Also highlighted part above is not going to work. As I told you before, you cannot change the height of the combo box. So replace it with it's real height. To get it call pComboBox->GetWindowRect() before.
(4) I don't think you need to call RecalcLayout() for just resizing your combo box.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.