|
-
March 18th, 2009, 12:11 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|