回答編集履歴

1

コード修正

2019/09/19 14:26

投稿

toki_td
toki_td

スコア2850

test CHANGED
@@ -66,7 +66,15 @@
66
66
 
67
67
  timer.Dispose();
68
68
 
69
+ var scr = Screen.FromHandle(Handle);
70
+
71
+ getClickLocation(scr, p => {
72
+
73
+ Visible = true;
74
+
69
- runScreenShot();
75
+ showProcessOfPoint(p);
76
+
77
+ });
70
78
 
71
79
  };
72
80
 
@@ -74,20 +82,70 @@
74
82
 
75
83
  }
76
84
 
77
-
78
-
85
+
86
+
79
- private void runScreenShot()
87
+ private void getClickLocation(Screen screen, Action<Point> clickHandler)
80
-
88
+
81
- {
89
+ {
82
-
90
+
83
- // とりあえず現在ウィンドウがいるスクリーンだけ対象に、スクリーンショットを撮る
91
+ // スクリーンショットを全画面で表示
92
+
84
-
93
+ var screenShotForm = new Form();
94
+
95
+ screenShotForm.FormBorderStyle = FormBorderStyle.None;
96
+
97
+ screenShotForm.DesktopBounds = screen.Bounds;
98
+
99
+
100
+
85
- var screen = Screen.FromHandle(Handle);
101
+ var sc = screenShot(screen);
102
+
103
+
104
+
105
+ screenShotForm.Paint += (s, a) => {
106
+
107
+ a.Graphics.DrawImage(sc, 0, 0);
108
+
109
+ };
110
+
111
+
112
+
113
+ screenShotForm.MouseDown += (s, a) => {
114
+
115
+ if (a.Button == MouseButtons.Left) {
116
+
117
+ screenShotForm.Close();
118
+
119
+ clickHandler(a.Location);
120
+
121
+ }
122
+
123
+ };
124
+
125
+
126
+
127
+ screenShotForm.FormClosed += (s, a) => {
128
+
129
+ screenShotForm.Dispose();
130
+
131
+ screenShotForm = null;
132
+
133
+ };
134
+
135
+
136
+
137
+ screenShotForm.Show();
138
+
139
+ }
140
+
141
+
142
+
143
+ private Image screenShot(Screen screen)
144
+
145
+ {
86
146
 
87
147
  var bounds = screen.Bounds;
88
148
 
89
-
90
-
91
149
  var image = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
92
150
 
93
151
 
@@ -106,75 +164,37 @@
106
164
 
107
165
  }
108
166
 
109
-
110
-
111
- // スクリーンショットを全画面で表示
112
-
113
- var screenShotForm = new Form();
114
-
115
- screenShotForm.FormBorderStyle = FormBorderStyle.None;
116
-
117
- screenShotForm.DesktopBounds = bounds;
118
-
119
-
120
-
121
- screenShotForm.Paint += (s, a) => {
122
-
123
- a.Graphics.DrawImage(image, 0, 0);
167
+ return image;
124
-
168
+
125
- };
169
+ }
126
-
127
-
128
-
129
- screenShotForm.MouseDown += (s, a) => {
170
+
130
-
171
+
172
+
131
- if (a.Button == MouseButtons.Left) {
173
+ private void showProcessOfPoint(Point p)
132
-
133
- screenShotForm.Close();
174
+
134
-
135
- Visible = true;
175
+ {
136
-
137
-
138
-
176
+
139
- // クリックした位置からウィンドウ -> プロセスID -> プロセスを取得、表示
177
+ // クリックした位置からウィンドウ -> プロセスID -> プロセスを取得、表示
140
-
178
+
141
- var hwnd = WindowFromPoint(a.Location);
179
+ var hwnd = WindowFromPoint(p);
142
-
143
-
144
-
180
+
181
+
182
+
145
- if (hwnd != IntPtr.Zero) {
183
+ if (hwnd != IntPtr.Zero) {
146
-
184
+
147
- if (GetWindowThreadProcessId(hwnd, out var processId) != 0) {
185
+ if (GetWindowThreadProcessId(hwnd, out var processId) != 0) {
148
-
186
+
149
- var process = Process.GetProcessById(processId);
187
+ var process = Process.GetProcessById(processId);
150
-
188
+
151
- MessageBox.Show(process.ProcessName);
189
+ MessageBox.Show(process.ProcessName);
152
-
153
- }
154
-
155
- } else {
156
-
157
- MessageBox.Show("No Window");
158
-
159
- }
160
190
 
161
191
  }
162
192
 
193
+ } else {
194
+
195
+ MessageBox.Show("No Window");
196
+
163
- };
197
+ }
164
-
165
-
166
-
167
- screenShotForm.FormClosed += (s, a) => {
168
-
169
- screenShotForm.Dispose();
170
-
171
- screenShotForm = null;
172
-
173
- };
174
-
175
-
176
-
177
- screenShotForm.Show();
178
198
 
179
199
  }
180
200