-1

I want my wpf application to be able to switch to full screen on my primary and secondary screens

currently it doesn't work

even though i moved the wpf window to my secondary screen it will only go full screen on the primary

private void MaximizeButton_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                // Berücksichtigen der Taskleiste und speichern der vorherigen Fenstergröße und Position
                previousHeight = Height;
                previousWidth = Width;
                previousTop = Top;
                previousLeft = Left;

                var workingArea = SystemParameters.WorkArea;
                MaxHeight = workingArea.Height;
                MaxWidth = workingArea.Width;
                Top = workingArea.Top;
                Left = workingArea.Left;

                WindowState = WindowState.Maximized;
            }
            else
            {
                // Wiederherstellen der vorherigen Fenstergröße und Position
                MaxHeight = double.PositiveInfinity;
                MaxWidth = double.PositiveInfinity;
                Height = previousHeight;
                Width = previousWidth;
                Top = previousTop;
                Left = previousLeft;

                WindowState = WindowState.Normal;
            }
        }

1 Answers1

0

Okay this should work out of the box by default if you did not change the borders. However, I did notice with the latest updates that there is a potential issue. What you can do is get the screen information directly by calling MonitorInfo data. Example would be:

void Window_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr mWindowHandle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
            System.Windows.Interop.HwndSource.FromHwnd(mWindowHandle).AddHook(new System.Windows.Interop.HwndSourceHook(WindowProc));
        }


        private static System.IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case 0x0024:
                    WmGetMinMaxInfo(hwnd, lParam);
                    break;
            }

            return IntPtr.Zero;
        }


        private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
        {
            POINT lMousePosition;
            GetCursorPos(out lMousePosition);

            IntPtr lPrimaryScreen = MonitorFromPoint(new POINT(0, 0), MonitorOptions.MONITOR_DEFAULTTOPRIMARY);
            MONITORINFO lPrimaryScreenInfo = new MONITORINFO();
            if (GetMonitorInfo(lPrimaryScreen, lPrimaryScreenInfo) == false)
            {
                return;
            }

            IntPtr lCurrentScreen = MonitorFromPoint(lMousePosition, MonitorOptions.MONITOR_DEFAULTTONEAREST);

            MINMAXINFO lMmi = (MINMAXINFO)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

            if (lPrimaryScreen.Equals(lCurrentScreen) == true)
            {
                lMmi.ptMaxPosition.X = lPrimaryScreenInfo.rcWork.Left;
                lMmi.ptMaxPosition.Y = lPrimaryScreenInfo.rcWork.Top;
                lMmi.ptMaxSize.X = lPrimaryScreenInfo.rcWork.Right - lPrimaryScreenInfo.rcWork.Left;
                lMmi.ptMaxSize.Y = lPrimaryScreenInfo.rcWork.Bottom - lPrimaryScreenInfo.rcWork.Top;
            }
            else
            {
                lMmi.ptMaxPosition.X = lPrimaryScreenInfo.rcMonitor.Left;
                lMmi.ptMaxPosition.Y = lPrimaryScreenInfo.rcMonitor.Top;
                lMmi.ptMaxSize.X = lPrimaryScreenInfo.rcMonitor.Right - lPrimaryScreenInfo.rcMonitor.Left;
                lMmi.ptMaxSize.Y = lPrimaryScreenInfo.rcMonitor.Bottom - lPrimaryScreenInfo.rcMonitor.Top;
            }

            System.Runtime.InteropServices.Marshal.StructureToPtr(lMmi, lParam, true);
        }

Hope that help if figuring out what your issue is

Demon
  • 379
  • 2
  • 7