回答編集履歴

1

見直しキャンペーン中

2023/07/21 10:32

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,453 +1,226 @@
1
1
  「文字入力支援君」はradianさんの言うようにおそらく、
2
-
3
2
  1. 対象アプリをアクティブ化
4
-
5
3
  1. クリップボードに文字をコピー
6
-
7
4
  1. Ctrl+Vを送る
8
5
 
9
-
10
-
11
6
  という動作をしています。
12
7
 
13
-
14
-
15
8
  対象アプリがアクティブ化したときにカーソルがない(ボタン等にフォーカスがある)と貼り付けられないので、特に入力エリアを探したりはしていなそうです(特定アプリに特別対応があるかもしれませんが)
16
9
 
17
-
18
-
19
10
  最低限動作確認できるサンプルを作りました。
20
-
21
11
  ##### 使い方
22
-
23
12
  1. 支援君のようにピンクの四角を対象アプリにドラッグ&ドロップ
24
-
25
13
  1. テキストボックスに何か入力
26
-
27
14
  1. 貼り付けボタンを押す
28
15
 
29
-
30
-
31
16
  ##### 注意
32
-
33
17
  * エラーチェック一切なし
34
-
35
18
  * Word・Excelは手元にないので未確認
36
19
 
37
-
38
-
39
- Form1.cs
20
+ ```cs:Form1.cs
40
-
41
- ```C#
42
-
43
21
  using System;
44
-
45
22
  using System.Drawing;
46
-
47
23
  using System.Runtime.InteropServices;
48
-
49
24
  using System.Text;
50
-
51
25
  using System.Windows.Forms;
52
26
 
53
-
54
-
55
27
  namespace Questions252432
56
-
57
28
  {
58
-
59
29
  public partial class Form1 : Form
60
-
61
30
  {
62
-
63
31
  private IntPtr hWnd;
64
32
 
65
-
66
-
67
33
  public Form1() => InitializeComponent();
68
34
 
69
-
70
-
71
35
  private void Panel1_MouseDown(object sender, MouseEventArgs e)
72
-
73
- {
36
+ {
74
-
75
37
  if(e.Button == MouseButtons.Left) panel1.Capture = true;
76
-
77
- }
38
+ }
78
-
79
-
80
39
 
81
40
  private void Panel1_MouseUp(object sender, MouseEventArgs e)
82
-
83
- {
41
+ {
84
-
85
42
  panel1.Capture = false;
86
-
87
43
  var p = NativeMethods.GetCursorPos();
88
-
89
44
  var h = NativeMethods.WindowFromPoint(p);
90
-
91
45
  hWnd = NativeMethods.GetAncestor(h);
92
46
 
93
-
94
-
95
47
  label_title.Text = $"Title: {NativeMethods.GetWindowText(hWnd)}";
96
-
97
48
  label_class.Text = $"Class: {NativeMethods.GetClassName(hWnd)}";
98
-
99
- }
49
+ }
100
-
101
-
102
50
 
103
51
  private void Button1_Click(object sender, EventArgs e)
104
-
105
- {
52
+ {
106
-
107
53
  Clipboard.SetText(textBox1.Text);
108
-
109
54
  NativeMethods.SetForegroundWindow(hWnd);
110
-
111
55
  SendKeys.Send("^v");
112
-
113
- }
56
+ }
114
-
115
-
116
57
 
117
58
  private static class NativeMethods
118
-
119
- {
59
+ {
120
-
121
- [DllImport("user32")]
60
+ [DllImport("user32")]
122
-
123
61
  private static extern bool GetCursorPos(out Point pPoint);
124
-
125
62
  public static Point GetCursorPos()
126
-
127
- {
63
+ {
128
-
129
64
  GetCursorPos(out var p);
130
-
131
65
  return p;
132
-
133
- }
66
+ }
134
-
135
-
136
-
67
+
137
- [DllImport("user32")]
68
+ [DllImport("user32")]
138
-
139
69
  public static extern IntPtr WindowFromPoint(Point point);
140
70
 
141
-
142
-
143
- [DllImport("user32")]
71
+ [DllImport("user32")]
144
-
145
72
  private static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
146
-
147
73
  public static IntPtr GetAncestor(IntPtr hWnd) => GetAncestor(hWnd, GA_ROOTOWNER);
148
-
149
74
  private const uint GA_ROOTOWNER = 3;
150
75
 
151
-
152
-
153
76
  [DllImport("user32", CharSet = CharSet.Unicode)]
154
-
155
77
  private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
156
-
157
78
  public static string GetWindowText(IntPtr hWnd)
158
-
159
- {
79
+ {
160
-
161
80
  var sb = new StringBuilder(100);
162
-
163
81
  GetWindowText(hWnd, sb, sb.Capacity);
164
-
165
82
  return sb.ToString();
166
-
167
- }
83
+ }
168
-
169
-
170
84
 
171
85
  [DllImport("user32", CharSet = CharSet.Unicode)]
172
-
173
86
  private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
174
-
175
87
  public static string GetClassName(IntPtr hWnd)
176
-
177
- {
88
+ {
178
-
179
89
  var sb = new StringBuilder(100);
180
-
181
90
  GetClassName(hWnd, sb, sb.Capacity);
182
-
183
91
  return sb.ToString();
184
-
185
- }
92
+ }
186
-
187
-
188
-
93
+
189
- [DllImport("user32")]
94
+ [DllImport("user32")]
190
-
191
95
  public static extern bool SetForegroundWindow(IntPtr hWnd);
192
-
193
- }
96
+ }
194
-
195
97
  }
196
-
197
98
  }
