用C#制作飘动的窗体效果

效果图:

最近翻看以前的学习C#的联系代码,无意之中发现一个很有趣的项目。是一个飘动窗体的效果,运行程序之后,在当前屏幕上会像雪花般飘动很多自定义图标,并且它们就像雪花般轻盈地从屏幕上方飘落到屏幕下方,直到消失。在程序运行过程中,屏幕上会维持一定数目的雪花。在系统托盘区域会有一个图标,点击这个图标,可以退出程序。这个联系代码联系了如何使用不规则窗体和系统托盘控件。

程序中核心部分源代码:

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace FallingGold { /// <summary></summary> /// 说明:图片轮廓类,通过这个类提供的方法得到图片的外围轮廓 /// 作者:周公 /// 原创地址: http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx /// public class BitmapRegion { public BitmapRegion() { } public static void CreateControlRegion(Control control, Bitmap bitmap) { //如果控件或者图象为空 if (control == null || bitmap == null) { return; } //将控件设置成图象一样的尺寸 control.Width = bitmap.Width; control.Height = bitmap.Height; //如果处理的是一个窗体对象 if (control is System.Windows.Forms.Form) { Form form = control as Form;//强制转换为Form实例 //将窗体的尺寸设置成比图片稍微大一点,这样就不用显示窗体边框 form.Width += 15; form.Height += 35; //设置窗体无边框 form.FormBorderStyle = FormBorderStyle.None; //设置窗体背景 form.BackgroundImage = bitmap; //根据图片计算路径 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //应用区域 form.Region = new Region(graphicsPath); } //如果处理的是一个按钮对象 else if (control is System.Windows.Forms.Button) { Button button = control as Button;//强制转换为Button实例 //不显示文字 button.Text = ""; //当鼠标处在上方时更改光标状态 button.Cursor = Cursors.Hand; //设置背景图片 button.BackgroundImage = bitmap; //根据图片计算路径 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //应用区域 button.Region = new Region(graphicsPath); } } /// <summary></summary> /// 通过逼近的方式扫描图片的轮廓 /// /// 要扫描的图片 /// <returns></returns> private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) { GraphicsPath graphicsPath = new GraphicsPath(); //将图片的(0,0)处的颜色定义为透明色 Color transparentColor = bitmap.GetPixel(0, 0); //存储发现的第一个不透明的象素的列值(即x坐标),这个值将定义我们扫描不透明区域的边缘 int opaquePixelX = 0; //从纵向开始 for (int y = 0; y < bitmap.Height; y++) { opaquePixelX = 0; for (int x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel(x, y) != transparentColor) { //标记不透明象素的位置 opaquePixelX = x; //记录当前位置 int nextX = x; for (nextX = opaquePixelX; nextX < bitmap.Width; nextX++) { if (bitmap.GetPixel(nextX, y) == transparentColor) { break; } } graphicsPath.AddRectangle(new Rectangle(opaquePixelX, y, nextX - opaquePixelX, 1)); x = nextX; } } } return graphicsPath; } } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FallingGold
{
    /// <summary></summary>
    /// 说明:飘动的窗体
    /// 作者:周公
    /// 原创地址:http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx
    /// 
    public partial class GoldForm : Form
    {
        private int currentX;//图片的当前横坐标
        private int currentY;//图片的当前纵坐标
        private int screenHeight;//屏幕高度
        private int screenWidth;//屏幕宽度
        private int counter;//图片数量
        private int increment;//移动增量
        private int interval;//移动时间间隔

        private Bitmap bmpFlake = Properties.Resources.snow;
        /// <summary></summary>
        /// 构造函数
        /// 
        /// 移动间隔
        /// 飘动窗体的横坐标
        public GoldForm(int interval, int currentX)
        {
            this.interval = interval + 10;
            this.currentX = currentX;
            InitializeComponent();

            BitmapRegion.CreateControlRegion(this, bmpFlake);
        }

        private void GoldForm_Load(object sender, EventArgs e)
        {
            //获取屏幕的工作区域,不包括状态栏
            Rectangle rectangleWorkArea = Screen.PrimaryScreen.WorkingArea;
            screenHeight = rectangleWorkArea.Height;
            screenWidth = rectangleWorkArea.Width;
            timerMove.Interval = interval;//设置timer的间隔
            this.Left = currentX;//设置窗体的起始横坐标
            timerMove.Start();//运行timer
        }
        //timer的事件
        private void timerMove_Tick(object sender, EventArgs e)
        {
            timerMove.Stop();
            currentY += 5;
            counter++;

            Random random = new Random();
            if (counter == 15)
            {
                if ((random.Next(10) - 5) > 0)
                {
                    increment = 1;
                }
                else
                {
                    increment = -1;
                }
                counter = 0;
            }

            currentX += increment;
            if (currentY > screenHeight)
            {
                currentY = 0;
                currentX = random.Next(screenWidth);
                interval = random.Next(50,100);
            }
            //设置窗体位置,相当于移动窗体
            this.Location = new Point(currentX, currentY);

            timerMove.Interval = interval;
            timerMove.Start();
        }
    }
}
整个程序的源代码请到 http://download.csdn.net/source/484535下载。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值