C#屏幕保护程序配置窗口将不会打开

时间:2019-02-28 21:51:23

标签: c# wpf window windows-screensaver

如何正确启动屏幕保护程序的配置视图?

enter image description here

我对任何提示和帮助感到高兴。提前致谢。屏保是用C#编写的.NET 4.5.2,因为IDE使用的是Visual Studio 2015 Professional。当我从cmd('screensaver.scr / c')启动屏幕保护程序时,将显示配置视图。如果让Windows启动配置,则不会显示该视图。我让您留在代码下方。

事件commandLineHandler.ConfigViewStartHandler启动configview。

app.xaml.cs:

public partial class App
{

    /// <summary>
    /// Constructor starts Managed Extensibility Framework. This allows
    /// inversion of control. Also startup event is handled to
    /// instantiate MainWindow.
    /// </summary>
    public App()
    {
        Init_App();
    }

    private CommandLineHandler commandLineHandler;
    private MainWindowViewModel mainWindowViewModel;
    private MainWindow mainWindow;
    private MainWindow2 mainWindow2;
    private MainWindow3 mainWindow3;
    private ConfigView configView;
    private PreviewHandler previewHandler;

    #region methods

    /// <summary>
    /// Initializes managed extensibility framework, varies and views
    /// inside app.
    /// </summary>
    private void Init_App()
    {

        // Loads and initializes Managed Extensibility Framework.
        ManagedExtensibilityHandler.Initialize(this);
        ManagedExtensibilityHandler.ComposeParts(this);

        commandLineHandler = new CommandLineHandler();

        // Fetches triggered events.
        commandLineHandler.StartScreensaverHandler += StartScreensaver;
        commandLineHandler.StartPreviewHandler += StartScreensaverPreview;
        commandLineHandler.ConfigViewStartHandler += ShowConfigView;
        DispatcherUnhandledException += OnDispatcherUnhandledException;

        // Initializes varies.
        mainWindowViewModel = new MainWindowViewModel();
        mainWindow = new MainWindow(mainWindowViewModel);
        mainWindow2 = new MainWindow2(mainWindowViewModel);
        mainWindow3 = new MainWindow3(mainWindowViewModel);
        previewHandler = new PreviewHandler();
        configView = new ConfigView();
    }

