使用WPF使用多个监视器

时间:2015-10-03 14:41:45

标签: c# wpf fullscreen multiple-monitors

我正在使用WPF在C#中开发应用程序。我有两个窗户。当我在第一个窗口中单击按钮时,我需要第二个窗口在第二个监视器上显示全屏(如果连接了第二个监视器)。

我有这个代码: 第一个窗口(应该在主监视器上)

//Error Checking
//Min Payment & Max Payment
... if($payment_amount>10.00) { ...

//Redirect User for Payment
$paypal_email = $email;
$return_url = $sso_url."/myaccount/payments/success";
$cancel_url = $sso_url."/myaccount/payments/cancel";
$notify_url = $sso_url."/servlet/gateway/PayPal-IPN.Event.php";

$payment_url  ="cmd=_xclick&business=$paypal_email&currency_code=GBP&no_shipping=1&amount=$payment_amount&item_name=Service+Payment+My+Account&return=$return_url&cancel_url=$cancel_url";
$payment_url  = "https://www.sandbox.paypal.com/cgi-bin/webscr?".rawurlencode(utf8_encode($payment_url));

header("Location: $payment_url");
exit();

的Xaml:

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;
    }
}
}

第二个窗口(应该在第二个监视器上)

<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>

的Xaml

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();
    }
}
}

0 个答案:

没有答案