Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]WM_SIZE message problem[/b]

Status
Not open for further replies.

JumboGeng

Programmer
Jul 19, 2005
5
CN
Hi:
i just begin to learn MFC,and i got a problem about WM_SIZE message.i start a new VC6 MFC Wizard project ,use all the defaul setting of the App wizard,except it is a single doc project,and the base class of the CTestView class is CFormView instead of CView.after the frame work is constructed ,i add a control to the form view,whatever control,say a button,whose id is IDC_BTN_TEST.i'd like to keep the size of the button the same with the client rect of the view,then i add the following code to the view's OnSize message handler.
Code:
void CTestView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	CWnd *pButton;
	RECT rect;

	pButton=GetDlgItem(IDC_BTN_TEST);
	GetClientRect(&rect);
	pButton->MoveWindow(&rect);
}

there is no compile error or warning ,but when i run it ,it crashes.

but when i change the code like fowlling it works .
Code:
void CTestView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	CWnd *pButton;
	RECT rect;
	static int iFlag=0;
	
	if(iFlag>2){
		pButton=GetDlgItem(IDC_BTN_TEST);
		GetClientRect(&rect);
		pButton->MoveWindow(&rect);
	}
}

it seems on the first several call of the handler,the buttton control is not constructed or valid yet.but i don't know the exactly reason,any one can help me ,thanks in advance.
 
sorry ,correct a mistake in the above post,
the second part code is not right ,
the corrected version is here:
Code:
void CTestView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	CWnd *pButton;
	RECT rect;
	static int iFlag=0;
	
	if(iFlag>2){
		pButton=GetDlgItem(IDC_BTN_TEST);
		GetClientRect(&rect);
		pButton->MoveWindow(&rect);
	}
        //miss this line ,incremet the flag
	iFlag++;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top