-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
350 lines (314 loc) · 12 KB
/
MainWindow.xaml.cs
File metadata and controls
350 lines (314 loc) · 12 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using WpfAnimatedGif;
namespace PandaDesktopApp
{
public partial class MainWindow : Window
{
enum Direction
{
Left,
Right,
Back,
Front,
Stand,
RollLeft,
RollRight
}
DispatcherTimer moveTimer = new DispatcherTimer();
DispatcherTimer directionTimer = new DispatcherTimer();
double pandaX = 0;
double pandaY = 0;
private Direction currentDirection = Direction.Stand;
private bool isDragging = false;
private Point mouseOffset;
string gifPathStand = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_walk_stand.gif");
string gifPathLeft = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_walk_left.gif");
string gifPathRight = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_walk_right.gif");
string gifPathFront = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_walk_front.gif");
string gifPathBack = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_walk_back.gif");
string giftPathRollRight = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_roll_right.gif");
string giftPathRollLeft = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "panda_roll_left.gif");
BitmapImage imageWalkLeft = new BitmapImage();
BitmapImage imageWalkRight = new BitmapImage();
BitmapImage imageWalkStand = new BitmapImage();
BitmapImage imageWalkFront = new BitmapImage();
BitmapImage imageWalkBack = new BitmapImage();
BitmapImage imagRollRight = new BitmapImage();
BitmapImage imagRollLeft = new BitmapImage();
public MainWindow()
{
InitializeComponent();
imageWalkLeft.BeginInit();
imageWalkLeft.UriSource = new Uri(gifPathLeft);
imageWalkLeft.EndInit();
imageWalkRight.BeginInit();
imageWalkRight.UriSource = new Uri(gifPathRight);
imageWalkRight.EndInit();
imageWalkFront.BeginInit();
imageWalkFront.UriSource = new Uri(gifPathFront);
imageWalkFront.EndInit();
imageWalkBack.BeginInit();
imageWalkBack.UriSource = new Uri(gifPathBack);
imageWalkBack.EndInit();
imageWalkStand.BeginInit();
imageWalkStand.UriSource = new Uri(gifPathStand);
imageWalkStand.EndInit();
imagRollRight.BeginInit();
imagRollRight.UriSource = new Uri(giftPathRollRight);
imagRollRight.EndInit();
imagRollLeft.BeginInit();
imagRollLeft.UriSource = new Uri(giftPathRollLeft);
imagRollLeft.EndInit();
PandaImage.Source = imageWalkStand;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkStand);
RandomStartingPoint();
pandaY = TaskbarHelper.GetTaskbarY() - 40;
RandomDirection();
Canvas.SetTop(PandaImage, pandaY);
Loaded += MainWindow_Loaded;
// In your MainWindow constructor, after InitializeComponent():
PandaImage.MouseLeftButtonDown += PandaImage_MouseLeftButtonDown;
PandaImage.MouseMove += PandaImage_MouseMove;
PandaImage.MouseLeftButtonUp += PandaImage_MouseLeftButtonUp;
moveTimer.Interval = TimeSpan.FromMilliseconds(250);
moveTimer.Tick += MoveTimer_Tick;
moveTimer.Start();
directionTimer.Interval = TimeSpan.FromSeconds((new Random()).Next(5, 30));
directionTimer.Tick += (s, e) =>
{
RandomDirection();
};
directionTimer.Start();
}
private void RollLeft()
{
if (currentDirection != Direction.RollLeft)
{
currentDirection = Direction.RollLeft;
PandaImage.Source = imagRollLeft;
ImageBehavior.SetAnimatedSource(PandaImage, imagRollLeft);
}
}
private void RollRight()
{
if (currentDirection != Direction.RollRight)
{
currentDirection = Direction.RollRight;
PandaImage.Source = imagRollRight;
ImageBehavior.SetAnimatedSource(PandaImage, imagRollRight);
}
}
private void WalkStand()
{
if (currentDirection != Direction.Stand)
{
currentDirection = Direction.Stand;
PandaImage.Source = imageWalkStand;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkStand);
}
}
private void WalkLeft()
{
if (currentDirection != Direction.Left)
{
currentDirection = Direction.Left;
PandaImage.Source = imageWalkLeft;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkLeft);
}
}
private void WalkRight()
{
if (currentDirection != Direction.Right)
{
currentDirection = Direction.Right;
PandaImage.Source = imageWalkRight;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkRight);
}
}
private void WalkFront()
{
if (currentDirection != Direction.Front)
{
currentDirection = Direction.Front;
PandaImage.Source = imageWalkFront;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkFront);
}
}
private void WalkBack()
{
if (currentDirection != Direction.Back)
{
currentDirection = Direction.Back;
PandaImage.Source = imageWalkBack;
ImageBehavior.SetAnimatedSource(PandaImage, imageWalkBack);
}
}
private void RandomDirection()
{
var random = new Random();
int randomDirection = random.Next(0, 22);
if (randomDirection <= 4)
{
WalkLeft();
}
else if (randomDirection >= 5 && randomDirection <= 9)
{
WalkRight();
}
else if (randomDirection >= 10 && randomDirection <= 12)
{
WalkFront();
}
else if (randomDirection >= 13 && randomDirection <= 15)
{
WalkBack();
}
else if (randomDirection >= 16 && randomDirection <= 17)
{
RollLeft();
}
else if (randomDirection >= 18 && randomDirection <= 19)
{
RollRight();
}
else
{
WalkStand();
}
if (currentDirection == Direction.Stand)
{
directionTimer.Interval = TimeSpan.FromSeconds((new Random()).Next(2, 5));
}
else if (currentDirection == Direction.Front || currentDirection == Direction.Back || currentDirection == Direction.RollLeft || currentDirection == Direction.RollRight)
{
directionTimer.Interval = TimeSpan.FromSeconds((new Random()).Next(2, 10));
}
else
{
directionTimer.Interval = TimeSpan.FromSeconds((new Random()).Next(20, 60));
}
}
private void RandomStartingPoint()
{
var random = new Random();
double x = random.Next(0, Convert.ToInt16(SystemParameters.PrimaryScreenWidth - 80));
double y = random.Next(0, Convert.ToInt16(SystemParameters.PrimaryScreenHeight - 80));
pandaX = x;
pandaY = y;
}
// Add these event handlers to your MainWindow class
private void PandaImage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
isDragging = true;
moveTimer.Stop(); // Pause automatic movement
directionTimer.Stop(); // Pause random direction changes
PandaImage.CaptureMouse();
var mousePos = e.GetPosition(MainCanvas);
mouseOffset = new Point(mousePos.X - pandaX, mousePos.Y - pandaY);
}
private void PandaImage_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (isDragging)
{
var mousePos = e.GetPosition(MainCanvas);
pandaX = mousePos.X - mouseOffset.X;
pandaY = mousePos.Y - mouseOffset.Y;
Canvas.SetLeft(PandaImage, pandaX);
Canvas.SetTop(PandaImage, pandaY);
}
}
private void PandaImage_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (isDragging)
{
isDragging = false;
PandaImage.ReleaseMouseCapture();
RandomDirection();
directionTimer.Start(); // Resume random direction changes
moveTimer.Start(); // Resume automatic movement
}
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//MakeWindowTransparent();
PositionWindowOnDesktop();
}
private void MoveTimer_Tick(object sender, EventArgs e)
{
double screenWidth = SystemParameters.PrimaryScreenWidth;
double pandaWidth = PandaImage.ActualWidth;
if (currentDirection== Direction.Right)
{
pandaX += 5;
if (pandaX > screenWidth - pandaWidth)
{
WalkLeft();
}
}
else if( currentDirection == Direction.Left)
{
pandaX -= 5;
if (pandaX < 0)
{
WalkRight();
}
}
if (currentDirection == Direction.RollRight)
{
pandaX += 10;
if (pandaX > screenWidth - pandaWidth)
{
RollLeft();
}
}
else if (currentDirection == Direction.RollLeft)
{
pandaX -= 10;
if (pandaX < 0)
{
RollRight();
}
}
Canvas.SetLeft(PandaImage, pandaX);
}
// Make window click-through
private void MakeWindowTransparent()
{
var hwnd = new WindowInteropHelper(this).Handle;
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
}
// Optional: Set to desktop layer
private void PositionWindowOnDesktop()
{
Left = 0;
Top = 0;
Width = SystemParameters.PrimaryScreenWidth;
Height = SystemParameters.PrimaryScreenHeight;
}
#region Win32 API
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
const int LWA_ALPHA = 0x2;
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
#endregion
private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}