实例介绍
【实例简介】GDI绘制可爱的红色游动金鱼,可以看到鱼鳍不停摆动
【实例截图】
【核心代码】
//########################################################################## //★★★★★★★ http://www.cnpopsoft.com ★★★★★★★ //★★ VB & C# source code and articles for free !!! ★★ //★★★★★★★ Davidwu ★★★★★★★ //########################################################################## using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace RotateTransformDemo { public partial class FishForm : 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; public FishForm() { InitializeComponent(); this.TopMost = true; 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(Form2_DoubleClick); this.MouseDown = new MouseEventHandler(Form2_MouseDown); this.MouseUp = new MouseEventHandler(Form2_MouseUp); this.MouseMove = new MouseEventHandler(Form2_MouseMove); } #region Override protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; base.OnClosing(e); haveHandle = false; } protected override void OnHandleCreated(EventArgs e) { InitializeStyles(); base.OnHandleCreated(e); haveHandle = true; } protected override CreateParams CreateParams { get { CreateParams cParms = base.CreateParams; cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED return cParms; } } #endregion void Form2_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 0.5f; stepY = ((float)new Random().NextDouble() - 0.5f) * 0.5f; } 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; } } private Image FullImage { get { if (toRight) return RotateTransformDemo.Properties.Resources.Right; else return RotateTransformDemo.Properties.Resources.Left; } } 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 Form2_DoubleClick(object sender, EventArgs e) { this.Dispose(); } void Form2_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 Form2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) this.Dispose(); oldPoint = e.Location; mouseDown = true; } public void SetBits(Bitmap bitmap) { if (!haveHandle) return; if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("The picture must be 32bit picture with alpha channel."); 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; 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); } } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论