【核心代码】
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
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);
}
}
}
} |