views:

827

answers:

3

Hi, I have a Windows Mobile application (written in C#) that only works on portrait mode. Is there anyway programmatically to prevent the system from automatically rotating my application to landscape mode without having to change Windows Mobile system settings?

Platform: Windows Mobile 6.1 Professional

+1  A: 

No, and even if there were it would involve hooks, which is a really, really bad idea on an embedded device.

But you can change the screen orientation to whatever you like, since you obviously know what it should be. See Developing Screen Orientation-Aware Applications -> Changing Screen Orientation for more details.

Edit: Just realized you're stuck with C# / .NETCF. Don't know that part of the framework, but I'll leave the answer here for reference, perhaps it will point you in the right direction. The concepts explained in tat article are still valid, of course.

Mihai Limbășan
A: 

What version of windows mobile?

Is your application going to be only used by a controlled group of users (e.g. an internal company application) where the hardware can be controlled?

Windows mobile supports different screen sizes and orientations. For example a device may be designed with a portrait, square or landscape screen. Your software being designed to only run in portrait mode could cause a problem if you are not controlling the hardware that runs it.

If you do want to set portrait mode you can do so by calling

SystemSettings.ScreenOrientation = ScreenOrientation.Angle0;

This will set the whole device to be in portrait mode. If you do chose to go ahead with this I'd recommend setting the orientation in your form activated event and checking the orientation in the resize event. This will reset the device back to portrait when your application gets focus if anyone changes it to landscape.

It may also be useful to store the initial orientation when your application starts and re-set the device to this when your application exits.

Stevo3000
I use Windows Mobile 6.1 Professional and it will be used only with Pocket-PCs.
Mohammadreza
Your solution will work if my application loads after the orientation change but what if the orientation changes when my app is active?
Mohammadreza
@Mohammadreza - As long as changing the orientation requres input from the user (e.g. navigating away from your form) then the form activated event will fire when your application gets focus. I still think that forcing the user to use portrait may well not work correctly in the real world.
Stevo3000
What if the device can rotate when the user rotates the phone itself? And also what do you mean by not work correctly? Does it lead to a bug or something?
Mohammadreza
@Mohammadreza - As stated in my answer the device may have a square screen or a screen that is natively landscape (with qwerty buttons below it). Forcing portrait mode is a hack and is not a wise choice. You should get a re-size event when the screen orientation changes.
Stevo3000
This is not a problem for me cause my application is designed for a specific purpose. (I should have said it before)
Mohammadreza
@Mohammadreza - Well if ur sure that this is what you want then I'd advise you to follow my answer using the form activated and re-size events to force the screen orientation.
Stevo3000
He'll still get an ugly screen flicker as it changes to landscape and back. I'm with you that he needs to do it right, not hack it.
ctacke
@ctacke - It would be best if the device just showed a lable when the screen when to landscape saying that the application would only function in portrait? Not ideal, but better than forcing the orientation.
Stevo3000
+4  A: 

This is a bad, bad, bad idea. The device is a general purpose device intended to run multiple applications. The platform should not be a slave to your application. That's like saying that you have a desktop application that only runs at 640x480, so you should be able to lock someone's PC to that resolution.

The better solution is to either actually fix your software (gasp) to support both orientations (and you are aware that there are also different resolutions as well as square-screen devices, right?) or to have your app at least pop up some sort of message box to inform the user about compatibility when an unsupported resolution or orientation is selected.

As for the "how" to do it - you can't. Rotation is handled by the display driver, specifically by calling ExtEscape with DRVESC_SETSCREENROTATION. In the standard GPEFlat driver that ships with PB, this in turn calls DynRotate and goes on to call SetRotation (you can look at the public source in the eval edition is you really want to know how it all works).

The short of this is that to intercept that call, you'd have to either create a replacement display driver, or at least some shim that you insert as the driver that in turns calls the existing one. Neither of these are going to happen in managed code.

ctacke
I agree that this is a bad idea, but you do get a resize event when the screen orientation changes. In my own application I allow the user to set their orientation quickly from the menu and i use the resize event to change the available menu items.
Stevo3000
I agree too but not if your application is designed for a specific purpose. Anyways I think the SystemSettings.ScreenOrientation that Stevo3000 mentioned is a good choice but is there anyways to find out when the rotation is happened like an event or something?
Mohammadreza
If by "designed for a specific purpose" you mean it's a kiosk app and nothing else will run, then how is it that the orientation is changing? If it's from some hardware interrupt (like a slide-out keyboard) then I still maintain that you should make the app support the rotated orientation.
ctacke
@Mohammadreza - As I've said before, the event that fires when the screen orientation changes is the resize event!
Stevo3000