當前位置:係統粉 >  係統教程 > 電腦軟件教程 >  電腦桌麵下雪的使用方法

電腦桌麵下雪的使用方法

時間:2015-04-16 來源:互聯網 瀏覽量:

完整版本,下雪了

C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
namespace WindowsApplication30
{
    public partial class frmDesktopSnow : Form
    {
        /// <summary>
        /// 下雪啦
        /// </summary>

        [DllImport("User32.dll ", EntryPoint = "FindWindow")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll ")]
        static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string strname);
        List<Point> SnowPoints = new List<Point>();
        List<int> SnowRate = new List<int>();
        Bitmap Bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        IntPtr DesktopHandle = IntPtr.Zero;
        #region public frmDesktopSnow() // 構造函數
        /// <summary>
        /// 構造函數
        /// </summary>
        public frmDesktopSnow()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            BtnStart.Size = new Size(100, 70);
            BtnClose.Size = new Size(100, 70);
            BtnStart.Location = new Point(0, 0);
            BtnClose.Location = new Point(101, 0);
            BtnStart.Text = "點一下吧";
            BtnClose.Text = "關閉";
            this.Size = new Size(BtnClose.Right, BtnClose.Bottom);
            this.Opacity = 0.8;
        }
        #endregion
        #region void SnowTimer_Tick(object sender, EventArgs e) // 下雪計時器事件
        /// <summary>
        /// 下雪計時器事件
        /// </summary>
        /// <param name="sender">計時器本身</param>
        /// <param name="e">計時器參數</param>
        void SnowTimer_Tick(object sender, EventArgs e)
        {
            Graphics DesktopGraphics = Graphics.FromHwnd(DesktopHandle);
            String Text = "聖誕快到了,希望大家身體健康!";
            if (Convert.ToInt32(SnowTimer.Tag) == 0)
                DesktopGraphics.DrawString(Text, new Font("宋體", 10), new SolidBrush(Color.Red),
                    new PointF(Screen.PrimaryScreen.Bounds.Width / 2 - 200, Screen.PrimaryScreen.Bounds.Height / 2));
            else
                DesktopGraphics.DrawString(Text, new Font("宋體", 10), new SolidBrush(Color.Green),
                    new PointF(Screen.PrimaryScreen.Bounds.Width / 2 - 200, Screen.PrimaryScreen.Bounds.Height / 2));
            SnowTimer.Tag = 1 - Convert.ToInt32(SnowTimer.Tag);
            for (int i = 0; i < SnowPoints.Count; i++)
            {
                DesktopGraphics.DrawImage(Bmp, SnowPoints[i].X, SnowPoints[i].Y, new Rectangle(SnowPoints[i], PbSnow.Size),
                    GraphicsUnit.Pixel);
                SnowPoints[i] = new Point(SnowPoints[i].X, SnowPoints[i].Y + SnowRate[i]);
                if (SnowPoints[i].Y > Screen.PrimaryScreen.Bounds.Bottom)
                    SnowPoints[i] = new Point(SnowPoints[i].X, 0);
                DesktopGraphics.DrawImage(PbSnow.Image, SnowPoints[i]); // PbSnow.Image 小雪花圖片
            }
            DesktopGraphics.Dispose();
        }
        #endregion
        #region void BtnStart_Click(object sender, EventArgs e) // 下雪
        /// <summary>
        /// 開始下雪
        /// </summary>
        /// <param name="sender">按鈕本身</param>
        /// <param name="e">按鈕</param>
        void BtnStart_Click(object sender, EventArgs e)
        {
            SnowPoints.Clear();
            Random R = new Random();
            for (int i = 0; i < 50; i++)
            {
                SnowPoints.Add(new Point(R.Next(Screen.PrimaryScreen.Bounds.Width),
                    R.Next(Screen.PrimaryScreen.Bounds.Height)));
                SnowRate.Add(R.Next(10));
            }
            ShowDesktop();
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(500); // 等待桌麵完全顯示
            Graphics G = Graphics.FromImage(Bmp);
            G.CopyFromScreen(new Point(0, 0), new Point(Screen.PrimaryScreen.Bounds.Left, Screen.PrimaryScreen.Bounds.Top),
                 new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            G.Dispose();
            this.WindowState = FormWindowState.Normal;
            IntPtr P = FindWindow("Progman", "Program Manager");
            P = FindWindowEx(P, IntPtr.Zero, "SHELLDLL_DefView", null);
            DesktopHandle = FindWindowEx(P, IntPtr.Zero, "SysListView32", null);
            SnowTimer.Interval = 1;
            SnowTimer.Enabled = true; // 開始下雪
        }
        #endregion
        #region void BtnClose_Click(object sender, EventArgs e) // 關閉
        /// <summary>
        /// 關閉
        /// </summary>
        /// <param name="sender">窗口本身</param>
        /// <param name="e">窗口參數</param>
        void BtnClose_Click(object sender, EventArgs e)
        {
            ShowDesktop();
            Close();
        }
        #endregion
        #region static void ShowDesktop() // 顯示桌麵
        /// <summary>
        /// 顯示桌麵
        /// </summary>
        static void ShowDesktop()
        {
            Type OleType = Type.GetTypeFromProgID("Shell.Application");
            object OleObject = System.Activator.CreateInstance(OleType);
            OleType.InvokeMember("ToggleDesktop", BindingFlags.InvokeMethod, null, OleObject, null);
        }
        #endregion
    }
} 小雪花圖片用網上有的小雪花矢量圖jpg或bmp用photoshop把背景顏色的區域刪掉鏤空成透明保存成GIF然後導入PbSnow PictureBox

我要分享:
版權信息

Copyright @ 2011 係統粉 版權聲明 最新發布內容 網站導航