質問編集履歴

2

質問内容を少し修正しました

2018/07/31 05:46

投稿

mamonPro
mamonPro

スコア38

test CHANGED
File without changes
test CHANGED
@@ -28,11 +28,13 @@
28
28
 
29
29
  出力には正常に結果が反映されたところまではたどり着けました。
30
30
 
31
- しかしグローバルフック自体難易度が高く、
31
+ しかしグローバルフック自体難易度が高く、参照先のコードの意味も理解できておりません。
32
-
32
+
33
- Form1.csザイと連携していないよう
33
+ Form1.csでコーグをする画面
34
-
34
+
35
- 上記の画像のようにォーム組んで、パラメ取得、設定したる方法わかりません。
35
+ グローバルック用いてコディングをしたいのですが
36
+
37
+ その方法がわかりません。
36
38
 
37
39
 
38
40
 

1

やりたいことの詳細と、現在記述中のコードを追記しました

2018/07/31 05:46

投稿

mamonPro
mamonPro

スコア38

test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,538 @@
40
40
 
41
41
 
42
42
 
43
+ 具体的には
44
+
45
+ 上記画像でエスケープキーのキー入力が認識された際にstartX,startYの座標を
46
+
47
+ クリックしたいと考えています。
48
+
49
+ ```C#
50
+
51
+ Program.cs
52
+
53
+
54
+
55
+ using System;
56
+
57
+ using System.Windows.Forms;
58
+
59
+ using System.Runtime.InteropServices;
60
+
61
+ using System.ComponentModel;
62
+
63
+ using System.Runtime.InteropServices;
64
+
65
+
66
+
67
+ namespace garu.Testhook
68
+
69
+ {
70
+
71
+ static class TestHook
72
+
73
+ {
74
+
75
+ [STAThread]
76
+
77
+ static void Main(string[] args)
78
+
79
+ {
80
+
81
+ Application.Run(new Form1());
82
+
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+ class Form1 : Form
90
+
91
+ {
92
+
93
+ //
94
+
95
+ // DELEGATE for Hook
96
+
97
+ //
98
+
99
+ public delegate int HookHandler(
100
+
101
+ int code, WM message, IntPtr state);
102
+
103
+
104
+
105
+ //
106
+
107
+ // SetWindowsHookEx
108
+
109
+ //
110
+
111
+ [DllImport("user32.dll", SetLastError = true)]
112
+
113
+ public static extern IntPtr SetWindowsHookEx(
114
+
115
+ WH hookType, HookHandler hookDelegate, IntPtr module, uint threadId);
116
+
117
+
118
+
119
+ //
120
+
121
+ // HookType
122
+
123
+ //
124
+
125
+ public enum WH
126
+
127
+ {
128
+
129
+ KEYBOARD_LL = 13,
130
+
131
+ MOUSE_LL = 14,
132
+
133
+ }
134
+
135
+
136
+
137
+ //
138
+
139
+ // UnhookWindowsHookEx
140
+
141
+ //
142
+
143
+ [DllImport("user32.dll", SetLastError = true)]
144
+
145
+ public static extern bool UnhookWindowsHookEx(IntPtr hook);
146
+
147
+
148
+
149
+ //
150
+
151
+ // CallNextHookEx
152
+
153
+ //
154
+
155
+ [DllImport("user32.dll")]
156
+
157
+ public static extern int CallNextHookEx(
158
+
159
+ IntPtr hook, int code, WM message, IntPtr state);
160
+
161
+
162
+
163
+ [DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
164
+
165
+ static extern void SetCursorPos(int X, int Y);
166
+
167
+
168
+
169
+ [DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
170
+
171
+ static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
172
+
173
+
174
+
175
+ private const int MOUSEEVENTF_LEFTDOWN = 0x2;
176
+
177
+ private const int MOUSEEVENTF_LEFTUP = 0x4;
178
+
179
+
180
+
181
+ //
182
+
183
+ // Win32 Message
184
+
185
+ //
186
+
187
+ public enum WM
188
+
189
+ {
190
+
191
+ KEYDOWN = 0x0100,
192
+
193
+ KEYUP = 0x0101,
194
+
195
+ SYSKEYDOWN = 0x0104,
196
+
197
+ SYSKEYUP = 0x0105,
198
+
199
+ MOUSEMOVE = 0x0200,
200
+
201
+ LBUTTONDOWN = 0x0201,
202
+
203
+ LBUTTONUP = 0x0202,
204
+
205
+ LBUTTONDBLCLK = 0x0203,
206
+
207
+ RBUTTONDOWN = 0x0204,
208
+
209
+ RBUTTONUP = 0x0205,
210
+
211
+ RBUTTONDBLCLK = 0x0206,
212
+
213
+ MBUTTONDOWN = 0x0207,
214
+
215
+ MBUTTONUP = 0x0208,
216
+
217
+ MBUTTONDBLCLK = 0x0209,
218
+
219
+ MOUSEWHEEL = 0x020A,
220
+
221
+ XBUTTONDOWN = 0x020B,
222
+
223
+ XBUTTONUP = 0x020C,
224
+
225
+ XBUTTONDBLCLK = 0x020D,
226
+
227
+ MOUSEHWHEEL = 0x020E,
228
+
229
+ }
230
+
231
+
232
+
233
+ //
234
+
235
+ // (private) Field
236
+
237
+ //
238
+
239
+ HookHandler hookDelegate;
240
+
241
+ IntPtr hook;
242
+
243
+
244
+
245
+ public Form1()
246
+
247
+ {
248
+
249
+ WH hookType = WH.KEYBOARD_LL;
250
+
251
+ hookDelegate = new HookHandler(OnHook);
252
+
253
+ IntPtr hMod = Marshal.GetHINSTANCE(
254
+
255
+ System.Reflection.Assembly.
256
+
257
+ GetExecutingAssembly().GetModules()[0]);
258
+
259
+ hook = SetWindowsHookEx(hookType, hookDelegate, hMod, 0);
260
+
261
+ if (hook == IntPtr.Zero)
262
+
263
+ {
264
+
265
+ int errorCode = Marshal.GetLastWin32Error();
266
+
267
+ throw new Win32Exception(errorCode);
268
+
269
+ }
270
+
271
+ }
272
+
273
+
274
+
275
+ protected override void OnClosing(CancelEventArgs e)
276
+
277
+ {
278
+
279
+ base.OnClosing(e);
280
+
281
+ if (hook != IntPtr.Zero)
282
+
283
+ {
284
+
285
+ UnhookWindowsHookEx(hook);
286
+
287
+ }
288
+
289
+ }
290
+
291
+
292
+
293
+ int OnHook(int code, WM message, IntPtr state)
294
+
295
+ {
296
+
297
+ SetCursorPos(1846, 260); //対象の座標をここで代入
298
+
299
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
300
+
301
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
302
+
303
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
304
+
305
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
306
+
307
+
308
+
309
+ Console.WriteLine("message={0}", message);
310
+
311
+ return CallNextHookEx(hook, code, message, state);
312
+
313
+ }
314
+
315
+ }
316
+
317
+ }
318
+
319
+
320
+
321
+ ```
322
+
323
+ 別プロジェクトで
324
+
325
+ ```c#
326
+
327
+ 添付画像のプロジェクト
328
+
329
+ Form1cs
330
+
331
+
332
+
333
+ using System;
334
+
335
+ using System.Collections.Generic;
336
+
337
+ using System.ComponentModel;
338
+
339
+ using System.Data;
340
+
341
+ using System.Drawing;
342
+
343
+ using System.Linq;
344
+
345
+ using System.Text;
346
+
347
+ using System.Threading.Tasks;
348
+
349
+ using System.Windows.Forms;
350
+
351
+ using System.Runtime.InteropServices;
352
+
353
+ using System.Threading;
354
+
355
+
356
+
357
+ namespace test0630
358
+
359
+ {
360
+
361
+ public partial class Form1 : Form
362
+
363
+ {
364
+
365
+ [DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
366
+
367
+ static extern void SetCursorPos(int X, int Y);
368
+
369
+ [DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
370
+
371
+ static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
372
+
373
+
374
+
375
+ private const int MOUSEEVENTF_LEFTDOWN = 0x2;
376
+
377
+ private const int MOUSEEVENTF_LEFTUP = 0x4;
378
+
379
+ int startX;
380
+
381
+ int startY;
382
+
383
+ int endX;
384
+
385
+ int endY;
386
+
387
+
388
+
389
+ public Form1()
390
+
391
+ {
392
+
393
+ InitializeComponent();
394
+
395
+ timer1.Start();
396
+
397
+ }
398
+
399
+
400
+
401
+ private void timer1_Tick(object sender, EventArgs e)
402
+
403
+ {
404
+
405
+ valueX.Text = (System.Windows.Forms.Cursor.Position.X).ToString();
406
+
407
+ valueY.Text = (System.Windows.Forms.Cursor.Position.Y).ToString();
408
+
409
+ }
410
+
411
+
412
+
413
+ private void Form1_KeyDown(object sender, KeyEventArgs e)
414
+
415
+ {
416
+
417
+ if (e.KeyData == Keys.Escape)
418
+
419
+ {
420
+
421
+ startX = int.Parse(connectS_X.Text);
422
+
423
+ startY = int.Parse(connectS_Y.Text);
424
+
425
+ endX = int.Parse(connectE_X.Text);
426
+
427
+ endY = int.Parse(connectE_Y.Text);
428
+
429
+ SetCursorPos(startX, startY);
430
+
431
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
432
+
433
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
434
+
435
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
436
+
437
+ System.Threading.Thread.Sleep(300);
438
+
439
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
440
+
441
+ SetCursorPos(50, 50);
442
+
443
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
444
+
445
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
446
+
447
+ }
448
+
449
+ else if (e.KeyData == Keys.F1)
450
+
451
+ {
452
+
453
+ startX = int.Parse(connectS_X.Text);
454
+
455
+ startY = int.Parse(connectS_Y.Text);
456
+
457
+ endX = int.Parse(connectE_X.Text);
458
+
459
+ endY = int.Parse(connectE_Y.Text);
460
+
461
+ SetCursorPos(endX, endY);
462
+
463
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
464
+
465
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
466
+
467
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
468
+
469
+ System.Threading.Thread.Sleep(300);
470
+
471
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
472
+
473
+ SetCursorPos(50, 50);
474
+
475
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
476
+
477
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
478
+
479
+ }
480
+
481
+ }
482
+
483
+
484
+
485
+ private void textBox1_KeyDown(object sender, KeyEventArgs e)
486
+
487
+ {
488
+
489
+ if (e.KeyData == Keys.Escape)
490
+
491
+ {
492
+
493
+ startX = int.Parse(connectS_X.Text);
494
+
495
+ startY = int.Parse(connectS_Y.Text);
496
+
497
+ endX = int.Parse(connectE_X.Text);
498
+
499
+ endY = int.Parse(connectE_Y.Text);
500
+
501
+ SetCursorPos(startX, startY);
502
+
503
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
504
+
505
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
506
+
507
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
508
+
509
+ System.Threading.Thread.Sleep(300);
510
+
511
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
512
+
513
+ SetCursorPos(50, 50);
514
+
515
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
516
+
517
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
518
+
519
+ }
520
+
521
+ else if (e.KeyData == Keys.F1)
522
+
523
+ {
524
+
525
+ startX = int.Parse(connectS_X.Text);
526
+
527
+ startY = int.Parse(connectS_Y.Text);
528
+
529
+ endX = int.Parse(connectE_X.Text);
530
+
531
+ endY = int.Parse(connectE_Y.Text);
532
+
533
+ SetCursorPos(endX, endY);
534
+
535
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
536
+
537
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
538
+
539
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
540
+
541
+ System.Threading.Thread.Sleep(300);
542
+
543
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
544
+
545
+ SetCursorPos(50, 50);
546
+
547
+ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
548
+
549
+ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
550
+
551
+ }
552
+
553
+ }
554
+
555
+
556
+
557
+ ~~文字数の関係で省いてます~~
558
+
559
+ }
560
+
561
+ }
562
+
563
+
564
+
565
+
566
+
567
+ ```
568
+
569
+ これを1つに取りまとめたいというのが
570
+
571
+ やりたいことになりますが、その他不足情報がありましたら
572
+
573
+ 指示ください。
574
+
43
575
 
44
576
 
45
577
  開発環境