2016-07-13 3 views
2

マルチスクリーン対応のマルチウィンドウでアプリケーションを開発しています。私はすべてのウィンドウのために私自身のタイトルバーを作った。デュアルスクリーン用の最大化機能の管理

私のタイトルバー:機能付き

<Grid x:Class="TitleBar" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     Style="{DynamicResource TitleStyle}" 
     MouseLeftButtonDown="gridBar_MouseLeftButtonDown" 
     x:Name="titleBar"> 

    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="auto"/> 
    </Grid.ColumnDefinitions> 

    <Label Grid.Column="0" [...]/> 

    <Button Grid.Column="1" x:Name="bttnClose" [...]/> 
    <Button Grid.Column="1" x:Name="buttonMinimize" [...]/> 
</Grid> 

private void gridBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    if (Window.GetWindow(this) != null) 
    { 
     if (e.ClickCount == 2) // double click handle 
     { 
      if (Window.GetWindow(this).WindowState == WindowState.Normal)     
       Window.GetWindow(this).WindowState = WindowState.Maximized; 
      else 
       Window.GetWindow(this).WindowState = WindowState.Normal; 
     } 
     try 
     { 
      Window.GetWindow(this).DragMove(); 
     } 
     catch { } 
    } 
} 

すべての私の窓は(最大/最小の高さと幅に等しいです)、メインウィンドウを除いて、かなりの再されていません。ですから、私はフルスクリーンからマルチスクリーン環境を管理したいと思っています。

私は私のMainWindowクラスでこれを追加しました:

protected override void OnStateChanged(EventArgs e) 
{ 
    if (WindowState == WindowState.Maximized) 
    { 
     var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle(); 
     var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST); 
     var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY); 
     var isInPrimary = currentMonitor == primaryMonitor; 

     // Don't want to hide the taskbar 
     if (isInPrimary) 
      this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; 
     else 
      this.MaxHeight = Double.PositiveInfinity; 
    } 
    base.OnStateChanged(e); 
} 

internal static class NativeMethods 
{ 
    public const Int32 MONITOR_DEFAULTTOPRIMERTY = 0x00000001; 
    public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002; 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern IntPtr MonitorFromWindow(IntPtr handle, Int32 flags); 
} 

それはほとんど動作しますが、私はMaxHeightを設定すると、それは実際のHeightをリフレッシュしません。私は画面で2倍にして、正しいサイズにしなければならなかった。

あなたは私を助けることができますか?

+1

タスクバーが左または右にある場合、またはセカンダリモニタにも表示されている場合は、 – Phil1970

+1

この場合、動作しません:-(。このコードは非常に基本的なものであり、ちょうど1つのケースを考慮していますが、.NETがアクティブエリアとスクリーンを簡単に管理する機能を備えていない(または、 –

答えて

0

私は最終的に解決策を見つけましたが、そうでないかもしれない最高の1:

private void Maximize() 
{ 
    var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle(); 
    var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST); 
    var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY); 
    var isInPrimary = currentMonitor == primaryMonitor; 

    // Don't want to hide the taskbar 
    if (isInPrimary) 
     this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; 
    else 
     this.MaxHeight = Double.PositiveInfinity; 
} 

protected override void OnStateChanged(EventArgs e) 
{ 
    if (WindowState == WindowState.Maximized && DoRefresh) 
    { 
     DoRefresh = false; 
     WindowState = WindowState.Normal; 
     Maximize(); 
     DoRefresh = true; 
    } 
} 

問題が最大のWindows上の高さやMaxHeightのを変更しても効果はありませんということでした。状態がMaximized Iに変更されると、強制的に強制的にMaxHeightを変更します。その後、私は状態を最大にリセットしました。無限ループを避けるために、私はブールDoRefreshを作成しました。

関連する問題