程序中核心部分源代码:
- 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;
- }
- }
- }
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace FallingGold { ////// 说明:图片轮廓类,通过这个类提供的方法得到图片的外围轮廓 /// 作者:周公 /// 原创地址: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); } } /// /// 通过逼近的方式扫描图片的轮廓 /// /// 要扫描的图片 /// 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>