    /// <summary>
    /// Helps to understand start and troubling exceptions.
    /// Shows a message box with the exception message and stack trace.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">DispatcherUnhandledExceptionEventArgs</param>
    private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show($"{e.Exception.Message}\n{e.Exception.StackTrace}\n{e.Exception.Source}",
            "Error Message",
            MessageBoxButton.OK,
            MessageBoxImage.Error);
        Environment.Exit(1);
    }

    /// <summary>
    /// Starts screensaver by an event.
    /// </summary>
    private void StartScreensaver()
    {
        mainWindowViewModel = new MainWindowViewModel();
        previewHandler = null;
        configView?.Close();

        // Opens views for one until 3 connected monitors.
        switch (Screen.AllScreens.Length)
        {
            case 1:
                {

                    // Section for one screen.
                    ShowOnOneMonitor();
                    break;
                }
            case 2:
                {

                    // Section for two screens.
                    ShowOnDualMonitors();
                    break;
                }
            case 3:
                {

                    // Section for three screens.
                    ShowOnTrippleMonitors();
                    break;
                }
        }
    }

    /// <summary>
    /// Compute CommandLine Args.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">StartupEventaArgs</param>
    public void App_Startup(object sender, StartupEventArgs e)
    {
        CommandLineInputs(Environment.GetCommandLineArgs());
    }

    /// <summary>
    /// Exit Screensaver on \a.
    /// </summary>
    /// <param name="commandLineArgs"></param>
    private void CommandLineInputs(string[] commandLineArgs)
    {

        // Starts the CommandlineHandler if there are more than one
        // argument.
        if (commandLineArgs.Length > 1)
        {
            commandLineHandler.ComputeCmdArgument(commandLineArgs);
        }
    }

    /// <summary>
    ///  Starts previewMainWindow of the screensaver inside a handle.
    /// </summary>
    /// <param name="e">StartHandleEventArgs</param>
    private void StartScreensaverPreview(StartHandleEventArgs e)
    {
        configView?.Close();
        CloseViews();

        previewHandler.DoMiniPreview(e.CommandLineArgStrings, true);
    }

    /// <summary>
    /// Shows the configuration of the screensaver and saves user changes
    /// int a json-file inside user's temp directory. For example
    /// C:\Users\REMY~1.MEI\AppData\Local\Temp.
    /// </summary>
    private void ShowConfigView()
    {
        CloseViews();
        previewHandler = null;

        configView.Show();
    }

    /// <summary>
    /// Closes every view on displays at once.
    /// </summary>
    private void CloseViews()
    {
        mainWindow?.Close();
        mainWindow2?.Close();
        mainWindow3?.Close();
    }

    /// <summary>
    /// Shows screensaver on one monitor.
    /// </summary>
    private void ShowOnOneMonitor()
    {

        // Getting screen and WorkingArea
        Screen s1 = Screen.AllScreens[0];
        Rectangle r1 = s1.WorkingArea;

        // Position mainWindow.
        mainWindow.Top = r1.Top;
        mainWindow.Left = r1.Left;

        // Show the view on monitor
        mainWindow.Show();
    }

    /// <summary>
    /// Shows screensaver on dual monitors setting.
    /// </summary>
    private void ShowOnDualMonitors()
    {

        // Gets the screens.
        Screen s1 = Screen.AllScreens[0];
        Screen s2 = Screen.AllScreens[1];

        // Gets the WorkingAreas, which store positions.
        Rectangle r1 = s1.WorkingArea;
        Rectangle r2 = s2.WorkingArea;

        // Positions mainWindow.
        mainWindow.Top = r1.Top;
        mainWindow.Left = r1.Left;

        // Positions mainWindow2.
        mainWindow2.Top = r2.Top;
        mainWindow2.Left = r2.Left;

        // Show views on each display.
        mainWindow.Show();
        mainWindow2.Show();

        // mainWindow owns other views and also closes them
        // on exit or events.
        mainWindow2.Owner = mainWindow;
    }

    /// <summary>
    /// Shows screensaver on tripple monitors settings.
    /// </summary>
    private void ShowOnTrippleMonitors()
    {

        // Gets screens.
        Screen s1 = Screen.AllScreens[0];
        Screen s2 = Screen.AllScreens[1];
        Screen s3 = Screen.AllScreens[2];

        // Gets WorkingsAreas.
        Rectangle r1 = s1.WorkingArea;
        Rectangle r2 = s2.WorkingArea;
        Rectangle r3 = s3.WorkingArea;

        // Position first view.
        mainWindow.Top = r1.Top;
        mainWindow.Left = r1.Left;

        // Position second view.
        mainWindow2.Top = r2.Top;
        mainWindow2.Left = r2.Left;

        // Position third view.
        mainWindow3.Top = r3.Top;
        mainWindow3.Left = r3.Left;

        // Shows views on screens.
        mainWindow.Show();
        mainWindow2.Show();
        mainWindow3.Show();

        // mainWindow owns other views and also closes them
        // on exit or events.
        mainWindow2.Owner = mainWindow;
        mainWindow3.Owner = mainWindow;
    }
    #endregion
}

configview.xaml.cs:

public partial class ConfigView : Window
{
    private ConfigViewModel configViewModel;

    /// <summary>
    /// Initializes an object of a configview.
    /// </summary>
    public ConfigView()
    {
        InitializeComponent();
        configViewModel = new ConfigViewModel();
        DataContext = configViewModel;
    }

    /// <summary>
    /// Changes timer delay of screensaver and stores inside a file.
    /// Then cloeses the configuration view.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void button_Click(object sender, RoutedEventArgs e)
    {
        configViewModel.ChangeTimerDelay();
        Close();
    }

    /// <summary>
    /// Closes view if cancel button is pressed.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void cancelButton_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

configview.xaml:

<Window x:Class="INV_Screensaver.Views.ConfigView"
        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:INV_Screensaver.Views"
        mc:Ignorable="d"
        WindowStyle="ThreeDBorderWindow"
        Title="Config INV-Screensaver" Height="150" Width="300">
    <Grid>
        <Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Left" Height="25" Margin="24,73,0,0" VerticalAlignment="Top" Width="101" Click="cancelButton_Click"/>
        <Button x:Name="button" Content="OK" HorizontalAlignment="Left" Height="25" Margin="144,73,0,0" VerticalAlignment="Top" Width="111" Click="button_Click"/>
        <Label x:Name="label" Content="Timer delay:" HorizontalAlignment="Left" Height="29" Margin="24,24,0,0" VerticalAlignment="Top" Width="86"/>
        <TextBox x:Name="textBoxTimerDelay" HorizontalAlignment="Left" Height="29" Margin="125,24,0,0" TextWrapping="NoWrap" Text="{Binding ConfigTimerElapsedTime}" VerticalAlignment="Top" Width="130"/>


    </Grid>
</Window>

最美好的祝愿

0 个答案:

没有答案