0

I am developing application in C# with WPF. I have two windows. When I click on the button in first window I need the second windows to display fullscreen on the second monitor (if a second monitor is connect).

I have this code: First window (should be on Primary monitor)

using System.Windows;

namespace CountDown
{
/// <summary>
/// Interaction logic for Toolbox.xaml
/// </summary>
public partial class Toolbox : Window
{
    CountDownDisplay CountDownWindow = new CountDownDisplay();

    public Toolbox()
    {
        InitializeComponent();
        CountDownWindow.displayCounter();
    }


    private void btnStartCountDown_Click(object sender, RoutedEventArgs e)
    {
        CountDownWindow.startCountDown();
        this.Hide();
    }

    private void btnPrepareCountDown_Click(object sender, RoutedEventArgs e)
    {
        CountDownWindow.vwbImage.Visibility = Visibility.Hidden;
        CountDownWindow.vwbTime.Visibility = Visibility.Visible;
        btnPrepareCountDown.Visibility = Visibility.Hidden;
        btnStartCountDown.Visibility = Visibility.Visible;
    }
}
}

Xaml:

<Window x:Name="windowToolbox" x:Class="CountDown.Toolbox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CountDown"
        mc:Ignorable="d"
        Title="Toolbox" Height="300" Width="300">
    <Grid>
        <Button x:Name="btnPrepareCountDown" Padding="20" Content="Připrav odpočítávání" HorizontalAlignment="center" VerticalAlignment="center" Click="btnPrepareCountDown_Click"/>
        <Button x:Name="btnStartCountDown" Visibility="Hidden" Padding="20" Content="Začni odpočítávat" HorizontalAlignment="center" VerticalAlignment="center" Click="btnStartCountDown_Click" />
         </Grid>
</Window>

Second window (should be on Second monitor)

using System;
using System.Linq;
using System.Windows;
using System.Windows.Threading;

namespace CountDown
{
/// <summary>
/// Interaction logic for CountDownDisplay.xaml
/// </summary>
public partial class CountDownDisplay : Window
{
    DispatcherTimer timer = new DispatcherTimer();
    int time = 5400;
    TimeSpan niceTime;

    public CountDownDisplay()
    {
        InitializeComponent();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += timer_Tick;
    }

    void timer_Tick(object sender, EventArgs e)
    {
        if (time == 0)
        {
            timer.Stop();
            vwbTime.Visibility = Visibility.Hidden;
            vwbImage.Visibility = Visibility.Visible;
        }

        time--;
        niceTime = TimeSpan.FromSeconds(time);

        txbTimeToEnd.Text = niceTime.ToString(@"hh\:mm\:ss");
    }

    public void startCountDown()
    {
        timer.Start();
    }
    public void MaximizeToSecondaryMonitor()
    {
       Window window = this;
        var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
        MessageBox.Show(secondaryScreen.ToString());

        if (secondaryScreen != null)
        {
            if (!window.IsLoaded)
                window.WindowStartupLocation = WindowStartupLocation.Manual;

            var workingArea = secondaryScreen.WorkingArea;
            window.Left = workingArea.Left;
            window.Top = workingArea.Top;
            window.Width = workingArea.Width;
            window.Height = workingArea.Height;
            // If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
            if (window.IsLoaded)
                window.WindowState = WindowState.Maximized;
        }
    }
    public void displayCounter()
    {
        this.Show();
        MaximizeToSecondaryMonitor();
    }
}
}

Xaml

    <Window x:Class="CountDown.CountDownDisplay"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CountDown"
        mc:Ignorable="d"
        Title="CountDownDisplay" Height="768" Width="1024" WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid Name="grid1">

        <Image x:Name="vwbImage" Source="matematickyproud2015.png"/>

        <Viewbox x:Name="vwbTime" Visibility="Hidden">
            <TextBlock x:Name="txbTimeToEnd" Margin="2" Text="01:30:00" HorizontalAlignment="center" VerticalAlignment="center"/>
         </Viewbox>

    </Grid>
</Window>
BlaBlaBla
  • 15
  • 8

0 Answers0