CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    10

    Question can someone help - OnSize and SetWindowPos?

    hello im new at visual C++, im asking for help here...
    i got problem
    bellow is the code, CForm1 is derived CFormView

    void CForm1::OnSize(UINT nType, int cx, int cy)
    {
    CRect RectEDIT, RectRICHEDIT;
    if(GetDlgItem(IDC_RICHEDIT)->GetSafeHwnd() &&
    GetDlgItem(IDC_EDIT)->GetSafeHwnd() )
    {
    GetDlgItem(IDC_EDIT)->GetWindowRect(&RectEDIT);
    ScreenToClient(&RectEDIT);

    GetDlgItem(IDC_RICHEDIT)->GetWindowRect(&RectRICHEDIT);
    ScreenToClient(&RectRICHEDIT);

    GetDlgItem(IDC_EDIT)->SetWindowPos(NULL ,
    0,
    RectRICHEDIT.bottom,
    cx - RectEDIT.left - 5 , RectEDIT.Height(),
    NULL );

    GetDlgItem(IDC_RICHEDIT)->SetWindowPos(NULL,
    0,
    0,
    cx - RectRICHEDIT.left - 5 ,
    cy - RectEDIT.Height() - 3 ,
    NULL );
    }
    }

    im using SDI with splitter

    the situation is like this,

    when the application start, the EDIT is placed in front of RICHEDIT
    when i resize the form, the EDIT and RICHEDIT is placed normally where i wanted to (bellow RICHEDIT)

    is there any wrong in the codes or missing something - to make the position of EDIT is bellow of RICHEDIT at start?

    please can somebody help me?
    thx for any opinion and forgive my bad english ^^

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: can someone help - OnSize and SetWindowPos?

    (Please use CODE tags).

    Just call OnSize() from OnInitDialog(), or better yet, make a new function and call it once in OnInitDIalog(), and call it from OnSize() whenever OnSize() is called.

  3. #3
    Join Date
    Mar 2009
    Posts
    10

    Question Re: can someone help - OnSize and SetWindowPos?

    sorry i can't find OnInitDialog(), is it because im using CFormView?

    i tried like this

    Code:
    void CForm1::OnInitialUpdate()
    {
    	CFormView::OnInitialUpdate();
    	CRect RForm;
    	this->GetWindowRect(&RForm);
    	OnSize(WM_SIZE, RForm.Width(),RForm.Height());
    }
    is it right? because it doesn't work anyway T-T, or is it have something to do with GetSafeHwnd()?
    Last edited by astoraiti; March 17th, 2009 at 11:27 PM.

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: can someone help - OnSize and SetWindowPos?

    DO NOT call OnSize directly
    It is a message handler for handling WM_SIZE message. It will not do any harm in this case but it creates a habbit of calling handlers and you can do harm doing that.

    CFormView class does not have OnInitDialog. OnInitDialog is also a part of a message handler but is handled differently in a CDialog class and CFormView class.

    Your algorithm is based on original position of the control in a resource editor. Use dimentions that are delivered by system to a OnSize handler. You should change code to something like that:

    Code:
    	CFormView::OnSize(nType, cx, cy);
    	
    	CWnd *pEditWnd = GetDlgItem(IDC_EDIT);
    
    	CWnd *pREditWnd = GetDlgItem(IDC_RICHEDIT);
    	
    	if(IsWindow(*pEditWnd) && IsWindow(*pREditWnd))
    	{
    		int iHeightRatio = cy / 3;
    		pREditWnd->SetWindowPos(NULL,
    			5, 
    			5,
    			cx - 10 , 
    			cy / 3 , 
    			SWP_NOZORDER );
    		
    		
    		pREditWnd->GetWindowRect(RectRICHEDIT);
    		ScreenToClient(&RectRICHEDIT);
    		
    		
    		pEditWnd->SetWindowPos(NULL, 
    			5, 
    			iHeightRatio + 3,
    			cx - 10 , 
    			cy - iHeightRatio - 13,
    			SWP_NOZORDER);
    		
    	}
    Above will position both windows with margins of 5 pixels from each side of the splitter.

    As you can see, I have changed parts of your code:

    Do not pass NULL to a SetWindowPos. At least pass SWP_NOZORDER, if you want to move and resize controls. In general: use flags to tell SetWindowPos what you want to do.

    Do not use GetDlgItem(IDC_EDIT)->CallSomeFunction. You do not know if GetDlgItem will return a valid pointer. If not, you are cooked; your application will crash. Also, you are calling function three times while assigning return value to a local variable requires only one call.

    You mau be better off using BeginDeferWindowPos , DeferWindowPos, EndDeferWindowPos:
    Code:
    	CFormView::OnSize(nType, cx, cy);
    	
    	CWnd *pEditWnd = GetDlgItem(IDC_EDIT);
    	CWnd *pREditWnd = GetDlgItem(IDC_RICHEDIT);
    
    	if(IsWindow(*pEditWnd) && IsWindow(*pREditWnd))
    	{
    		HDWP hDwp = BeginDeferWindowPos(2);
    		
    		int iHeightRatio = cy / 3;
    
    		hDwp = DeferWindowPos(hDwp, *pEditWnd, NULL, 
    					5, 
    					5,
    					cx - 10, 
    					cy / 3,
    					SWP_NOZORDER);
    		
    		
    		hDwp = DeferWindowPos(hDwp, *pREditWnd, NULL, 
    					5, 
    					iHeightRatio + 5,
    					cx - 10 , 
    					cy - iHeightRatio - 15,
    					SWP_NOZORDER );
    
    		// move all at once
    		EndDeferWindowPos(hDwp);
    		
    	}
    You may want to change calculations as you need but general idea is there.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Mar 2009
    Posts
    10

    Re: can someone help - OnSize and SetWindowPos?

    yes that's worked

    thanx again Johncz ^^

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: can someone help - OnSize and SetWindowPos?

    You are very welcome.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

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