CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Posts
    11

    Automatically Resize CRichEditCtrl and Dialog

    Hi,
    I have a Dialog, and a CRichEditCtrl inside it. I set a string (which can be multiple lines) to the CRichEditCtrl programmatically (setWindowText).
    What I need is, how to make the CRichEditCtrl and Dialog (because the CRichEditCtrl takes up whole space in the Dialog) resize automatically depending on the size of the shown string so that the string fits into CRichEditCtrl nicely without any need of scrollbars.
    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automatically Resize CRichEditCtrl and Dialog

    APIs ::MoveWindow or::SetWindowPos as well as their wrappers CWnd::MoveWindow or CWnd::SetWindowPos work for any window. You only have to obtain the rect size your string needs to.
    Last edited by VictorN; May 15th, 2007 at 10:08 AM.

  3. #3
    Join Date
    Jun 2006
    Posts
    11

    Re: Automatically Resize CRichEditCtrl and Dialog

    Hmm...thanks, but how to obtain the rect size of the multiline string ?

  4. #4
    Join Date
    Aug 2001
    Posts
    507

    Re: Automatically Resize CRichEditCtrl and Dialog

    here you go

    Code:
    void CClientDlg::OnSize(UINT nType, int cx, int cy) 
    {
    	
    		CDialog::OnSize(nType, cx, cy);
    		
    		// TODO: Add your message handler code here
    		
    		static CRect rectClient;	// Client Rectangle
    		
    		// First child window
    		CWnd* pWnd = GetWindow(GW_CHILD);
    		
    		// Runs loop to take into account of every child window
    		while (pWnd)
    		{
    			// Gets control rectangle in window cordinates with respect to dialog
    			CRect rectCtrl;
    			pWnd->GetWindowRect(rectCtrl);
    			ScreenToClient(rectCtrl);
    			
    			// Calculates scale factors for resizing
    			double xScale = 1.0 * cx / rectClient.Width();
    			double yScale = 1.0 * cy / rectClient.Height();
    			
    			// Calculates scaled control size
    			rectCtrl.left = (int) (rectCtrl.left * xScale);
    			rectCtrl.top = (int) (rectCtrl.top * yScale);
    			rectCtrl.right = (int) (rectCtrl.right * xScale);
    			rectCtrl.bottom = (int) (rectCtrl.bottom * yScale);
    			
    			// Sizes the control
    			pWnd->MoveWindow(rectCtrl);
    			
    			// Searches for next child window
    			pWnd = pWnd->GetWindow(GW_HWNDNEXT);
    			
    		}
    		
    		// Stores client rectangle size for next sizing
    		rectClient = CRect(0, 0, cx, cy);
    	
    	
    	
    	// TODO: Add your message handler code here
    	
    }
    You just implement the OnSize handler, and this is what it should look like.

    now keep putting as many controls as u want on it.

    Enjoyy
    If you found my reply to be useful, please dont hesitate to rate it.


    DO NOT kick the Axe if it doesnt fall on your foot.

    Salman

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Automatically Resize CRichEditCtrl and Dialog

    Also take a look at EasySize .

  6. #6
    Join Date
    Jun 2006
    Posts
    11

    Re: Automatically Resize CRichEditCtrl and Dialog

    Hmm...thanks guy, maybe I should say like this :
    given font settings and a string :"Space, the Final Frontier.\nThese are the voyages of starship Enterprise...". Then, the Richedit together with Dialog will resize according to the total size of the string. Maybe what I need is, how to calculcate total size (in pixels ?) of the string ?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Automatically Resize CRichEditCtrl and Dialog

    Have you tried the CDC:: DrawText (or API DrawText) with DT_CALCRECT flag?

  8. #8
    Join Date
    Jun 2006
    Posts
    11

    Re: Automatically Resize CRichEditCtrl and Dialog

    Thanks, that DrawText and MoveWindow do the trick

    And, I have another question...my program is running in the background...How can I get the topleft coordinate and width/height of the current active window ?

    Using GetSystemMetrics I can only get the width/height of the whole screen, but not the active window...

    Thanks

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Automatically Resize CRichEditCtrl and Dialog

    use ::GetActiveWindow() to get the handle of your currently active Window and can use GetWindowRect() method

    Thanx

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