forked from Trevor3000/RemoteControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmCaptureScreen.cs
More file actions
289 lines (264 loc) · 10.2 KB
/
Copy pathFrmCaptureScreen.cs
File metadata and controls
289 lines (264 loc) · 10.2 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RemoteControl.Protocals;
using log4net;
using System.IO;
using System.Drawing.Imaging;
using RemoteControl.Server.Utils;
namespace RemoteControl.Server
{
public partial class FrmCaptureScreen : FrmBase
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(FrmCaptureScreen));
private SocketSession oSession;
private bool _isCaptureMouse = false;
private bool _isCaptureKeyboard = false;
public FrmCaptureScreen(SocketSession session)
{
InitializeComponent();
this.oSession = session;
this.FormClosed += new FormClosedEventHandler(FrmCaptureScreen_FormClosed);
}
void FrmCaptureScreen_FormClosed(object sender, FormClosedEventArgs e)
{
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_SCREEN_REQUEST, null);
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
ToolStripButton button = sender as ToolStripButton;
button.Checked = !button.Checked;
if (button.Checked)
{
RequestStartGetScreen req = new RequestStartGetScreen();
req.fps = 5;
oSession.Send(ePacketType.PACKET_START_CAPTURE_SCREEN_REQUEST, req);
}
else
{
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_SCREEN_REQUEST, null);
}
}
public void HandleScreen(ResponseStartGetScreen resp)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<ResponseStartGetScreen>(HandleScreen), resp);
return;
}
try
{
this.pictureBox1.Image = resp.GetImage();
}
catch (Exception ex)
{
Logger.Error("HandleScreen", ex);
}
}
private void FrmCaptureScreen_Load(object sender, EventArgs e)
{
// Panel增加滚动条
this.panel1.AutoScroll = true;
// 根据图像大小,自动调节控件和Image的尺寸
this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void toolStripButtonSave_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image != null)
{
string fileName = "";
// 直接从picturebox中调用save()的话,容易出现“GDI+ 发生一般性错误”。
// 此处用bitmap对象中专一次
using (Bitmap bmp = new Bitmap(this.pictureBox1.Image))
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "*.bmp|*.bmp|*.jpg;*.jpeg|*.jpg;*.jpeg|*.*|*.*";
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = dialog.FileName;
try
{
using (var ms = new MemoryStream())
{
bmp.Save(ms, ImageFormat.Jpeg);
System.IO.File.WriteAllBytes(fileName, ms.ToArray());
}
MsgBox.Info("保存成功!");
}
catch (Exception ex)
{
MsgBox.Info("保存失败," + ex.Message);
}
}
}
}
else
{
MsgBox.Info("暂无图像,无法保存!");
}
}
#region 鼠标操作事件
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if(_isCaptureMouse)
{
RequestMouseEvent req = new RequestMouseEvent();
req.MouseButton = (eMouseButtons)e.Button;
req.MouseOperation = eMouseOperations.MouseDown;
req.MouseLocation = e.Location;
this.oSession.Send(ePacketType.PACKET_MOUSE_EVENT_REQUEST, req);
Console.WriteLine(req);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (_isCaptureMouse)
{
RequestMouseEvent req = new RequestMouseEvent();
req.MouseButton = (eMouseButtons)e.Button;
req.MouseOperation = eMouseOperations.MouseUp;
req.MouseLocation = e.Location;
this.oSession.Send(ePacketType.PACKET_MOUSE_EVENT_REQUEST, req);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//if (_isCaptureMouse)
//{
// RequestMouseEvent req = new RequestMouseEvent();
// req.MouseButton = (eMouseButtons)e.Button;
// req.MouseOperation = eMouseOperations.MouseMove;
// req.MouseLocation = e.Location;
// this.oSession.Send(ePacketType.PACKET_MOUSE_EVENT_REQUEST, req);
//}
}
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (_isCaptureMouse)
{
RequestMouseEvent req = new RequestMouseEvent();
req.MouseButton = (eMouseButtons)e.Button;
req.MouseOperation = eMouseOperations.MouseDoubleClick;
req.MouseLocation = e.Location;
this.oSession.Send(ePacketType.PACKET_MOUSE_EVENT_REQUEST, req);
}
}
#endregion
#region 键盘操作事件
private void FrmCaptureScreen_KeyDown(object sender, KeyEventArgs e)
{
if (_isCaptureKeyboard)
{
RequestKeyboardEvent req = new RequestKeyboardEvent();
req.KeyOperation = eKeyboardOpe.KeyDown;
req.KeyCode = (eKeyboardKeys)e.KeyCode;
req.KeyValue = e.KeyValue;
req.KeyData = (eKeyboardKeys)e.KeyData;
this.oSession.Send(ePacketType.PACKET_KEYBOARD_EVENT_REQUEST, req);
}
}
private void FrmCaptureScreen_KeyUp(object sender, KeyEventArgs e)
{
if (_isCaptureKeyboard)
{
RequestKeyboardEvent req = new RequestKeyboardEvent();
req.KeyOperation = eKeyboardOpe.KeyUp;
req.KeyCode = (eKeyboardKeys)e.KeyCode;
req.KeyValue = e.KeyValue;
req.KeyData = (eKeyboardKeys)e.KeyData;
this.oSession.Send(ePacketType.PACKET_KEYBOARD_EVENT_REQUEST, req);
}
}
#endregion
#region 帧率选择
/// <summary>
/// 不同的帧率的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemFPS_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item != null && item.Tag != null)
{
var parent = this.toolStripSplitButton2;
for (int i = 0; i < parent.DropDownItems.Count; i++)
{
var mItem = parent.DropDownItems[i] as ToolStripMenuItem;
if (mItem != null)
{
mItem.Checked = false;
}
}
int fps = Convert.ToInt32(item.Tag);
item.Checked = true;
RequestStartGetScreen req = new RequestStartGetScreen();
req.fps = fps;
oSession.Send(ePacketType.PACKET_START_CAPTURE_SCREEN_REQUEST, req);
}
}
/// <summary>
/// 帧率选择点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e)
{
toolStripSplitButton2.ShowDropDown();
}
#endregion
#region 捕捉操作选择
/// <summary>
/// 捕获鼠标操作按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemCaptureMouse_Click(object sender, EventArgs e)
{
toolStripMenuItemCaptureMouse.Checked = !toolStripMenuItemCaptureMouse.Checked;
_isCaptureMouse = toolStripMenuItemCaptureMouse.Checked;
}
/// <summary>
/// 捕获键盘操作按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemCaptureKeyboard_Click(object sender, EventArgs e)
{
toolStripMenuItemCaptureKeyboard.Checked = !toolStripMenuItemCaptureKeyboard.Checked;
_isCaptureKeyboard = toolStripMenuItemCaptureKeyboard.Checked;
}
/// <summary>
/// 捕获操作点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
toolStripSplitButton1.ShowDropDown();
}
#endregion
#region 窗体关闭前事件
/// <summary>
/// 窗体关闭前事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmCaptureScreen_FormClosing(object sender, FormClosingEventArgs e)
{
oSession.Send(ePacketType.PACKET_STOP_CAPTURE_SCREEN_REQUEST, null);
}
#endregion
private void ctrlAltDelToolStripMenuItem_Click(object sender, EventArgs e)
{
Win32API.keybd_event(0x11, 0, 0, 0);
Win32API.keybd_event(18, 0, 0, 0);
Win32API.keybd_event(0x2E, 0, 0, 0);
}
}
}