【问题标题】:WPF application that slides from edge of screen when triggered by a button由按钮触发时从屏幕边缘滑动的 WPF 应用程序
【发布时间】:2021-05-15 09:20:42
【问题描述】:

我正在尝试制作一个当您按下按钮(如 F8)时可以从最右侧屏幕边缘滑动的 WPF 应用程序。这将类似于 Windows 10 中的 Windows 通知栏。

我正在努力寻找制作类似东西的方法。任何帮助将不胜感激!

我能够制作一个具有给定高度、宽度的窗口,并使其贴在屏幕右侧和顶部:

public MainWindow()
{
    InitializeComponent();

    Width = 300;
    Height = System.Windows.SystemParameters.WorkArea.Height;

    Left = System.Windows.SystemParameters.WorkArea.Width - Width;
    Top = 0;
}

【问题讨论】:

    标签: wpf windows-10 notification-bar


    【解决方案1】:

    您可以覆盖窗口的OnPreviewKeyDown 方法并使用DoubleAnimationLeft 属性设置动画。像这样的:

    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);
    
        if (e.Key == Key.F8)
        {
            double distanceToSlide = 100;
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = Left,
                To = Left - distanceToSlide,
                Duration = new Duration(TimeSpan.FromSeconds(1))
            };
    
            this.BeginAnimation(Window.LeftProperty, doubleAnimation);
        }
    }
    

    上面的示例在一秒钟内将窗口 100 DIP 向左滑动。 DoubleAnimation 的属性显然可以根据自己的需求进行更改。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    相关资源
    最近更新 更多