-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowResizeListener.cs
61 lines (50 loc) · 1.52 KB
/
WindowResizeListener.cs
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
using System;
using System.Timers;
namespace PopCat
{
public class WindowResizeListener
{
private readonly IntPtr _hWnd;
private Tuple<int, int> _size;
private readonly Timer _timer;
public EventHandler<WindowResizeEventArgs> OnResize;
public WindowResizeListener(IntPtr hWnd)
{
_hWnd = hWnd;
_size = Win32Utils.GetWindowDimensions(_hWnd);
_timer = new Timer() {Interval = 10};
_timer.Elapsed += (_, _) => CheckResize();
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
private void CheckResize()
{
var currentSize = Win32Utils.GetWindowDimensions(_hWnd);
var (width, height) = currentSize;
var (x, y) = Win32Utils.GetWindowPosition(_hWnd);
if (Equals(currentSize, _size)) return;
_size = currentSize;
var args = new WindowResizeEventArgs()
{
Width = width,
Height = height,
X = x,
Y = y
};
OnResize.Invoke(this, args);
}
public class WindowResizeEventArgs : EventArgs
{
public int Width { get; init; }
public int Height { get; init; }
public int X { get; init; }
public int Y { get; init; }
}
}
}