198
-
199
99
  ```
200
100
 
201
-
202
-
203
- Form1.Designer.cs
101
+ ```cs:Form1.Designer.cs
204
-
205
- ```C#
206
-
207
102
  namespace Questions252432
208
-
209
103
  {
210
-
211
104
  partial class Form1
212
-
213
105
  {
214
-
215
106
  /// <summary>
216
-
217
107
  /// 必要なデザイナー変数です。
218
-
219
108
  /// </summary>
220
-
221
109
  private System.ComponentModel.IContainer components = null;
222
110
 
223
-
224
-
225
111
  /// <summary>
226
-
227
112
  /// 使用中のリソースをすべてクリーンアップします。
228
-
229
113
  /// </summary>
230
-
231
114
  /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
232
-
233
115
  protected override void Dispose(bool disposing)
234
-
235
- {
116
+ {
236
-
237
117
  if(disposing && (components != null))
238
-
239
- {
118
+ {
240
-
241
119
  components.Dispose();
242
-
243
- }
120
+ }
244
-
245
121
  base.Dispose(disposing);
246
-
247
- }
122
+ }
248
-
249
-
250
123
 
251
124
  #region Windows フォーム デザイナーで生成されたコード
252
125
 
253
-
254
-
255
126
  /// <summary>
256
-
257
127
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
258
-
259
128
  /// コード エディターで変更しないでください。
260
-
261
129
  /// </summary>
262
-
263
130
  private void InitializeComponent()
264
-
265
- {
131
+ {
266
-
267
132
  this.button1 = new System.Windows.Forms.Button();
268
-
269
133
  this.label_title = new System.Windows.Forms.Label();
270
-
271
134
  this.label_class = new System.Windows.Forms.Label();
272
-
273
135
  this.textBox1 = new System.Windows.Forms.TextBox();
274
-
275
136
  this.panel1 = new System.Windows.Forms.Panel();
276
-
277
137
  this.SuspendLayout();
278
-
279
- //
138
+ //
280
-
281
139
  // button1
282
-
283
- //
140
+ //
284
-
285
141
  this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
286
-
287
142
  this.button1.Location = new System.Drawing.Point(297, 222);
288
-
289
143
  this.button1.Name = "button1";
290
-
291
144
  this.button1.Size = new System.Drawing.Size(75, 23);
292
-
293
145
  this.button1.TabIndex = 0;
294
-
295
146
  this.button1.Text = "貼り付け";
296
-
297
147
  this.button1.UseVisualStyleBackColor = true;
298
-
299
148
  this.button1.Click += new System.EventHandler(this.Button1_Click);
300
-
301
- //
149
+ //
302
-
303
150
  // label_title
304
-
305
- //
151
+ //
306
-
307
152
  this.label_title.AutoSize = true;
308
-
309
153
  this.label_title.Location = new System.Drawing.Point(54, 16);
310
-
311
154
  this.label_title.Name = "label_title";
312
-
313
155
  this.label_title.Size = new System.Drawing.Size(30, 12);
314
-
315
156
  this.label_title.TabIndex = 1;
316
-
317
157
  this.label_title.Text = "Title:";
318
-
319
- //
158
+ //
320
-
321
159
  // label_class
322
-
323
- //
160
+ //
324
-
325
161
  this.label_class.AutoSize = true;
326
-
327
162
  this.label_class.Location = new System.Drawing.Point(54, 31);
328
-
329
163
  this.label_class.Name = "label_class";
330
-
331
164
  this.label_class.Size = new System.Drawing.Size(36, 12);
332
-
333
165
  this.label_class.TabIndex = 2;
334
-
335
166
  this.label_class.Text = "Class:";
336
-
337
- //
167
+ //
338
-
339
168
  // textBox1
340
-
341
- //
169
+ //
342
-
343
170
  this.textBox1.AcceptsReturn = true;
344
-
345
171
  this.textBox1.AcceptsTab = true;
346
-
347
172
  this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
348
-
349
173
  | System.Windows.Forms.AnchorStyles.Left)
350
-
351
174
  | System.Windows.Forms.AnchorStyles.Right)));
352
-
353
175
  this.textBox1.Location = new System.Drawing.Point(12, 54);
354
-
355
176
  this.textBox1.Multiline = true;
356
-
357
177
  this.textBox1.Name = "textBox1";
358
-
359
178
  this.textBox1.Size = new System.Drawing.Size(360, 162);
360
-
361
179
  this.textBox1.TabIndex = 3;
362
-
363
- //
180
+ //
364
-
365
181
  // panel1
366
-
367
- //
182
+ //
368
-
369
183
  this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
370
-
371
184
  this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
372
-
373
185
  this.panel1.Cursor = System.Windows.Forms.Cursors.Cross;
374
-
375
186
  this.panel1.Location = new System.Drawing.Point(12, 12);
376
-
377
187
  this.panel1.Name = "panel1";
378
-
379
188
  this.panel1.Size = new System.Drawing.Size(36, 36);
380
-
381
189
  this.panel1.TabIndex = 4;
382
-
383
190
  this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Panel1_MouseDown);
384
-
385
191
  this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Panel1_MouseUp);
386
-
387
- //
192
+ //
388
-
389
193
  // Form1
390
-
391
- //
194
+ //
392
-
393
195
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
394
-
395
196
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
396
-
397
197
  this.ClientSize = new System.Drawing.Size(384, 257);
398
-
399
198
  this.Controls.Add(this.panel1);
400
-
401
199
  this.Controls.Add(this.textBox1);
402
-
403
200
  this.Controls.Add(this.label_class);
404
-
405
201
  this.Controls.Add(this.label_title);
406
-
407
202
  this.Controls.Add(this.button1);
408
-
409
203
  this.Name = "Form1";
410
-
411
204
  this.Text = "貼り付けくん";
412
-
413
205
  this.ResumeLayout(false);
414
-
415
206
  this.PerformLayout();
416
207
 
417
-
418
-
419
- }
208
+ }
420
-
421
-
422
209
 
423
210
  #endregion
424
211
 
425
-
426
-
427
212
  private System.Windows.Forms.Button button1;
428
-
429
213
  private System.Windows.Forms.Label label_title;
430
-
431
214
  private System.Windows.Forms.Label label_class;
432
-
433
215
  private System.Windows.Forms.TextBox textBox1;
434
-
435
216
  private System.Windows.Forms.Panel panel1;
436
-
437
217
  }
438
-
439
218
  }
440
-
441
219
  ```
442
-
443
220
  ![アプリ画像](ef407a57ef15943dd18b67c9f4791ffd.png)
444
221
 
445
-
446
-
447
222
  ---
448
223
 
449
-
450
-
451
- SendKeysは(SendInputも)文字列自体を送ることもできますが、明らかに遅いのと改行がアプリによってうまくいかない等なかなか難しいです。
224
+ `SendKeys`は(`SendInput`も)文字列自体を送ることもできますが、明らかに遅いのと改行がアプリによってうまくいかない等なかなか難しいです。
452
225
 
453
226
  クリップボードが汚れるのを許容できるなら、この方式のほうがいいと思います。