views:

636

answers:

1

Hi,

I am developing an application for pocket PC which should run in landscape mode.

I wrote the function SetScreenOrientation(int angle), which rotates the screen. This function is called on application start and on application close. I want to change the screen orientation when I minimize/maximize orientation as well. To do this I edited the following function:

void CMainFrame::OnSize(UINT nType, int cx, int cy)

{ RECT r; GetWindowRect(&r);

RECT rstatus;
rstatus.left = 0;
rstatus.top = 0;
rstatus.right = r.right;
rstatus.bottom = TOOLBAR_HEIGHT;
m_wndStatus.MoveWindow(&rstatus, TRUE);

RECT rcamera;
rcamera.left = 0;
rcamera.top = 0;
rcamera.right = r.right;
rcamera.bottom = r.bottom - TOOLBAR_HEIGHT;
m_wndCameraView.MoveWindow(&rcamera, TRUE);

if(nType == SIZE_MAXIMIZED)
{
    CScreenOrientation::SetScreenOrientation(270);
}
if(nType == SIZE_MINIMIZED)
{
    CScreenOrientation::SetScreenOrientation(0);
}

}

The problem is that when I minimize the application the function is executed more than once so the screen first rotates back to 0 degrees and then it rotates to 270 degrees.

While debugging I can see that the second time the function is executed the following piece of wincore code is executed:

BOOL CWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)

{ ... switch (lpEntry->nSig) { ... case AfxSig_v_u_ii: (this->*mmf.pfn_v_u_i_i)(static_cast(wParam), LOWORD(lParam), HIWORD(lParam)); break; ... } }

Does anyone know any other way to set the screen orientation on application minimize/maximize or any trick that could prevent multiple function execution?

Thanks!

Niko

+1  A: 

For one thing, it seems likely that SetScreenOrientation is going to give you another OnSize notification, so you want to detect recursive calls and do nothing when that happens.

More importantly, how do you know what orientation the user really wants? When your application starts up you can check the orientation and save that. But if the user changed the orientation while you happened to be running, they won't be happy when you change it back. Maybe you can check notifications of system settings changes and detect if the user changed the orientation themselves.

Windows programmer