基本信息
源码名称:C# 半透明桌面宠物红色金鱼
源码大小:1.74M
文件格式:.zip
开发语言:C#
更新时间:2018-05-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
半透明桌面宠物红色金鱼
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();
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 重载
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 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 RotateTransformDemo.Properties.Resources.Right;
else
return RotateTransformDemo.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 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("图片必须是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;
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);
}
}
}
}