Udostępnij za pośrednictwem


How to: Handle Orientation and Resolution Changes

The following table shows the Pocket PC and Smartphone screen orientations supported by the .NET Compact Framework. Dimensions are in pixels, width by height.

Screen Orientation

96 DPI Typical Dimensions

192 DPI (High Resolution) Typical Dimensions

Portrait - Pocket PC

Portrait - Smartphone

240 x 320

176 x 220, and 240 x 320

480 x 640

352 x 440, and 480 x 640

Landscape - Pocket PC only

320 x 240

640 x 480

Square - Pocket PC only

240 x 240

480 x 480

You can use anchoring and docking to automatically resize controls so that form contents adapt to different screen orientations. Anchoring maintains a specified distance from an edge, whereas docking snaps to an edge of the parent container.

A control's Anchor property determines its resizing behavior when its containing control or form is resized. A control's Dock property specifies which edges of its containing control to adhere to.

Anchoring and docking in the .NET Compact Framework has the same behavior as in the full .NET Framework.

You can customize your application to adapt to different DPI (dots per inch) pixel resolution settings.

To handle a screen orientation change

  • You can put controls that should be in a particular area of the form in a container control, such as a Panel, and then set the Dock property of the Panel to the desired edge.

  • To maintain the proper size and location of a control in relation to the sides of its form, set the Anchor property on the controls to the desired location.

    For example, to have a button always appear at the lower right corner and maintain its size, use the following statement:

    Me.Button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
    
    this.button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    

In Microsoft Visual Studio 2005, you can set anchoring and docking settings in the Properties pane.

To change the screen orientation

  • If your Pocket PC is running Windows Mobile version 5.0, you can change the screen orientation from portrait at zero degrees to 90, 180, and 270 degrees as specified by the ScreenOrientation enumeration. For example, the following statement sets a landscape orientation:

    SystemSettings.ScreenOrientation = ScreenOrientation.Angle270
    
    SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;
    

    Note that the screen orientation changes the device, not just the application. Therefore, a good practice would be to set the screen orientation back to its original setting in the event handling code for the FormClosing event.

To handle a change in screen resolution

  • When you create a smart device project in Microsoft Visual Studio 2005, the designer provides code to automatically scale controls appropriately for the device's screen resolution; otherwise, if your application is run on a device with a different DPI (dots per inch) resolution than the device used for its development, the form will appear either too larger or too small. Consequently, the controls on the form must be scaled appropriately.

    When you create a smart device project in Visual Studio 2005, the designer adds the following statements in the InitializeComponent method:

    [Visual Basic]

    Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0!, 96.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
    

    [C#]

    this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
    

    These statements show an application being developed for a device with a 96 DPI resolution and that automatic scaling be performed using the DPI mode. Note that 96 DPI is the default value obtained from the desktop computer running Visual Studio 2005. The designer automatically generates this code, and the controls on your form will automatically scale to handle different DPI settings.

    If your application contains graphics that are drawn in the OnPaint method, they will not scale automatically. You will need to use the DpiX and DpiY properties of your Graphics objects to determine appropriate scaling. The Rotated Text Using LogFont Sample shows an example of manual scaling.

See Also

Tasks

How to: Align a Control to the Edges of Forms

Concepts

Automatic Scaling in Windows Forms

Other Resources

Windows Forms Controls in the .NET Compact Framework