2016-10-24 79 views
1

當我在輔助監視器上最大化時,我有關於恢復窗口狀態的問題。 我在非主屏幕上最大化了窗口,然後關閉。 當重新打開窗口時,它也被最大化,但是它在主屏幕上被最大化。 我想在非主屏幕上(關閉時的屏幕顯示窗口)最大化。在輔助監視器上恢復最大化狀態

請幫助我,如果你知道。

注意:如果窗口狀態正常,窗口將恢復正確的屏幕。

我的代碼如下:

if (ShellState == WindowState.Maximized) 
     { 
      ShellState = WindowState.Normal; 
      LeftPosition = Screen.AllScreens[selectedScreen].WorkingArea.Left; 
      TopPosition = Screen.AllScreens[selectedScreen].WorkingArea.Top; 
      ShellHeight = Screen.AllScreens[selectedScreen].WorkingArea.Height; 
      ShellWidth = Screen.AllScreens[selectedScreen].WorkingArea.Width; 
      ShellState = WindowState.Maximized; 
     } 

回答

1

我們使用標準的WPF工具存儲和恢復窗口狀態和大小,只要在屏幕分配對多屏系統的許多問題。

我們致力於創建使用本機WinAPI函數的自定義行爲。 以下是我們行爲的(簡化)源代碼。你可以在你的應用程序中使用它,而不是WPF工具。

您必須更改窗口布局的存儲方式。這可以是提供容器的依賴項屬性,靜態的參考或其他參考。在下面的代碼中,以靜態ApplicationSettings引用爲例。

class WindowPlacementPersistenceBehavior : Behavior<Window> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     this.AssociatedObject.SourceInitialized += this.AssociatedObject_SourceInitialized; 
     this.AssociatedObject.Closing += this.AssociatedObject_Closing; 
    } 

    protected override void OnDetaching() 
    { 
     this.AssociatedObject.SourceInitialized -= this.AssociatedObject_SourceInitialized; 
     this.AssociatedObject.Closing -= this.AssociatedObject_Closing; 
     base.OnDetaching(); 
    } 

    private void AssociatedObject_Closing(object sender, CancelEventArgs e) 
    { 
     WINDOWPLACEMENT wp; 
     NativeMethods.GetWindowPlacement(new WindowInteropHelper(this.AssociatedObject).Handle, out wp); 

     // Here you can store the window placement 
     ApplicationSettings.WindowPlacement = wp.ToString(); 
    } 

    private void AssociatedObject_SourceInitialized(object sender, EventArgs e) 
    { 
     // Here you can load the window placement 
     WINDOWPLACEMENT wp = WINDOWPLACEMENT.Parse(ApplicationSettings.WindowPlacement); 
     if (wp.ShowCmd == NativeMethods.SW_SHOWMINIMIZED) 
     { 
      // Don't start in the minimized state 
      wp.ShowCmd = NativeMethods.SW_SHOWNORMAL; 
     } 

     try 
     { 
      NativeMethods.SetWindowPlacement(new WindowInteropHelper(this.AssociatedObject).Handle, ref wp); 
     } 
     catch 
     { 
     } 
    } 

    [Serializable] 
    [StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
     public int Left; 
     public int Top; 
     public int Right; 
     public int Bottom; 

     public static RECT Parse(string input) 
     { 
      RECT result; 
      string[] items = input.Split(';'); 
      result.Left = int.Parse(items[0]); 
      result.Top = int.Parse(items[1]); 
      result.Right = int.Parse(items[2]); 
      result.Bottom = int.Parse(items[3]); 
      return result; 
     } 

     public override string ToString() 
     { 
      return this.Left + ";" + this.Top + ";" + this.Right + ";" + this.Bottom; 
     } 
    } 

    [Serializable] 
    [StructLayout(LayoutKind.Sequential)] 
    private struct POINT 
    { 
     public int X; 
     public int Y; 

     public static POINT Parse(string input) 
     { 
      POINT result; 
      string[] items = input.Split(';'); 
      result.X = int.Parse(items[0]); 
      result.Y = int.Parse(items[1]); 
      return result; 
     } 

     public override string ToString() 
     { 
      return this.X + ";" + this.Y; 
     } 
    } 

    [Serializable] 
    [StructLayout(LayoutKind.Sequential)] 
    private struct WINDOWPLACEMENT 
    { 
     public int Length; 
     public int Flags; 
     public int ShowCmd; 
     public POINT MinPosition; 
     public POINT MaxPosition; 
     public RECT NormalPosition; 

     public static WINDOWPLACEMENT Parse(string input) 
     { 
      WINDOWPLACEMENT result = default(WINDOWPLACEMENT); 
      result.Length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); 
      try 
      { 
       string[] items = input.Split('/'); 
       result.Flags = int.Parse(items[0]); 
       result.ShowCmd = int.Parse(items[1]); 
       result.MinPosition = POINT.Parse(items[2]); 
       result.MaxPosition = POINT.Parse(items[3]); 
       result.NormalPosition = RECT.Parse(items[4]); 
      } 
      catch 
      { 
      } 

      return result; 
     } 

     public override string ToString() 
     { 
      return this.Flags + "/" + this.ShowCmd + "/" + this.MinPosition.ToString() + "/" + this.MaxPosition.ToString() + "/" + this.NormalPosition.ToString(); 
     } 
    } 

    private static class NativeMethods 
    { 
     public const int SW_SHOWNORMAL = 1; 
     public const int SW_SHOWMINIMIZED = 2; 

     [DllImport("user32.dll")] 
     public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); 

     [DllImport("user32.dll")] 
     public static extern bool GetWindowPlacement(IntPtr hWnd, [Out] out WINDOWPLACEMENT lpwndpl); 
    } 
} 

要使用此行爲,只是將它添加到你的窗口在XAML:

<Window 
    xmlns:v="clr-namespace:YourNameSpace" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"> 
<i:Interaction.Behaviors> 
    <v:WindowPlacementPersistenceBehavior /> 
</i:Interaction.Behaviors> 
</Window>