prosource

현재 화면의 WPF 창 최대화

probook 2023. 4. 23. 10:34
반응형

현재 화면의 WPF 창 최대화

윈도리스 wpf 어플리케이션이 있는데 윈도 상태를 maximized로 설정하면 프라이머리 디스플레이에서 최대가 됩니다.

어플리케이션이 실행되고 있는 디스플레이의 크기를 최대화하고 싶습니다.

그럼 어떻게 해야 할지 짐작 가는 거라도 있어?

현재 제 코드는

private void titleBarThumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (this.WindowState == System.Windows.WindowState.Normal)
    {
        this.WindowState = System.Windows.WindowState.Maximized;
    }
    else
    {
        this.WindowState = System.Windows.WindowState.Normal;
    }
}

Main Window(첫 번째 제어) 컨스트럭터에 다음 행을 포함시켰습니다.

Application.Current.MainWindow.WindowState = WindowState.Maximized;

태스크바 때문에 사용자 작업영역의 크기를 사용해야 합니다.

this.Width=SystemParameters.WorkArea.Width;
this.Height=SystemParameters.WorkArea.Height;

뷰의 생성자에서 사용할 수 있습니다.

아직 답변할 수 있을지 모르겠습니다.- 저는 이 앱으로 샘플 앱을 만들었습니다.

WindowStyle = WindowStyle.None;

버튼을 만들고 클릭 핸들러로 이렇게...

WindowState = WindowState.Maximized

마우스의 왼쪽 버튼 아래로 핸들러를 연결해서 창을 끌어다 놓았습니다.

this.MouseLeftButtonDown += new(MainWindow_MouseLeftButtonDown);

private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   DragMove();
}

창을 두 번째 모니터로 드래그하여 [최대화]버튼을 클릭하면 시작창이 아닌 현재 창에서 최대화됩니다.VS2010 및 을 사용하고 있었습니다.NET 4. 이게 도움이 되는지 알려주세요.

7개의 보충투표가 있는 질문은 정답을 얻을 수 있습니다. : D

일반 창 대신 이 창을 사용하면 Maxmize/Minimize/Normalize가 자동으로 처리됩니다.

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

public partial class MyWindow : Window
{
    public MyWindow ()
    {
        this.InitializeComponent();

        this.SourceInitialized += this.OnSourceInitialized;
    }

    #endregion

    #region Methods

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

    private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
    {
        var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

        // Adjust the maximized size and position to fit the work area of the correct monitor
        IntPtr monitor = MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);

        if (monitor != IntPtr.Zero)
        {
            var monitorInfo = new MONITORINFO();
            GetMonitorInfo(monitor, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            RECT rcMonitorArea = monitorInfo.rcMonitor;
            mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
            mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
            mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
            mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
        }

        Marshal.StructureToPtr(mmi, lParam, true);
    }

    private void OnSourceInitialized(object sender, EventArgs e)
    {
        var window = sender as Window;

        if (window != null)
        {
            IntPtr handle = (new WindowInteropHelper(window)).Handle;
            HwndSource.FromHwnd(handle).AddHook(WindowProc);
        }
    }
}

DLL Import 및 선언

[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
    public POINT ptReserved;

    public POINT ptMaxSize;

    public POINT ptMaxPosition;

    public POINT ptMinTrackSize;

    public POINT ptMaxTrackSize;
} ;

public enum MonitorFromWindowFlags
{
    MONITOR_DEFAULTTONEAREST = 0x00000002
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
    public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

    public RECT rcMonitor;

    public RECT rcWork;

    public int dwFlags;
}

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct RECT
{
    public int Left;

    public int Top;

    public int Right;

    public int Bottom;

    public static readonly RECT Empty;

    public int Width
    {
        get
        {
            return Math.Abs(this.Right - this.Left);
        } // Abs needed for BIDI OS
    }

    public int Height
    {
        get
        {
            return this.Bottom - this.Top;
        }
    }

    public RECT(int left, int top, int right, int bottom)
    {
        this.Left = left;
        this.Top = top;
        this.Right = right;
        this.Bottom = bottom;
    }

    public RECT(RECT rcSrc)
    {
        this.Left = rcSrc.Left;
        this.Top = rcSrc.Top;
        this.Right = rcSrc.Right;
        this.Bottom = rcSrc.Bottom;
    }

    public bool IsEmpty
    {
        get
        {
            // BUGBUG : On Bidi OS (hebrew arabic) left > right
            return this.Left >= this.Right || this.Top >= this.Bottom;
        }
    }

    public override string ToString()
    {
        if (this == Empty)
        {
            return "RECT {Empty}";
        }
        return "RECT { left : " + this.Left + " / top : " + this.Top + " / right : " + this.Right + " / bottom : " +
               this.Bottom + " }";
    }

    public override bool Equals(object obj)
    {
        if (!(obj is RECT))
        {
            return false;
        }
        return (this == (RECT)obj);
    }

    public override int GetHashCode()
    {
        return this.Left.GetHashCode() + this.Top.GetHashCode() + this.Right.GetHashCode() +
               this.Bottom.GetHashCode();
    }

    public static bool operator ==(RECT rect1, RECT rect2)
    {
        return (rect1.Left == rect2.Left && rect1.Top == rect2.Top && rect1.Right == rect2.Right &&
                rect1.Bottom == rect2.Bottom);
    }

    public static bool operator !=(RECT rect1, RECT rect2)
    {
        return !(rect1 == rect2);
    }
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

로딩하기 전에는 창을 최대화할 수 없습니다.따라서 fullScreenWindow의 Loaded 이벤트를 잠그고 다음 방법으로 이벤트를 처리합니다.

private void Window_Loaded(object sender, RoutedEventArgs e) 
{
    WindowState = WindowState.Maximized;
}

이렇게 해서 보조화면에서 응용 프로그램을 극대화 시켰습니다.

메인 창의 맨 위에 이 항목을 추가합니다.

using Screen = System.Windows.Forms.Screen;

최대화 핸들러에 추가합니다.

private void AdjustWindowSize()
{
    if (this.WindowState == WindowState.Maximized)
    {
        this.WindowState = WindowState.Normal;
    }
    else
    {
        System.Drawing.Rectangle r = Screen.GetWorkingArea(new System.Drawing.Point((int)this.Left, (int)this.Top));
        this.MaxWidth = r.Width;
        this.MaxHeight = r.Height;
        this.WindowState = WindowState.Maximized;
    }
}

자, 갑니다!

c# 어플리케이션은 우선 프라이머리 디스플레이에서 기동합니다.단, 이동하지 않는 한, 코드는 동작합니다.그러나 wpf 앱을 다른 디스플레이로 이동할 경우 새 위치를 기록하고 로컬 구성 파일에 저장할 수 있습니다.그러나 앱에는 테두리나 다른 네이티브 컨트롤이 없기 때문에 이동 비트를 구현해야 합니다.또한 창이 이동되면 System Parameters를 사용하여 디스플레이 인덱스를 캡처할 수 있습니다.

행운을 빌어요

저도 같은 문제에 부딪혔어요.내 경우 팝업 창을 다 썼을 때 숨겼다는 사실이 밝혀졌습니다.그래서 다음 번에 전화해서 Maximize를 요청하면 원래 화면에서 할 수 있습니다.일단 Closing(닫기)을 시작하자 적절한 화면에서 최대화되기 시작했습니다.

시험:

Window.WindowState = WindowState.Normal;

언급URL : https://stackoverflow.com/questions/6071372/maximize-wpf-window-on-the-current-screen

반응형