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

回答編集履歴

1

見直しキャンペーン中

2023/07/25 13:28

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,66 +1,66 @@
1
- > 右クリックを押している状態で左クリックを押す
2
-
3
- ここの解釈ですが順番「右→左」・「左→右」にこだわらないのであれば、マウスフック等を使わずに簡単に実現可能です。
4
- つまり「ある時点で両方押されていればカーソル移動する」で十分な場合です。
5
- それではダメだということであれば、以降見る必要はありません。
6
-
7
- ---
8
-
9
- クリックは自ウィンドウ外では取得できませんが、ボタンが押されているかはカーソル位置によらず取得できます。
10
- ONボタンでタイマー開始し、タイマー中でボタン状態を取得、両方押されていればカーソル移動の流れです。
11
-
12
- * `TrackBar` 2つ
13
- * `Button` 2つ
14
- * `Timer` 1つ
15
- * `Label` 必要なだけ
16
-
17
- をフォームに置きます。
18
- `TrackBar`の`Maximum`・`Minimum`・`TickFrequency`・`TickStyle`各プロパティを好みに調整します。
19
- `Button.Click`・`Timer.Tick`イベントを作成します。
20
-
21
- とりあえずこのようにしました。
22
- ![アプリ画像](a32899804818d6bbf83e7ecac7e20084.png)
23
-
24
- C#コードは雑ですがこのような感じです。
25
- ```C#
26
- using System;
27
- using System.Drawing;
28
- using System.Windows.Forms;
29
-
30
- namespace Questions306937
31
- {
32
- public partial class Form1 : Form
33
- {
34
- public Form1() => InitializeComponent();
35
-
36
- private void timer1_Tick(object sender, EventArgs e)
37
- {
38
- // 今マウスボタンが左右とも押されていれば...
39
- if (Control.MouseButtons == (MouseButtons.Left | MouseButtons.Right))
40
- {
41
- // タイマーは0.1秒間隔 1秒で指定PX移動のつもり(誤差が出る&10以下だと動かない)
42
- var x = trackBar1.Value / 10;
43
- var y = trackBar2.Value / 10;
44
-
45
- // カーソル位置を更新
46
- Cursor.Position = new Point(Cursor.Position.X + x, Cursor.Position.Y + y);
47
- }
48
- }
49
-
50
- // ONボタンでタイマー開始
51
- private void button1_Click(object sender, EventArgs e) => timer1.Start();
52
-
53
- // OFFボタンでタイマー停止
54
- private void button2_Click(object sender, EventArgs e) => timer1.Stop();
55
- }
56
- }
57
- ```
58
-
59
- 「誤差が出る&10以下だと動かない」問題は、気になるようなら別の計算方法を考えてください。
60
-
61
- 参考
62
- [TrackBar クラス (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.trackbar)
63
-
64
- [Control.MouseButtons プロパティ (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.control.mousebuttons)
65
-
1
+ > 右クリックを押している状態で左クリックを押す
2
+
3
+ ここの解釈ですが順番「右→左」・「左→右」にこだわらないのであれば、マウスフック等を使わずに簡単に実現可能です。
4
+ つまり「ある時点で両方押されていればカーソル移動する」で十分な場合です。
5
+ それではダメだということであれば、以降見る必要はありません。
6
+
7
+ ---
8
+
9
+ クリックは自ウィンドウ外では取得できませんが、ボタンが押されているかはカーソル位置によらず取得できます。
10
+ ONボタンでタイマー開始し、タイマー中でボタン状態を取得、両方押されていればカーソル移動の流れです。
11
+
12
+ * `TrackBar` 2つ
13
+ * `Button` 2つ
14
+ * `Timer` 1つ
15
+ * `Label` 必要なだけ
16
+
17
+ をフォームに置きます。
18
+ `TrackBar`の`Maximum`・`Minimum`・`TickFrequency`・`TickStyle`各プロパティを好みに調整します。
19
+ `Button.Click`・`Timer.Tick`イベントを作成します。
20
+
21
+ とりあえずこのようにしました。
22
+ ![アプリ画像](a32899804818d6bbf83e7ecac7e20084.png)
23
+
24
+ C#コードは雑ですがこのような感じです。
25
+ ```cs
26
+ using System;
27
+ using System.Drawing;
28
+ using System.Windows.Forms;
29
+
30
+ namespace Questions306937
31
+ {
32
+ public partial class Form1 : Form
33
+ {
34
+ public Form1() => InitializeComponent();
35
+
36
+ private void timer1_Tick(object sender, EventArgs e)
37
+ {
38
+ // 今マウスボタンが左右とも押されていれば...
39
+ if (Control.MouseButtons == (MouseButtons.Left | MouseButtons.Right))
40
+ {
41
+ // タイマーは0.1秒間隔 1秒で指定PX移動のつもり(誤差が出る&10以下だと動かない)
42
+ var x = trackBar1.Value / 10;
43
+ var y = trackBar2.Value / 10;
44
+
45
+ // カーソル位置を更新
46
+ Cursor.Position = new Point(Cursor.Position.X + x, Cursor.Position.Y + y);
47
+ }
48
+ }
49
+
50
+ // ONボタンでタイマー開始
51
+ private void button1_Click(object sender, EventArgs e) => timer1.Start();
52
+
53
+ // OFFボタンでタイマー停止
54
+ private void button2_Click(object sender, EventArgs e) => timer1.Stop();
55
+ }
56
+ }
57
+ ```
58
+
59
+ 「誤差が出る&10以下だと動かない」問題は、気になるようなら別の計算方法を考えてください。
60
+
61
+ 参考
62
+ [TrackBar クラス (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.trackbar)
63
+
64
+ [Control.MouseButtons プロパティ (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.control.mousebuttons)
65
+
66
66
  [Cursor.Position プロパティ (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.cursor.position)