共用方式為


GraphicsDevice.Viewport Property

Gets or sets a viewport identifying the portion of the render target to receive draw calls.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

Syntax

public Viewport Viewport { get; set; }

Property Value

The viewport to set or get.

Remarks

Viewport can be used to draw on part of the screen. It should be set before any geometry is drawn so the viewport parameters will take effect.

To draw multiple views within a scene, repeat setting Viewport and draw a geometry sequence for each view.

Example

This code sample, from How to: Use Viewports for Split Screen Gaming, demonstrates how to use the Viewport property to display different scenes to different parts of the screen.

Viewport defaultViewport;
Viewport leftViewport;
Viewport rightViewport;
protected override void LoadGraphicsContent( bool loadAllContent )
{
    if (loadAllContent)
    {
        ...
    }

    defaultViewport = graphics.GraphicsDevice.Viewport;
    leftViewport = defaultViewport;
    rightViewport = defaultViewport;
    leftViewport.Width = leftViewport.Width / 2;
    rightViewport.Width = rightViewport.Width / 2;
    rightViewport.X = leftViewport.Width + 1;

    projectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, 4.0f / 3.0f, 1.0f, 10000f );
    halfprojectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, 2.0f / 3.0f, 1.0f, 10000f );

}
protected override void Draw( GameTime gameTime )
{
    graphics.GraphicsDevice.Clear( Color.Black );

    // TODO: Add your drawing code here
    graphics.GraphicsDevice.Viewport = leftViewport;

    DrawScene( gameTime, Camera1, halfprojectionMatrix);
    graphics.GraphicsDevice.Viewport = rightViewport;

    DrawScene( gameTime, Camera2, halfprojectionMatrix );

    base.Draw( gameTime );
}

See Also

Tasks

How to: Use Viewports for Split Screen Gaming

Concepts

Displays, Client Bounds, Viewports, and Back Buffers

Reference

GraphicsDevice Class
GraphicsDevice Members
Microsoft.Xna.Framework.Graphics Namespace

Platforms

Xbox 360, Windows XP SP2, Windows Vista