同步第二个窗口大小以匹配主窗口大小

时间:2014-07-14 13:33:52

标签: c# wpf resize sync

我已经尝试在MainWindow属性的ResizeMode中导出资源,认为数据可能与我的Window2 ResizeMode属性绑定,但它与抓取和拖动位置/值不匹配,它只是匹配属性值,在此case是'CanResizeWithGrip',我已经分配给MainWindow。所以我最终得到两个窗口的夹子,Window2与MainWindow的调整大小不匹配。当我点击并拖动MainWindow上的调整大小手柄时,我想自动收缩/放大我的Window2。我无法真正掌握LocationChanged或SizeChanged句柄以及在这种情况下应该如何使用它们。 我的MainWindow有一个带VLC插件的视频文件源,我的Window2有一个透明的背景和覆盖的Toggle按钮和一个Exit按钮。如果任何人可以提供帮助,任何建议/确定的例子都会有所帮助。

主窗口:

namespace VLC_Test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
  public partial class MainWindow : Window
  {
    AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();

    public MainWindow()
    {
        InitializeComponent();
        WFH1.Child = vlcPlayer;           

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Window2 win2 = new Window2();
        win2.Show();

        string file1 = @"C:\Users\Username\Desktop\drop.avi";

        vlcPlayer.addTarget("file:///" + file1, null, AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
        vlcPlayer.play();
    }    
  }
}

XAML:

<Window x:Class="VLC_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" SizeToContent="WidthAndHeight"
    WindowStartupLocation="Manual" 
    Top="0" Left="7" AllowsTransparency="False" WindowStyle="None" Loaded="Window_Loaded" Topmost="True" ShowInTaskbar="False" BorderThickness="0" ResizeMode="CanResizeWithGrip">


<Window.Background>
    <SolidColorBrush Opacity="0" Color="White"/>
</Window.Background>


  <Grid Width="Auto" Height="Auto">
    <WindowsFormsHost Height="495" Width="550" HorizontalAlignment="Left" Name="WFH1" VerticalAlignment="Top" Margin="-11,-24,0,0" ChildChanged="WFH1_ChildChanged" />                
  </Grid>
</Window>

窗口2:

namespace VLC_Test
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
  public partial class Window2 : Window
  {

    public Window2()
    {
        InitializeComponent();
    }



    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {

       this.Close();
       App.Current.Shutdown();
    }

  }
}

XAML:

<Window x:Class="VLC_Test.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="495" Width="550"
    WindowStartupLocation="Manual" 
    Top="0" Left="7" AllowsTransparency="True" WindowStyle="None" Topmost="True" >

<Window.Background>
    <SolidColorBrush Opacity="0" />
</Window.Background>

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>

<Grid Width="Auto" Height="Auto">
    <ToggleButton Content="Crosshair" Height="39" HorizontalAlignment="Right" Margin="0,0,125,12" Name="Button1" VerticalAlignment="Bottom" Width="58" Click="button1_Click" IsChecked="False" DataContext="{Binding}"/>
    <Button Content="Exit" Height="39" HorizontalAlignment="Right" Margin="0,0,49,12" Name="button2" VerticalAlignment="Bottom" Width="58" Click="button2_Click" />

    <Canvas Background="Transparent" Height="200" Width="200" HorizontalAlignment="Center" Margin="0,0,0,0" Name="Canvas1" VerticalAlignment="Center" Visibility="{Binding IsChecked, ElementName=Button1, Converter={StaticResource BooleanToVisibilityConverter}}">
        <Line X1="100" Y1="0" X2="100" Y2="75" Stroke="Red" StrokeThickness="0.95" />
        <!--Top long vertical line> /-->
        <Line X1="100" Y1="95" X2="100" Y2="105" Stroke="Red" StrokeThickness="0.95" />
        <!--Crosshair vertical line> /-->
        <Line X1="100" Y1="125" X2="100" Y2="200" Stroke="Red" StrokeThickness="0.95" />
        <!--Bottom long vertical line> /-->
        <Line X1="0" Y1="100" X2="75" Y2="100" Stroke="Red" StrokeThickness="0.75" />
        <!--Left long horizontal line> /-->
        <Line X1="95" Y1="100" X2="105" Y2="100" Stroke="Red" StrokeThickness="0.75" />
        <!--Crosshair horizontal line> /-->
        <Line X1="125" Y1="100" X2="200" Y2="100" Stroke="Red" StrokeThickness="0.75" />
    </Canvas>

  </Grid>

</Window>

1 个答案:

答案 0 :(得分:1)

我只想使用SizeChanged事件。更改MainWindow XAML以包含对事件处理程序的调用。 (同样,如果要在移动MainWindow时将两个窗口保持在一起,请使用也显示的LocationChanged事件。):

<Window x:Class="VLC_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" SizeToContent="WidthAndHeight"
    WindowStartupLocation="Manual" 
    Top="0" Left="7" AllowsTransparency="False" WindowStyle="None" Loaded="Window_Loaded" Topmost="True" ShowInTaskbar="False" BorderThickness="0" ResizeMode="CanResizeWithGrip"
    SizeChanged="Window_Resize" LocationChanged="Window_Moved">

    <Window.Background>
        <SolidColorBrush Opacity="0" Color="White"/>
    </Window.Background>


    <Grid Width="Auto" Height="Auto">
        <WindowsFormsHost Height="495" Width="550" HorizontalAlignment="Left" Name="WFH1" VerticalAlignment="Top" Margin="-11,-24,0,0" ChildChanged="WFH1_ChildChanged" />                
    </Grid>
</Window>

然后在您的MainWindow的cs文件中,实现该事件。请注意,我在窗口上保留了一个变量来存储你的Window2对象,所以我可以在它加载后再引用它。

namespace VLC_Test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
  public partial class MainWindow : Window
  {
    AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();
    Window2 win2;

    public MainWindow()
    {
        InitializeComponent();
        WFH1.Child = vlcPlayer;           

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        CreateWindow2Overlay();

        string file1 = @"C:\Users\Username\Desktop\drop.avi";

        vlcPlayer.addTarget("file:///" + file1, null,     AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
        vlcPlayer.play();
    }  

    private void CreateWindow2Overlay()
    {
        win2 = new Window2();
        win2.Left = Left;
        win2.Top = Top;
        win2.Width = Width;
        win2.Height = Height;
        win2.Owner = this;
        win2.Show();
    }  

    private void Window_Resize(object sender, SizeChangedEventArgs e)
    {
        if (win2 != null)
        {
            win2.Height = e.NewSize.Height;
            win2.Left = Left;
            win2.Width = e.NewSize.Width;
            win2.Top = Top;
        }
    }

    private void Window_Moved(object sender, EventArgs e)
    {
        if (win2 != null)
        {
            win2.Left = Left;
            win2.Top = Top;
        }
    }
  }
}

对MainWindow代码所做的所有更改。我没有更改Window2的cs或xaml。

此解决方案在Visual Studio 2010中进行了测试。请注意,我添加了CreateWindow2Overlay方法,该方法设置了第二个窗口。它专门将Window2的所有者设置为此(MainWindow),以便将Window2放在MainWindow前面。

要明确,这一变化解决的问题是: 1.将Window2对象放在MainWindow的顶部 2. MainWindow调整大小时更改Window2大小

如果您的请求中有任何我遗失的内容,请不要犹豫,将其指出。

我希望能帮助你。

相关问题