teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード修正

2019/09/19 14:26

投稿

toki_td
toki_td

スコア2850

answer CHANGED
@@ -32,52 +32,32 @@
32
32
  timer.Interval = 250;
33
33
  timer.Tick += (s, a) => {
34
34
  timer.Dispose();
35
+ var scr = Screen.FromHandle(Handle);
36
+ getClickLocation(scr, p => {
37
+ Visible = true;
35
- runScreenShot();
38
+ showProcessOfPoint(p);
39
+ });
36
40
  };
37
41
  timer.Start();
38
42
  }
39
-
43
+
40
- private void runScreenShot()
44
+ private void getClickLocation(Screen screen, Action<Point> clickHandler)
41
45
  {
42
- // とりあえず現在ウィンドウがいるスクリーンだけ対象に、スクリーンショットを撮る
43
- var screen = Screen.FromHandle(Handle);
44
- var bounds = screen.Bounds;
45
-
46
- var image = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
47
-
48
- using (var g = Graphics.FromImage(image)) {
49
- g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, new Size(bounds.Width, bounds.Height));
50
- g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
51
- using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255))) {
52
- g.FillRectangle(brush, 0, 0, bounds.Width, bounds.Height);
53
- }
54
- }
55
-
56
46
  // スクリーンショットを全画面で表示
57
47
  var screenShotForm = new Form();
58
48
  screenShotForm.FormBorderStyle = FormBorderStyle.None;
59
- screenShotForm.DesktopBounds = bounds;
49
+ screenShotForm.DesktopBounds = screen.Bounds;
60
50
 
51
+ var sc = screenShot(screen);
52
+
61
53
  screenShotForm.Paint += (s, a) => {
62
- a.Graphics.DrawImage(image, 0, 0);
54
+ a.Graphics.DrawImage(sc, 0, 0);
63
55
  };
64
56
 
65
57
  screenShotForm.MouseDown += (s, a) => {
66
58
  if (a.Button == MouseButtons.Left) {
67
59
  screenShotForm.Close();
68
- Visible = true;
69
-
70
- // クリックした位置からウィンドウ -> プロセスID -> プロセスを取得、表示
71
- var hwnd = WindowFromPoint(a.Location);
60
+ clickHandler(a.Location);
72
-
73
- if (hwnd != IntPtr.Zero) {
74
- if (GetWindowThreadProcessId(hwnd, out var processId) != 0) {
75
- var process = Process.GetProcessById(processId);
76
- MessageBox.Show(process.ProcessName);
77
- }
78
- } else {
79
- MessageBox.Show("No Window");
80
- }
81
61
  }
82
62
  };
83
63
 
@@ -88,6 +68,36 @@
88
68
 
89
69
  screenShotForm.Show();
90
70
  }
71
+
72
+ private Image screenShot(Screen screen)
73
+ {
74
+ var bounds = screen.Bounds;
75
+ var image = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
76
+
77
+ using (var g = Graphics.FromImage(image)) {
78
+ g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, new Size(bounds.Width, bounds.Height));
79
+ g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
80
+ using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255))) {
81
+ g.FillRectangle(brush, 0, 0, bounds.Width, bounds.Height);
82
+ }
83
+ }
84
+ return image;
85
+ }
86
+
87
+ private void showProcessOfPoint(Point p)
88
+ {
89
+ // クリックした位置からウィンドウ -> プロセスID -> プロセスを取得、表示
90
+ var hwnd = WindowFromPoint(p);
91
+
92
+ if (hwnd != IntPtr.Zero) {
93
+ if (GetWindowThreadProcessId(hwnd, out var processId) != 0) {
94
+ var process = Process.GetProcessById(processId);
95
+ MessageBox.Show(process.ProcessName);
96
+ }
97
+ } else {
98
+ MessageBox.Show("No Window");
99
+ }
100
+ }
91
101
  }
92
102
  }
93
103
  ```