11

抛开实际用途不谈,如何(如果可能的话)在运行 Windows 的台式 PC 上创建“下雪”效果?最好只使用原始 C/C++ 和 WinAPI。

对雪的要求是:

  • 出现在显示的所有其他内容上(注意:始终在顶部的窗口可能仍然在雪上,没关系。我知道任何应用程序都没有“绝对在顶部”标志)
  • 雪花很小,可能是几个白色像素的简单点或簇;
  • 无需费心使用计算机(点击雪花会将点击发送到底层窗口);
  • 与用户拖动窗口很好地配合;
  • 多显示器能力。

以下任何功能的奖励积分:

  • 雪堆积在窗口或任务栏的下边缘(如果它在屏幕底部);
  • 雪也会积聚在顶层窗户上。或者,也许有些雪会积聚,有些雪会继续向下,积聚在每个带有标题栏的窗口上;
  • 当窗户被拖动时,积在窗户上的雪会被“抖掉”;
  • 任务栏上的积雪知道 Vista/7 下扩展的“开始”按钮。
  • 雪花有阴影/轮廓,因此它们在白色背景上可见;
  • 雪花具有复杂的类似雪花的形状(它们必须仍然很小)。
  • 点击雪花确实会将点击发送到底层窗口,但雪花会随着一点很酷的动画消失;

这些效果中的大多数都很简单,除了雪是点击的部分并且可以很好地拖动窗口。在我早期的时候,我已经做了一个实现,它利用了你从中获得的 HDC GetDesktopWindow(),它是点击的,但是在用户拖动窗口时遇到了问题(在它们上呈现的雪花被“拖着”)。

该解决方案可能使用 Vista/7 Aero 功能,但当然,首选通用解决方案。有任何想法吗?

4

1 回答 1

6

为了简洁起见,这个答案已被修剪为一组有限的要求。扩展它并使其更健壮是微不足道的。

这个答案在 Windows XP 上使用 WPF。它最多可以在 2 个显示器上工作,并且也应该可以在其他 Windows 系统上工作。

它从一个简单的窗口开始:

<Window x:Class="TestDump.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" WindowStartupLocation="Manual" Loaded="Window_Loaded"
WindowStyle="None" AllowsTransparency="True" Background="Transparent"
>
    <Grid x:Name="FieldOfSnow"/>
</Window>

在这个窗口中,我们将添加定义如下的雪花:

<UserControl x:Class="TestDump.SnowFlake"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="5" Width="5">
    <Border Background="White" CornerRadius="2" BorderThickness="1" BorderBrush="LightGray"/>
</UserControl>

雪花具有默认的 UserControl 代码隐藏,没有任何更改。

最后,Window CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace TestDump
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private TimeSpan _lastRender;

        public Window1()
        {
            InitializeComponent();
            _lastRender = TimeSpan.FromTicks(0);
            CompositionTarget.Rendering += SnowflakeTick;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Topmost = true;
            this.Top = 0;
            this.Left = 0;

            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

            if (System.Windows.Forms.SystemInformation.MonitorCount == 2)
            {
                System.Drawing.Rectangle SecondScreenArea = System.Windows.Forms.Screen.AllScreens[1].Bounds;

                this.Width += SecondScreenArea.Width;
                this.Height = this.Height > SecondScreenArea.Height ? this.Height : SecondScreenArea.Height;
            }
        }

        public const int WS_EX_TRANSPARENT = 0x00000020;
        public const int GWL_EXSTYLE = (-20);

        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            // Get this window's handle
            IntPtr hwnd = new WindowInteropHelper(this).Handle;

            // Change the extended window style to include WS_EX_TRANSPARENT
            int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
        }

        List<TranslateTransform> Flakes = new List<TranslateTransform>();
        Random rand = new Random();

        private void SnowflakeTick(object sender, EventArgs e)
        {
            RenderingEventArgs renderingArgs = (RenderingEventArgs)e;
            TimeSpan dTime = (renderingArgs.RenderingTime - _lastRender);
            double deltaTime = dTime.TotalMilliseconds;
            _lastRender = renderingArgs.RenderingTime;

            if ( _lastRender.Milliseconds < deltaTime)
            {
                TranslateTransform SnowPos = new TranslateTransform(this.Width * rand.Next(1000) / 1000.0 - this.Width/2, -this.Height/2);

                SnowFlake sf = new SnowFlake();
                sf.RenderTransform = SnowPos;

                // The flakes are centered when added, so all positions must be translated with this in mind.
                FieldOfSnow.Children.Add(sf);
                Flakes.Add(SnowPos);
            }

            foreach (TranslateTransform Flake in Flakes)
            {
                double ScreenHeight = this.Height / 2 - 2;

                if (Flake.Y < ScreenHeight)
                {
                    Flake.Y += deltaTime / 50;
                }
            }
        }
    }
}

我必须使用一些表单代码来获取多屏内容,并且我必须在我的项目中包含对程序集的引用。

我没有对其进行太多测试,但它确实可以在我的系统上运行,并且雪在完成后位于屏幕底部。

将此参考用于点击行为。

一个比我更敬业的人应该能够适应这一点,再加上一些边缘检测,以完成将雪放在别处的任务。

请注意,在此示例中从未清理过雪花,并且在运行足够长的时间后,您可能会注意到一些速度变慢。

玩得开心!

于 2011-01-11T20:19:48.543 回答