1

我已经使用 WPF 和一些不错的教程构建了一个屏幕保护程序。当我将我的 exe 重命名为 scr 并对其进行测试时,它按预期运行,移动鼠标或按一个键按预期退出。

当我右键单击并安装屏幕保护程序时,它会显示带有预览的屏幕保护程序窗口(这看起来像我当前桌面的屏幕截图,这很烦人,如果有人知道如何更改它,那将很有用)。

如果我按下预览,它应该会显示我的屏幕保护程序,而不是什么都不会发生(除了预览图像看起来像是被新图像替换了)。

如果我离开屏幕保护程序超时,我得到的只是一个闪光,但实际上什么都没有显示。

这是我的代码:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private void OnStartup(object sender, StartupEventArgs e)
    {
        string[] args = e.Args;

        if (args.Length > 0)
        {
            // Get the 2 character command line argument
            string arg = args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2);
            switch (arg)
            {
                case "/c":
                    // Show the options dialog
                    MessageBox.Show("This screensaver has no options.");
                    break;
                case "/p":
                    // Don't do anything for preview
                    ShowScreensaver();
                    break;
                case "/s":
                    // Show screensaver form
                    ShowScreensaver();
                    break;
                default:
                    MessageBox.Show("Default!.");
                    Application.Current.Shutdown();
                    break;
            }
        }
        else
        {
            // If no arguments were passed in, show the screensaver
            ShowScreensaver();
        }
    }

    /// <summary>
    /// Shows screen saver by creating one instance of Window1 for each monitor.
    /// 
    /// Note: uses WinForms's Screen class to get monitor info.
    /// </summary>
    void ShowScreensaver()
    {
        //creates window on primary screen
        var primaryWindow = new MainWindow();

        //creates window on other screens
        foreach (var screen in System.Windows.Forms.Screen.AllScreens)
        {
            if (screen == System.Windows.Forms.Screen.PrimaryScreen)
                continue;

            var window = new MainWindow();
        }

        //show non-primary screen windows
        foreach (Window window in System.Windows.Application.Current.Windows)
        {
            if (window != primaryWindow)
                window.Show();
        }

        ///shows primary screen window last
        primaryWindow.Show();
    }
}

void OnLoaded(object sender, EventArgs e)
{
    #if !DEBUG
        Topmost = true;
        MouseMove += new MouseEventHandler(PlayOnScreen_MouseMove);
        MouseDown += new MouseButtonEventHandler(PlayOnScreen_MouseDown);
        KeyDown += new KeyEventHandler(PlayOnScreen_KeyDown);
    #endif
}

void PlayOnScreen_KeyDown(object sender, KeyEventArgs e)
{
    Application.Current.Shutdown();
}

void PlayOnScreen_MouseDown(object sender, MouseButtonEventArgs e)
{
    Application.Current.Shutdown();
}

void PlayOnScreen_MouseMove(object sender, MouseEventArgs e)
{
    Point currentPosition = e.MouseDevice.GetPosition(this);
    // Set IsActive and MouseLocation only the first time this event is called.
    if (!isActive)
    {
        mousePosition = currentPosition;
        isActive = true;
    }
    else
    {
        // If the mouse has moved significantly since first call, close.
        if ((Math.Abs(mousePosition.X - currentPosition.X) > 10) ||
            (Math.Abs(mousePosition.Y - currentPosition.Y) > 10))
        {
            Application.Current.Shutdown();
        }
    }
}

bool isActive;
Point mousePosition;

XAML 很简单:

<Application x:Class="PlayOnScreen.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="OnStartup">
    <Application.Resources>

    </Application.Resources>
</Application>

而对于屏保

<Window xmlns:my="http://schemas.awesomium.com/winfx" 
        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" mc:Ignorable="d" x:Class="PlayOnScreen.MainWindow"
        Title="mediaPlayer" Height="350" Width="525" 
        Loaded="OnLoaded" Cursor="None">
    <Grid x:Name="root">
        <Grid Panel.ZIndex="1000">
            <Image x:Name="overlay" HorizontalAlignment="Left" VerticalAlignment="Bottom" />
        </Grid>
    </Grid>
</Window>

请帮我解决这个问题:(

4

0 回答 0