XNA 实时切换屏幕全屏、窗口

 

XNA按F键全屏和窗口的转换

 
 
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Key1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        // Constructor
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);

            graphics.PreferredBackBufferWidth = 640;    // Sets the screen size to 640x480
            graphics.PreferredBackBufferHeight = 480;

            // Here we would like switch the device to full screen mode if the user
            // set the Solution configurations to Release mode
#if !DEBUG
            // The IsFullScreen property can be used to Get or Set a value that indicates
            // whether the device should start in full screen mode.
            graphics.IsFullScreen = true;
#endif

            // This property is FALSE by default and will not allow you to change the window size.
            // Setting it will allow you to change windows size.
            Window.AllowUserResizing = true;
            // Setting up an event handler to monitor changed window size
            Window.ClientSizeChanged += new System.EventHandler(UpdateFramebufferSize);
        }


        protected override void Initialize()
        {
            base.Initialize();
        }


        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
            }
        }


        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                content.Unload();
            }
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the default game to exit on Xbox 360 and Windows
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // KeyboardState - Represents a state of keystrokes recorded by a keyboard input device.
            // Keyboard - A class dedicated to retrieve keystrokes fron the keyboard input device.
            // Keys - An enumeration to Identify keys on the keyboard.
            KeyboardState kState = Keyboard.GetState(); //Gets the current keyboard state.
            if (kState.IsKeyDown(Keys.F))
            {
                // The GraphicsDeviceManager class has a built in function called
                // ToggleFullScreen that will handle
                graphics.ToggleFullScreen();
            }
            base.Update(gameTime);
        }


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

        /// <summary>
        /// UDF -
        /// This is called when the game window is resized. It sets the backbuffer
        /// width and height to that of the windows client area size whenever the
        /// window is resized by the user.
        /// </summary>
        /// <param name="sender">contains the details about the sender that called the event handler.</param>
        /// <param name="e">parameter that will contain event data.</param>
        void UpdateFramebufferSize(object sender, System.EventArgs e)
        {
            graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
            graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值