基本信息
源码名称:C# 图片动画(屏幕上游动的金鱼)
源码大小:3.49M
文件格式:.rar
开发语言:C#
更新时间:2017-03-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ScreenTool { public partial class frmFish : Form { Point oldPoint = new Point(0, 0); bool mouseDown = false; bool haveHandle = false; Timer timerSpeed = new Timer(); int MaxCount = 50; float stepX = 2f; float stepY = 0f; int count = 0; bool speedMode = false; float left = 0f, top = 0f; bool toRight = true; //是否向右 int frameCount = 20; //总帧数 int frame = 0; //当前帧 int frameWidth = 100; //每帧宽度 int frameHeight = 100; //每帧高度 /// <summary> /// 保存实例对象(单例模式的应用) /// </summary> private static frmFish _frmFish = null; /// <summary> /// 无参构造 /// </summary> public frmFish() { InitializeComponent(); toRight = true; frame = 20; frame = 0; frameWidth = FullImage.Width / 20; frameHeight = FullImage.Height; left = -frameWidth; top = Screen.PrimaryScreen.WorkingArea.Height / 2f; timerSpeed.Interval = 50; timerSpeed.Enabled = true; timerSpeed.Tick = new EventHandler(timerSpeed_Tick); this.DoubleClick = new EventHandler(frmFish_DoubleClick); this.MouseDown = new MouseEventHandler(frmFish_MouseDown); this.MouseUp = new MouseEventHandler(frmFish_MouseUp); this.MouseMove = new MouseEventHandler(frmFish_MouseMove); } /// <summary> /// 获取金鱼实例(单例模式的应用) /// </summary> internal static frmFish GetFishInstance() { if (!(_frmFish is frmFish) || _frmFish.IsDisposed) { _frmFish = new frmFish(); _frmFish.Activate(); } return _frmFish; } #region 重写 /// <summary> /// 窗体实例关闭事件 /// </summary> /// <param name="e"></param> protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; base.OnClosing(e); haveHandle = false; } /// <summary> /// 窗体实例创建事件 /// </summary> /// <param name="e"></param> protected override void OnHandleCreated(EventArgs e) { InitializeStyles(); base.OnHandleCreated(e); haveHandle = true; } /// <summary> /// 构建参数 /// </summary> protected override CreateParams CreateParams { get { CreateParams cParms = base.CreateParams; cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED return cParms; } } #endregion void frmFish_MouseUp(object sender, MouseEventArgs e) { count = 0; MaxCount = new Random().Next(70) 40; timerSpeed.Interval = new Random().Next(20) 2; speedMode = true; mouseDown = false; } private void InitializeStyles() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); UpdateStyles(); } void timerSpeed_Tick(object sender, EventArgs e) { if (!mouseDown) { count ; if (count > MaxCount) { MaxCount = new Random().Next(70) 30; if (speedMode) timerSpeed.Interval = 50; count = 0; stepX = (float)new Random().NextDouble() * 3f 1f; stepY = (float)new Random().NextDouble() * 0.5f; if (stepY < 0.3f) stepY = 0f; stepY = (new Random().Next(2) == 0 ? -1 : 1) * stepY; } left = (left (toRight ? 1 : -1) * stepX); top = (top stepY); FixLeftTop(); this.Left = (int)left; this.Top = (int)top; } frame ; if (frame >= frameCount) frame = 0; SetBits(FrameImage); } private void FixLeftTop() { if (toRight && left > Screen.PrimaryScreen.WorkingArea.Width) { toRight = false; frame = 0; count = 0; } else if (!toRight && left < -frameWidth) { toRight = true; frame = 0; count = 0; } if (top < -frameHeight) { stepY = 1f; count = 0; } else if (top > Screen.PrimaryScreen.WorkingArea.Height) { stepY = -1f; count = 0; } } /// <summary> /// 背景图片 /// </summary> private Image FullImage { get { if (toRight) return Properties.Resources.Right; else return Properties.Resources.Left; } } /// <summary> /// 返回当前帧图片 /// </summary> public Bitmap FrameImage { get { Bitmap bitmap = new Bitmap(frameWidth, frameHeight); Graphics g = Graphics.FromImage(bitmap); g.DrawImage(FullImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(frameWidth * frame, 0, frameWidth, frameHeight), GraphicsUnit.Pixel); return bitmap; } } void frmFish_DoubleClick(object sender, EventArgs e) { this.Dispose(); } void frmFish_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { this.Left = (e.X - oldPoint.X); this.Top = (e.Y - oldPoint.Y); left = this.Left; top = this.Top; FixLeftTop(); } } void frmFish_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.Dispose(); } oldPoint = e.Location; mouseDown = true; } /// <summary> /// 设置位图显示 /// </summary> /// <param name="bitmap">位图</param> public void SetBits(Bitmap bitmap) { if (!haveHandle) return; if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; if (this.IsHandleCreated) { Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } } } }