⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxmutsettingsdlg.cs

📁 运用directX完成的坦克游戏雏形
💻 CS
📖 第 1 页 / 共 3 页
字号:
//--------------------------------------------------------------------------------------
// File: DXMUTSettingsDlg.cs
//
// Dialog for selection of device settings 
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Microsoft.Samples.DirectX.UtilityToolkit
{
    #region Control Ids
    public enum SettingsDialogControlIds
    {
        Static = -1,
        None,
        OK,
        Cancel,
        Adapter,
        DeviceType,
        Windowed,
        Fullscreen,
        AdapterFormat,
        AdapterFormatLabel,
        Resolution,
        ResolutionLabel,
        RefreshRate,
        RefreshRateLabel,
        BackBufferFormat,
        DepthStencil,
        MultisampleType,
        MultisampleQuality,
        VertexProcessing,
        PresentInterval,
        DeviceClip,
        RadioButtonGroup = 0x100,
    }
    #endregion

    /// <summary>
    /// Dialog for selection of device settings 
    /// </summary>
    public class SettingsDialog
    {
        #region Creation
        /// <summary>Creates a new settings dialog</summary>
        public SettingsDialog(Framework sample) 
        {
            parent = sample;
            windowWidth = Framework.DefaultSizeWidth; windowHeight = Framework.DefaultSizeHeight;
            CreateControls();
        } 
        #endregion

        #region Class Data
        private Framework parent; // Parent framework for this dialog
        private Dialog dialog; // Dialog that will be rendered
        private uint windowWidth; // Width of window
        private uint windowHeight; // Height of window
        private DeviceSettings globalSettings; // Device settings
        private StateBlock state; // state block for device
        #endregion

        #region Control variables
        // Combo boxes
        private ComboBox resolution;
        private ComboBox adapterCombo;
        private ComboBox deviceCombo;
        private ComboBox adapterFormatCombo;
        private ComboBox refreshCombo;
        private ComboBox backBufferCombo;
        private ComboBox depthStencilCombo;
        private ComboBox multiSampleTypeCombo;
        private ComboBox multiSampleQualityCombo;
        private ComboBox vertexCombo;
        private ComboBox presentCombo;
        // Check boxes
        private Checkbox clipBox;
        // Radio buttons
        private RadioButton windowedButton;
        private RadioButton fullscreenButton;
        // Static controls that are cared about
        private StaticText adapterFormatStatic;
        private StaticText resolutionStatic;
        private StaticText refreshStatic;
        #endregion

        /// <summary>
        /// Creates the controls for use in the dialog
        /// </summary>
        private void CreateControls()
        {
            dialog = new Dialog(parent);
            dialog.IsUsingKeyboardInput = true;
            dialog.SetFont(0, "Arial", 15, FontWeight.Normal);
            dialog.SetFont(1, "Arial", 28, FontWeight.Bold);
            
            // Right justify static controls
            Element e = dialog.GetDefaultElement(ControlType.StaticText, 0);
            e.textFormat = DrawTextFormat.VerticalCenter | DrawTextFormat.Right;

            // Title
            StaticText title = dialog.AddStatic((int)SettingsDialogControlIds.Static, "Direct3D Settings", 10, 5, 400, 50);
            e = title[0];
            e.FontIndex = 1;
            e.textFormat = DrawTextFormat.Top | DrawTextFormat.Left;

            // Adapter
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Display Adapter", 10, 50, 180, 23);
            adapterCombo = dialog.AddComboBox((int)SettingsDialogControlIds.Adapter, 200, 50, 300, 23);

            // Device Type
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Render Device", 10, 75, 180, 23);
            deviceCombo = dialog.AddComboBox((int)SettingsDialogControlIds.DeviceType, 200, 75, 300, 23);

            // Windowed / Fullscreen
            windowedButton = dialog.AddRadioButton((int)SettingsDialogControlIds.Windowed, (int)SettingsDialogControlIds.RadioButtonGroup, 
                "Windowed", 240, 105, 300, 16, false);
            clipBox = dialog.AddCheckBox((int)SettingsDialogControlIds.DeviceClip, "Clip to device when window spans across multiple monitors", 
                250, 126, 400, 16, false);
            fullscreenButton = dialog.AddRadioButton((int)SettingsDialogControlIds.Fullscreen, (int)SettingsDialogControlIds.RadioButtonGroup, "Full Screen", 
                240, 147, 300, 16, false);

            // Adapter Format
            adapterFormatStatic = dialog.AddStatic((int)SettingsDialogControlIds.AdapterFormatLabel, "Adapter Format", 
                10, 180, 180, 23);
            adapterFormatCombo = dialog.AddComboBox((int)SettingsDialogControlIds.AdapterFormat, 200, 180, 300, 23);

            // Resolution
            resolutionStatic = dialog.AddStatic((int)SettingsDialogControlIds.ResolutionLabel, "Resolution", 10, 205, 180, 23);
            resolution = dialog.AddComboBox((int)SettingsDialogControlIds.Resolution, 200, 205, 300, 23);
            resolution.SetDropHeight(106);

            // Refresh Rate
            refreshStatic = dialog.AddStatic((int)SettingsDialogControlIds.RefreshRateLabel, "Refresh Rate", 10, 230, 180, 23);
            refreshCombo = dialog.AddComboBox((int)SettingsDialogControlIds.RefreshRate, 200, 230, 300, 23);

            // BackBuffer Format
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Back Buffer Format", 10, 265, 180, 23 );
            backBufferCombo = dialog.AddComboBox((int)SettingsDialogControlIds.BackBufferFormat, 200, 265, 300, 23 );

            // Depth Stencil
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Depth/Stencil Format", 10, 290, 180, 23 );
            depthStencilCombo = dialog.AddComboBox((int)SettingsDialogControlIds.DepthStencil, 200, 290, 300, 23 );

            // Multisample Type
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Multisample Type", 10, 315, 180, 23 );
            multiSampleTypeCombo = dialog.AddComboBox((int)SettingsDialogControlIds.MultisampleType, 200, 315, 300, 23 );

            // Multisample Quality
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Multisample Quality", 10, 340, 180, 23 );
            multiSampleQualityCombo = dialog.AddComboBox((int)SettingsDialogControlIds.MultisampleQuality, 200, 340, 300, 23 );

            // Vertex Processing
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Vertex Processing", 10, 365, 180, 23 );
            vertexCombo = dialog.AddComboBox((int)SettingsDialogControlIds.VertexProcessing, 200, 365, 300, 23 );

            // Present Interval
            dialog.AddStatic((int)SettingsDialogControlIds.Static, "Present Interval", 10, 390, 180, 23 );
            presentCombo = dialog.AddComboBox((int)SettingsDialogControlIds.PresentInterval, 200, 390, 300, 23 );

            // Add the ok/cancel buttons
            Button okButton = dialog.AddButton((int)SettingsDialogControlIds.OK, "OK", 230,435,73,31);
            Button cancelButton = dialog.AddButton((int)SettingsDialogControlIds.Cancel, "Cancel", 315,435,73,31,0, true);
            okButton.Click += new EventHandler(OnOkClicked);
            cancelButton.Click += new EventHandler(OnCancelClicked);
        }

        /// <summary>Changes the UI defaults to the current device settings</summary>
        public void Refresh()
        {
            // Get some information
            globalSettings = parent.DeviceSettings.Clone();
            System.Drawing.Rectangle client = parent.WindowClientRectangle;
            windowWidth = (uint)client.Width;
            windowHeight = (uint)client.Height;

            // Fill the UI with the current settings
            if (!deviceCombo.ContainsItem(globalSettings.DeviceType.ToString()))
                deviceCombo.AddItem(globalSettings.DeviceType.ToString(), globalSettings.DeviceType.ToString());

            SetWindowed(globalSettings.presentParams.Windowed);
            clipBox.IsChecked = ((globalSettings.presentParams.PresentFlag & PresentFlag.DeviceClip) != 0);

            if (!adapterFormatCombo.ContainsItem(globalSettings.AdapterFormat.ToString()))
                adapterFormatCombo.AddItem(globalSettings.AdapterFormat.ToString(), globalSettings.AdapterFormat);

            AddResolution((short)globalSettings.presentParams.BackBufferWidth, (short)globalSettings.presentParams.BackBufferHeight);
            AddRefreshRate(globalSettings.presentParams.FullScreenRefreshRateInHz);

            if (!backBufferCombo.ContainsItem(globalSettings.presentParams.BackBufferFormat.ToString()))
                backBufferCombo.AddItem(globalSettings.presentParams.BackBufferFormat.ToString(), globalSettings.presentParams.BackBufferFormat);

            if (!depthStencilCombo.ContainsItem(globalSettings.presentParams.AutoDepthStencilFormat.ToString()))
                depthStencilCombo.AddItem(globalSettings.presentParams.AutoDepthStencilFormat.ToString(), globalSettings.presentParams.AutoDepthStencilFormat);

            if (!multiSampleTypeCombo.ContainsItem(globalSettings.presentParams.MultiSample.ToString()))
                multiSampleTypeCombo.AddItem(globalSettings.presentParams.MultiSample.ToString(), globalSettings.presentParams.MultiSample);

            if (!multiSampleQualityCombo.ContainsItem(globalSettings.presentParams.MultiSampleQuality.ToString()))
                multiSampleQualityCombo.AddItem(globalSettings.presentParams.MultiSampleQuality.ToString(), globalSettings.presentParams.MultiSampleQuality);

            if (!presentCombo.ContainsItem(globalSettings.presentParams.PresentationInterval.ToString()))
                presentCombo.AddItem(globalSettings.presentParams.PresentationInterval.ToString(), globalSettings.presentParams.PresentationInterval);

            BehaviorFlags flags = new BehaviorFlags(globalSettings.BehaviorFlags);
            if (flags.PureDevice)
                AddVertexProcessing(CreateFlags.PureDevice);
            else if (flags.HardwareVertexProcessing)
                AddVertexProcessing(CreateFlags.HardwareVertexProcessing);
            else if (flags.SoftwareVertexProcessing)
                AddVertexProcessing(CreateFlags.SoftwareVertexProcessing);
            else if (flags.MixedVertexProcessing)
                AddVertexProcessing(CreateFlags.MixedVertexProcessing);

            // Get the adapters list from Enumeration object
            ArrayList adapterInfoList = Enumeration.AdapterInformationList;

            if (adapterInfoList.Count == 0)
                throw new NoCompatibleDevicesException();

            adapterCombo.Clear();
            
            // Add all of the adapters
            for (int iAdapter = 0; iAdapter < adapterInfoList.Count; iAdapter++)
            {
                EnumAdapterInformation adapterInfo = adapterInfoList[iAdapter] as EnumAdapterInformation;
                if (!adapterCombo.ContainsItem(adapterInfo.UniqueDescription))
                    adapterCombo.AddItem(adapterInfo.UniqueDescription, iAdapter);
            }
            adapterCombo.SetSelectedByData(globalSettings.AdapterOrdinal);

            // The adapter changed, call the handler
            OnAdapterChanged(adapterCombo, EventArgs.Empty);

            Dialog.SetRefreshTime((float)FrameworkTimer.GetTime());
        }

        /// <summary>Render the dialog</summary>
        public void OnRender(float elapsedTime)
        {
            state.Capture();
            parent.Device.RenderState.FillMode = FillMode.Solid;
            dialog.OnRender(elapsedTime);
            state.Apply();
        }

        /// <summary>Hand messages off to dialog</summary>
        public void HandleMessages(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam)
        {
            dialog.MessageProc(hWnd, msg, wParam, lParam);
        }
        #region Device event callbacks
        /// <summary>
        /// Called when the device is created
        /// </summary>
        public void OnCreateDevice(Device d)  
        { 
            // Hook all the events we care about
            resolution.Changed += new EventHandler(OnResolutionChanged);
            adapterCombo.Changed += new EventHandler(OnAdapterChanged);
            deviceCombo.Changed += new EventHandler(OnDeviceChanged);
            adapterFormatCombo.Changed += new EventHandler(OnAdapterFormatChange);
            refreshCombo.Changed += new EventHandler(OnRefreshRateChanged);
            backBufferCombo.Changed += new EventHandler(OnBackBufferChanged);
            depthStencilCombo.Changed += new EventHandler(OnDepthStencilChanged);
            multiSampleTypeCombo.Changed += new EventHandler(OnMultisampleTypeChanged);
            multiSampleQualityCombo.Changed += new EventHandler(OnMultisampleQualityChanged);
            vertexCombo.Changed += new EventHandler(OnVertexProcessingChanged);
            presentCombo.Changed += new EventHandler(OnPresentIntervalChanged);
            clipBox.Changed += new EventHandler(OnClipWindowChanged);
            windowedButton.Changed += new EventHandler(OnWindowedFullscreenChanged);
            fullscreenButton.Changed += new EventHandler(OnWindowedFullscreenChanged);
        } 

        /// <summary>
        /// Called when the device is reset
        /// </summary>
        public void OnResetDevice()
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -