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