-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
executable file
·262 lines (223 loc) · 9.1 KB
/
MainWindow.xaml.cs
File metadata and controls
executable file
·262 lines (223 loc) · 9.1 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
using System.Windows.Interop;
using Microsoft.Win32;
using Prefab;
using PrefabWinAPI;
namespace PrefabVideoCapture
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_state = State.None;
}
CaptureThread _capture;
LowLevelMouseHook _mousehook;
Tree _selectedWindow;
WindowHighlight _windowHiglight;
State _state;
BackgroundWorker _windowGetter;
IEnumerable<Tree> _windows;
WindowStreamCapture _windowEnumerator;
enum UpdateType
{
EnableRecord,
DisableRecord,
HighlightWindow,
DisableSelecting,
EnableSelecting,
ChangeRecordToStopRecording,
ChangeStopRecordingToRecord
}
enum State
{
Recording,
Selecting,
SelectedButNotRecording,
None
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_windowGetter = new BackgroundWorker();
_windowGetter.DoWork += new DoWorkEventHandler(_windowGetter_DoWork);
_windowGetter.RunWorkerAsync();
_windowHiglight = new WindowHighlight();
_windowHiglight.Owner = this;
_updateUI = new UpdateDel(UpdateUI);
_mousehook = new LowLevelMouseHook("low level mouse hook");
_mousehook.OnMouseMove += new EventHandler<System.Windows.Forms.MouseEventArgs>(_mousehook_OnMouseMove);
_mousehook.OnMouseDown += new EventHandler<System.Windows.Forms.MouseEventArgs>(_mousehook_OnMouseDown);
_mousehook.Install();
}
void _windowGetter_DoWork(object sender, DoWorkEventArgs e)
{
_windowEnumerator = new WindowStreamCapture();
while (true)
{
_windows = _windowEnumerator.GetAllWindowsWithoutPixels().Where(ShouldConsiderWindow);
Thread.Sleep(100);
}
}
private delegate void UpdateDel(UpdateType updateType);
private UpdateDel _updateUI;
private void UpdateUI(UpdateType updatetype)
{
switch (updatetype)
{
case UpdateType.EnableRecord:
RecordButton.IsEnabled = true;
break;
case UpdateType.DisableRecord:
RecordButton.IsEnabled = false;
break;
case UpdateType.ChangeRecordToStopRecording:
RecordButton.Content = "Stop Recording";
break;
case UpdateType.ChangeStopRecordingToRecord:
RecordButton.Content = "Record";
break;
case UpdateType.HighlightWindow:
_windowHiglight.Left = _selectedWindow.Left;
_windowHiglight.Top = _selectedWindow.Top;
_windowHiglight.Width = _selectedWindow.Width;
_windowHiglight.Height = _selectedWindow.Height;
_windowHiglight.Show();
break;
case UpdateType.DisableSelecting:
SelectAWindowButton.IsEnabled = false;
break;
case UpdateType.EnableSelecting:
SelectAWindowButton.IsEnabled = true;
break;
default:
break;
}
}
void _mousehook_OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch (_state)
{
case State.Selecting:
_state = State.SelectedButNotRecording;
Dispatcher.BeginInvoke(_updateUI, UpdateType.EnableRecord);
Dispatcher.BeginInvoke(_updateUI, UpdateType.EnableSelecting);
break;
default: //We don't care about global mouse events unless we're selecting a window to record. So do nothing here.
break;
}
}
void _mousehook_OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch(_state){
case State.Selecting:
IEnumerable<Tree> windows = _windows;
Tree underCursor = windows.FirstOrDefault((wo) => BoundingBox.Contains(wo, e.X, e.Y));
//Win32.POINT pt = new Win32.POINT();
//pt.x = e.X;
//pt.y = e.Y;
//WindowOccurrence underCursor = _windowEnumerator.CreateOccurrence(Win32.WindowFromPoint(pt));
if (underCursor != null)
{
_selectedWindow = underCursor;
Dispatcher.BeginInvoke(_updateUI, UpdateType.HighlightWindow);
}
break;
}
}
private bool ShouldConsiderWindow(Tree window)
{
IntPtr highlightHandle = IntPtr.Zero;
if (_windowHiglight != null)
highlightHandle = _windowHiglight.Handle;
return window != null &&
(((IntPtr)window["handle"]) != highlightHandle) &&
((Win32.WindowStyles)window["style"] & Win32.WindowStyles.WS_ICONIC) != Win32.WindowStyles.WS_ICONIC && // not minimized
((Win32.WindowStyles)window["style"] & Win32.WindowStyles.WS_CHILD) != Win32.WindowStyles.WS_CHILD && //not a child window
((Win32.WindowStyles)window["style"] & Win32.WindowStyles.WS_VISIBLE) == Win32.WindowStyles.WS_VISIBLE &&
((( (Win32.WindowStyles)window["style"] & Win32.WindowStyles.WS_POPUP) != Win32.WindowStyles.WS_POPUP ||
((Win32.WindowStyles)window["exstyle"] & Win32.WindowStyles.WS_EX_TOOLWINDOW) != Win32.WindowStyles.WS_EX_TOOLWINDOW) || window.Height > 29) && //is not a tooltip (not a popup or not a tool window or bigger than 28 high). (28 px is a hack to distinguish tooltips from other windows).
(window.Width < System.Windows.SystemParameters.VirtualScreenWidth ||
window.Height < System.Windows.SystemParameters.VirtualScreenHeight);
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if(RecordButton.IsChecked.Value == true)
e.Cancel = true;
}
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
if (_selectedWindow != null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
sfd.DefaultExt = ".avi";
if (sfd.ShowDialog().Value)
{
_state = State.Recording;
_capture = new CaptureThread(sfd.FileName);
_capture.UsePrintWindow = UsePrintWindow.IsChecked.Value;
_capture.Start((IntPtr)_selectedWindow["handle"]);
Dispatcher.BeginInvoke(_updateUI, UpdateType.ChangeRecordToStopRecording);
Dispatcher.BeginInvoke(_updateUI, UpdateType.DisableSelecting);
}
else
{
RecordButton.IsChecked = false;
}
}
else
{
RecordButton.IsChecked = false;
}
}
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
if (_state == State.Recording)
{
_state = State.SelectedButNotRecording;
_capture.Stop();
Dispatcher.BeginInvoke(_updateUI, UpdateType.ChangeStopRecordingToRecord);
Dispatcher.BeginInvoke(_updateUI, UpdateType.EnableSelecting);
}
}
private void SelectAWindowButton_Click(object sender, RoutedEventArgs e)
{
switch (_state)
{
case State.None:
case State.SelectedButNotRecording:
_state = State.Selecting;
Dispatcher.BeginInvoke(_updateUI, UpdateType.DisableSelecting);
break;
}
}
private void UsePrintWindow_Checked_1(object sender, RoutedEventArgs e)
{
if(_capture != null)
_capture.UsePrintWindow = true;
}
private void UsePrintWindow_Unchecked_1(object sender, RoutedEventArgs e)
{
if(_capture != null)
_capture.UsePrintWindow = false;
}
}
}