XNA程序开发常用到的一些代码汇总

This is the code form the menu system that starts the XNA game.


    var process = Process.Start(info);
    var currentProcess = Process.GetCurrentProcess();
    while (!process.HasExited)
    {
        Thread.Sleep(50);
        if (GetForegroundWindow() == currentProcess.MainWindowHandle)
        {
            Activate(process.MainWindowHandle);
        }
    }

This is the code that sets the XNA game as the foreground window.


        /// <summary>
        
/// Sets the window to be foreground
        
/// </summary>
        [DllImport("User32")]
        private static extern int SetForegroundWindow(IntPtr hwnd);
 
        /// <summary>
        
/// Activate or minimize a window
        
/// </summary>
        [DllImportAttribute("User32.DLL")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        private const int SW_SHOW = 5;
        private const int SW_MINIMIZE = 6;
        private const int SW_RESTORE = 9;
 
        /// <summary>
        
/// The GetForegroundWindow function returns a handle to the foreground window.
        
/// </summary>
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
 
        private static void Activate(IntPtr windowHandle)
        {
            ShowWindow(windowHandle, SW_RESTORE);
            SetForegroundWindow(windowHandle);
        }


 

 

//设置幕大小,其中ChatForm为聊天WinForm窗体。
ChatForm form = new ChatForm();
                int screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;// System.Windows.Forms.SystemInformation.WorkingArea.Width;
                int screenHeight = System.Windows.Forms.SystemInformation.WorkingArea.Height;
                form.Size = new System.Drawing.Size((int)(screenWidth * 0.2f * 1000) / 1000, (int)(screenHeight));
                form.FormLocation = new System.Drawing.Point((int)(screenWidth * 0.8f * 1000) / 10000);
                form.Show();
          
                IntPtr ip = Window.Handle;
                System.Windows.Forms.Form gameForm = System.Windows.Forms.Control.FromHandle(ip) as System.Windows.Forms.Form;
                if (gameForm != null)
                {
                    gameForm.Location = new System.Drawing.Point(00);
                    gameForm.Size = new System.Drawing.Size((int)(screenWidth * 0.8f * 1000) / 1000, (int)(screenHeight));
                }

===========================================================================================

int screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            int screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            graphics.PreferredBackBufferWidth = (int)(screenWidth * 0.8f);
            graphics.PreferredBackBufferHeight = screenHeight;


this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);

//获取除任务栏之外的显示屏高度
 System.Windows.Forms.SystemInformation.WorkingArea.Height;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string currentScreenSize_OutTaskBar=SystemInformation.WorkingArea.Width.ToString() + "," +SystemInformation.WorkingArea.Height.ToString();

MessageBox.Show("当前的屏幕除任务栏外的工作域大小为:"+currentScreenSize_OutTaskBar);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string currentScreenSize=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString() + "," + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString();

MessageBox.Show("当前的屏幕包括任务栏的工作域大小为:"+currentScreenSize);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Size OutTaskBarSize = new Size(SystemInformation.WorkingArea.Width, SystemInformation.WorkingArea.Height);

Size ScreenSize = new Size(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);

Size TaskBarSize;

TaskBarSize = new Size(
                (ScreenSize.Width - (ScreenSize.Width - OutTaskBarSize.Width)),
                (ScreenSize.Height - OutTaskBarSize.Height)
                );

MessageBox.Show("任务栏大小:" + TaskBarSize.Width + "," + TaskBarSize.Height);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/furenjun/p/xnaCode.html