用C#制作飘动的窗体效果
来源:互联网 发布:淘宝营销代理 编辑:程序博客网 时间:2024/05/16 15:52
效果图:
最近翻看以前的学习C#的联系代码,无意之中发现一个很有趣的项目。是一个飘动窗体的效果,运行程序之后,在当前屏幕上会像雪花般飘动很多自定义图标,并且它们就像雪花般轻盈地从屏幕上方飘落到屏幕下方,直到消失。在程序运行过程中,屏幕上会维持一定数目的雪花。在系统托盘区域会有一个图标,点击这个图标,可以退出程序。这个联系代码联系了如何使用不规则窗体和系统托盘控件。
程序中核心部分源代码:
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace FallingGold
- {
- /// <SUMMARY></SUMMARY>
- /// 说明:图片轮廓类,通过这个类提供的方法得到图片的外围轮廓
- /// 作者:周公
- /// 原创地址:<A href="http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx">http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx</A>
- ///
- 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>
- /// 通过逼近的方式扫描图片的轮廓
- ///
- /// <PARAM name="bitmap" />要扫描的图片
- /// <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;
- }
- }
- }
- <PRE class=csharp name="code">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>
- /// 说明:飘动的窗体
- /// 作者:周公
- /// 原创地址:<A href="http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx">http://blog.csdn.net/zhoufoxcn/archive/2008/06/06/2515753.aspx</A>
- ///
- 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>
- /// 构造函数
- ///
- /// <PARAM name="interval" />移动间隔
- /// <PARAM name="currentX" />飘动窗体的横坐标
- 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();
- }
- }
- }</PRE>
- <PRE class=csharp name="code"> </PRE>
- <PRE class=csharp name="code">整个程序的源代码请到<A href="http://download.csdn.net/source/484535">http://download.csdn.net/source/484535</A>下载。</PRE>
- 用C#制作飘动的窗体效果
- 用C#制作飘动的窗体效果
- 用C#制作飘动的窗体效果
- C# 窗体制作Aero效果
- 用C#(Winform)的Timer控件让窗体左右飘动
- 飘动窗体
- C#制作闪动的窗体
- 窗口中一直飘动的广告效果
- Android实现飘动的旗帜效果实例
- 飘动动画窗体
- C#制作鼠标可以穿透的窗体
- 收藏的C#制作不规则窗体
- C#制作不规则窗体的简单方法
- C#闪屏窗体的制作
- C# 制作半透明等待窗体的类
- c# 制作简单的登录验证窗体
- C#0008--制作闪烁的窗体
- C#实现窗体的淡入淡出效果
- oracle卸载
- 利用RMAN备份恢复ORACLE数据
- 2003操作系统不显示移动硬盘盘符的解决办法
- 仿QQ面板的WinForm窗体
- 十年,两个字,一辈子
- 用C#制作飘动的窗体效果
- 稀疏矩阵的接压缩算法的实现
- Ext中的TextArea存值和显示值的问题
- VS2008Express下配置WTL开发环境
- linux下编译c++的一个小问题(解决)
- Asp.net 操作Excel 无法关闭解决方法之一
- Tomcat配置MySql数据源
- C++网络编程 ACE相关资料收集
- Tomcat中配置数据源