CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    Resizing CComboBoxEx object programatically

    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?
    mpliam

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Resizing CComboBoxEx object programatically

    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.

  3. #3
    Join Date
    May 2002
    Posts
    1,798

    Re: Resizing CComboBoxEx object programatically

    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();
    	}
    }
    Attached Files Attached Files
    mpliam

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Resizing CComboBoxEx object programatically

    (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?
    Code:
    pComboBox->MoveWindow(r.left, r.top, cx-6, r.Height()-3, TRUE);
    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.

  5. #5
    Join Date
    May 2002
    Posts
    1,798

    Re: Resizing CComboBoxEx object programatically

    dc_2000

    Thanks very much for your great help.

    This works:
    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 rb;	// dialog bar rect
    	CRect rc;	// combo box rect
    
    	if(m_wndDlgBar1)		// resize the dialog bar comboboxex window if visible
    	{
    		m_wndDlgBar1.GetWindowRect(&rb);
    		m_wndDlgBar1.ScreenToClient(&rb);
    		pComboBox->GetWindowRect(&rc);
    		pComboBox->MoveWindow(rb.left, rb.top, cx-6, rc.Height(), TRUE);
    	}
    }
    Sure appreciate your help.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured