示例#1
0
        public static void FullscreenOn()
        {
            if (gameWindow == IntPtr.Zero)
            {
                IntPtr[] possibleWindows = GetPossibleWindows();
                if (possibleWindows.Length == 0)
                {
                    return;
                }
                else if (possibleWindows.Length == 1)
                {
                    gameWindow = possibleWindows[0];
                }
                else
                {
                    FormSelectApp.instance.UpdateWindowsList(possibleWindows);
                    if (FormSelectApp.instance.ShowDialog() == DialogResult.OK)
                    {
                        gameWindow = FormSelectApp.instance.GetSelectedWindow();
                        if (gameWindow == IntPtr.Zero)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            Rectangle rect  = PInvokeFunc.GetWindowRect(gameWindow);
            int       w     = rect.Width;
            int       h     = rect.Height;
            int       fullX = -1;
            int       fullY = -1;

            int[,] sizes = { {  640, 480 }, {  800,  600 }, { 1024,  768 }, { 1152,  720 }, { 1152, 864 }, { 1280, 720 },
                             { 1280, 768 }, { 1280,  800 }, { 1280,  960 }, { 1280, 1024 }, { 1360, 768 }, { 1440, 900 },
                             { 1600, 900 }, { 1600, 1024 }, { 1600, 1200 } };
            int minD = int.MaxValue;

            for (int i = 0; i <= sizes.GetUpperBound(0); ++i)
            {
                int dx = w - sizes[i, 0];
                int dy = h - sizes[i, 1];
                int d  = dx * dx + dy * dy;
                if (d <= minD)
                {
                    minD  = d;
                    fullX = sizes[i, 0];
                    fullY = sizes[i, 1];
                }
            }

            Global.windowPosition.MainFormPosition = WindowPosition.Serialize(Form1.thisForm);
            if (Form1.thisForm.formOptions != null)
            {
                Global.windowPosition.OptionsFormPosition = WindowPosition.Serialize(Form1.thisForm.formOptions);
            }
            if (FormMonitor.isCreated())
            {
                Global.windowPosition.MonitorFormPosition = WindowPosition.Serialize(FormMonitor.instance);
            }
            oldGameLocation = new Point(rect.Left, rect.Top);
            fullscreen      = true;
            try
            {
                if (dxDevice == null)
                {
                    dxDevice = new Device();
                }
                dxDevice.SetDisplayMode(fullX, fullY, 32, 0, true);
                int border = (rect.Width - fullX) / 2;
                PInvokeFunc.SetWindowPos(gameWindow, IntPtr.Zero, -border, fullY - rect.Height + border, 0, 0, 5);
                WindowPosition.Deserialize(Form1.thisForm, Global.windowPosition.FullscreenPosition);
                if (Form1.thisForm.Width > fullX)
                {
                    Form1.thisForm.Width = fullX;
                }
                if (Form1.thisForm.Height > fullY)
                {
                    Form1.thisForm.Height = fullY;
                }
                if (Form1.thisForm.Left + Form1.thisForm.Width > fullX)
                {
                    Form1.thisForm.Left = fullX - Form1.thisForm.Width;
                }
                if (Form1.thisForm.Top + Form1.thisForm.Height > fullY)
                {
                    Form1.thisForm.Top = fullY - Form1.thisForm.Height;
                }
                Form1.thisForm.TopMost = true;
            }
            catch (Exception)
            {
                FullscreenOff();
            }
        }