Re: HWND and ScreenToClient issues.
Isn't CSmartBuddies a CWnd derived class? Why are you using SDK calls when
you can be using the CWnd methods for GetClientRect, GetWindowRect, and
GetDlgItem.
And yes there is a problem with attaching the same hwnd to two different
CWnd objects, MFC keeps tabs on which CWnd object is attached to a HWND.
You should have gotten an ASSERT inside Attach.
void CSmartBuddies::OnSize(UINT nType, int cx, int cy)
{
CPropertyPage::OnSize(nType, cx, cy);
CRect rcClient;
GetClientRect(&rcClient);
//try using a control variable attached to the control, instead of
GetDlgItem
CWnd *pWnd = GetDlgItem(ID_OF_A_CONTROL_ON_DIALOG);
CRect rcCtl;
pWnd->GetWindowRect(&rcCtl);
ScreenToClient(rcCtl);
rcCtrl.left = ((rcClient.right - rcClient.left) -
(rcCtl.right -rcCtl.left))/2;
pWnd->MoveWindow(rcCtrl.left, TRUE);
}
AliR.
"triveni" <prabhu.triveni@gmail.com> wrote in message
news:a8fc2701-999f-484f-b226-ec9f3802b5d5@m36g2000hse.googlegroups.com...
Hi,
I am writing an application for Pocket PC handset. I have few controls
on my PropertyPage. I am handling the WM_SIZE event to adjust the
controls to center of the display area on the screen, so that even if
the screen size changes ( by handset screen rotate), the controls are
always in the center of the screen.
Here is what i do:
void CSmartBuddies::OnSize(UINT nType, int cx, int cy)
{
CPropertyPage::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
RECT rcClient;
::GetClientRect(this->m_hWnd, &rcClient);
HWND hwndCtl = ::GetDlgItem(this->m_hWnd, ID_OF_A_CONTROL_ON_DIALOG);
RECT rcCtl;
::GetWindowRect(hwndCtl, &rcCtl);
int nLeft, nTop, nWidth, nHeight;
nLeft = ((rcClient.right - rcClient.left) - (rcCtl.right -
rcCtl.left))/2;
nWidth = rcCtl.right - rcCtl.left;
nHeight = rcCtl.bottom - rcCtl.top;
CWnd mywnd; // Line 1
mywnd.Attach(this->m_hWnd); // Line 2
mywnd.ScreenToClient(rcCtl); // Line 3
nTop = rcCtl.top ; // PROBLEM HERE
hwndDlg = myWnd.Detach(); // Line 4
::MoveWindow(hwndCtl, nLeft, nTop, nWidth, nHeight, TRUE);
}
Now, If I remove lines "Line 1, 2, 3, 4" and hardcode nTop to some
value like 15, then the control is in centre of the dialog and
everything is fine (for control ID_OF_A_CONTROL_ON_DIALOG) .
I checked with rcCtl.top in line "Problem here" after adding "Line
1, 2, 3, 4". Its value is also 15. But, The page does not appear
correctly. Control is not in center either. Controls of other property
pages are overlapping over this page. Can anybody please explain what
I should do so that it works fine. Is there something wrong with
Attach() and Detach() code.
Thanks in advance,
Regards,
Triveni Prabhu