1

我有一个窗口,它只显示一个图像和一个字符串。如果 5 秒后我通过更改其 Top 属性“淡出”窗口。如果发生这种情况,它会完全窃取我的主窗口的焦点,我无法做任何事情,因为主窗口被阻塞了。我已经用一些本机方法尝试过这个,但这对我不起作用。有没有办法完全移除此窗口的焦点,以便在更改另一个窗口的 Top 属性时不会窃取主窗口的焦点并且不会阻止它?

用户界面 xaml 代码:

<Window x:Class="Rikkachan.PlayedVideo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PlayedVideo" Height="307" Width="542" 
        ResizeMode="NoResize" AllowsTransparency="True" 
        WindowStyle="None" Background="Transparent" ShowActivated="False">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Image Name="picBox" Grid.Row="0"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="100"/>
        <Label Name="lblPlayedVideo" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Opacity="100" Background="DimGray"/>
    </Grid>
</Window>

这是淡出和东西背后的代码:

    public partial class PlayedVideo : Window
{
    #region "Variables"
    Rect workingArea = SystemParameters.WorkArea;
    #endregion

    #region "Initilizing"
    public PlayedVideo()
    {
        InitializeComponent();
        #region "Setting window at the lower right bottom"
        this.EnableWindowMoving();
        this.Left = workingArea.Right - this.Width;
        this.Top = workingArea.Bottom - this.Height;
        #endregion
        picBox.Source = Screenshot.CreateScreenshotFromWindow("vlc").ToBitmapSource();
        lblPlayedVideo.Content = "Media: " + PlayerProcessInformation.GetCurrentPlayedFile(SupportedPlayer.VLCMediaPlayer);
        StartCloseTimer();

        this.Closed += (sender, e) =>
            {
                Debug.WriteLine("I'm dead");
            };
    }
    #endregion

    #region "StartCloseTimer"
    private void StartCloseTimer(double sec = 5)
    {
        this.Dispatcher.BeginInvoke((Action)(() =>
            {
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(sec);
                timer.Tick += (sender, e) =>
                    {
                        for (double i = this.Top; i < workingArea.Bottom; i += 10, Opacity -= 0.099) // look if we get this smoother
                        {
                            this.Top = i;
                            Debug.WriteLine("Top: " + this.Top + "\r\nBottom: " + workingArea.Bottom);
                            Thread.Sleep(200);
                        }
                        timer.Stop();
                        Debug.WriteLine("Timer stopped, now I'm going to die!");
                        this.Close();
                    };
                timer.Start();
            }));
    }
    #endregion
}

更新 了 UI XAML 代码:

 <Window x:Class="Rikkachan.PlayedVideo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PlayedVideo" Height="307" Width="542" 
        ResizeMode="NoResize" AllowsTransparency="True" 
        WindowStyle="None" Background="Transparent" ShowActivated="False"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard Name="storyBoardBegin" Completed="storyBoardBegin_Completed">
                    <DoubleAnimation AutoReverse="False" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:3" FillBehavior="HoldEnd" RepeatBehavior="1x" />
                    <DoubleAnimation AutoReverse="False" Storyboard.TargetProperty="Top" From="{Binding TopEnd}" To="{Binding TopBegin}" Duration="0:0:1" RepeatBehavior="1x"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Image Name="picBox" Grid.Row="0"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="100"/>
        <Label Name="lblPlayedVideo" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Opacity="100" Background="DimGray"/>
    </Grid>
</Window>

后面的代码:

    public partial class PlayedVideo : Window
{
    #region "Variables"
    Rect workingArea = SystemParameters.WorkArea;
    private bool closeStoryBoardCompleted = false;
    private Window m_wnd = null;
    //private Storyboard storyBoardBegin;
    //private Storyboard storyBoardEnd;
    #endregion

    #region "Initilizing"
    public PlayedVideo(Window wnd)
    {
        InitializeComponent();
        m_wnd = wnd;
        #region "Setting window at the lower right bottom"
        this.EnableWindowMoving();
        this.Left = workingArea.Right - this.Width;
        this.Top = workingArea.Bottom - this.Height;

        #endregion
        StartCloseTimer();

        this.Closed += (sender, e) =>
            {
                Debug.WriteLine("I'm dead");
            };
    }
    #endregion

    #region "StartCloseTimer"
    private void StartCloseTimer(double sec = 5)
    {
        Task.Run(() =>
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(sec);
                timer.Tick += (sender, e) =>
                    {
                        if (!closeStoryBoardCompleted)
                        {
                            DoubleAnimation animation = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(sec));
                            DoubleAnimation animation2 = new DoubleAnimation(workingArea.Bottom, (Duration)TimeSpan.FromSeconds(sec));
                            m_wnd.Focus();
                            animation.Completed += (s, _) =>
                                {
                                    Debug.WriteLine("Timer stopped, now I'm going to die!");
                                    this.Close();
                                };
                            this.BeginAnimation(UIElement.OpacityProperty, animation);
                            this.BeginAnimation(Window.TopProperty, animation2);
                        }
                    };
                timer.Start();
            }));
        });
    }
    #endregion

    private void storyBoardBegin_Completed(object sender, EventArgs e)
    {
        picBox.Source = Screenshot.CreateScreenshotFromWindow("vlc").ToBitmapSource();
        lblPlayedVideo.Content = "Media: " + PlayerProcessInformation.GetCurrentPlayedFile(SupportedPlayer.VLCMediaPlayer);
    }

    public double TopBegin{get { return workingArea.Bottom - this.Height; }}
    public double TopEnd { get { return workingArea.Bottom; } }
}
4

1 回答 1

0

查看 WPF 动画。您可以使用内置的 WPF 功能集而不是您拥有的代码来执行此操作。动画开始和持续时间的计时器包含在功能集中,旨在比您自己的 Dispatcher 调用更好地管理 UI 焦点。

可以在此处找到一个可以帮助您入门的教程:

http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx

以下是对这些想法的基于代码(而不是基于 XAML)的介绍:

http://www.codeproject.com/Articles/23257/Beginner-s-WPF-Animation-Tutorial

并且,官方文档。使元素淡入淡出的教程可能特别适用。

http://msdn.microsoft.com/en-us/library/ms752312.aspx

于 2013-06-19T16:57:15.713 回答