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

回答編集履歴

2

見直しキャンペーン中

2023/07/29 08:36

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -1,151 +1,151 @@
1
- 解決されたようですが、第三者目線ではさっぱり意味が分かんないですね(私の頭が悪いだけかもしれませんが^^;
2
-
3
- ---
4
-
5
- そもそも`HitTest`はどういう意味で使っていますか?
6
- [UIElement.InputHitTest(Point) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.uielement.inputhittest?view=net-5.0)
7
- や、
8
- [VisualTreeHelper.HitTest メソッド (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.visualtreehelper.hittest?view=net-5.0)
9
- のことですか?
10
- それともマウスクリック等のことですか?
11
-
12
- > Canvas上のGridは見かけ上の位置しか変更できず
13
- > もともとの位置((x,y)=(0,0))でしか、HitTestを行えません。
14
-
15
- そんなことないと思いますけど、どう確認しましたか?
16
-
17
- > rowとcolumnの情報を取得するにはどうしたらよいでしょうか!?
18
-
19
- 「HitTestでGridを選択したい」(取得したい?)と矛盾するというか、`Grid`の**中に**入っている何かの`HitTest`から、`Row`・`Column`を取得ならわかります。
20
- しかし`Grid`自体を取ったところで、その位置の`Row`・`Column`を手軽に出す方法はないと思うのですが(計算することはできるでしょうけど)
21
-
22
- > //無効(LayoutTransformのMSDより)
23
-
24
- これのことですね。
25
- > However, LayoutTransform ignores TranslateTransform operations.
26
-
27
- [FrameworkElement.LayoutTransform Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.layouttransform#remarks)
28
-
29
- ---
30
-
31
- なにか質問に書かれていない、前提条件があるのでしょうか?
32
- (コンパイル可能な)簡単なサンプルを提示いただくと、他人にとっても役に立つ質問になるかもしれません。
33
-
34
- ```xaml
35
- <Window
36
- x:Class="Questions369551.MainWindow"
37
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
39
- Width="500"
40
- Height="600">
41
- <Grid>
42
- <Grid.RowDefinitions>
43
- <RowDefinition Height="2*" />
44
- <RowDefinition Height="3*" />
45
- </Grid.RowDefinitions>
46
- <GroupBox Header="Canvas in Grid 移動テスト">
47
- <Canvas
48
- x:Name="canvas1"
49
- Background="White"
50
- MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
51
- <Grid
52
- x:Name="grid1"
53
- Width="100"
54
- Height="100"
55
- Background="Blue" />
56
- <Grid
57
- x:Name="grid2"
58
- Width="100"
59
- Height="100"
60
- Background="Blue">
61
- <Grid.RenderTransform>
62
- <TranslateTransform X="100" Y="100" />
63
- </Grid.RenderTransform>
64
- </Grid>
65
- <Grid
66
- x:Name="grid3"
67
- Width="100"
68
- Height="100"
69
- Margin="200,0,0,0"
70
- Background="Blue" />
71
- <Grid
72
- x:Name="grid4"
73
- Canvas.Left="300"
74
- Canvas.Top="100"
75
- Width="100"
76
- Height="100"
77
- Background="Blue" />
78
- </Canvas>
79
- </GroupBox>
80
-
81
- <GroupBox Grid.Row="1" Header="Shape HitTest テスト">
82
- <Canvas x:Name="canvas2" MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
83
- <Grid
84
- Canvas.Left="30"
85
- Canvas.Top="30"
86
- Width="200"
87
- Height="200"
88
- Margin="20,20,0,0"
89
- ShowGridLines="True">
90
- <Grid.RenderTransform>
91
- <TranslateTransform X="10" Y="10" />
92
- </Grid.RenderTransform>
93
- <Grid.RowDefinitions>
94
- <RowDefinition />
95
- <RowDefinition />
96
- </Grid.RowDefinitions>
97
- <Grid.ColumnDefinitions>
98
- <ColumnDefinition />
99
- <ColumnDefinition />
100
- </Grid.ColumnDefinitions>
101
- <Rectangle
102
- x:Name="rectangle"
103
- Grid.Column="1"
104
- Fill="Green" />
105
- <Ellipse
106
- x:Name="ellipse"
107
- Grid.Row="1"
108
- Fill="HotPink" />
109
- <Polygon
110
- x:Name="polygon"
111
- Grid.Row="1"
112
- Grid.Column="1"
113
- Fill="Orange"
114
- Points="0,100 100,100 50,0" />
115
- </Grid>
116
- </Canvas>
117
- </GroupBox>
118
- </Grid>
119
- </Window>
120
- ```
121
-
122
- ```C#
123
- using System;
124
- using System.Diagnostics;
125
- using System.Windows;
126
- using System.Windows.Controls;
127
- using System.Windows.Input;
128
- using System.Windows.Media;
129
-
130
- namespace Questions369551
131
- {
132
- public partial class MainWindow : Window
133
- {
134
- public MainWindow() => InitializeComponent();
135
-
136
- private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
137
- {
138
- if (sender is Canvas canvas)
139
- {
140
- var p = e.GetPosition(canvas);
141
- if (VisualTreeHelper.HitTest(canvas, p)?.VisualHit is FrameworkElement element)
142
- {
143
- var r = Grid.GetRow(element);
144
- var c = Grid.GetColumn(element);
145
- Debug.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {element.Name} Row:{r} Column:{c}");
146
- }
147
- }
148
- }
149
- }
150
- }
1
+ 解決されたようですが、第三者目線ではさっぱり意味が分かんないですね(私の頭が悪いだけかもしれませんが^^;
2
+
3
+ ---
4
+
5
+ そもそも`HitTest`はどういう意味で使っていますか?
6
+ [UIElement.InputHitTest(Point) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.uielement.inputhittest?view=net-5.0)
7
+ や、
8
+ [VisualTreeHelper.HitTest メソッド (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.visualtreehelper.hittest?view=net-5.0)
9
+ のことですか?
10
+ それともマウスクリック等のことですか?
11
+
12
+ > Canvas上のGridは見かけ上の位置しか変更できず
13
+ > もともとの位置((x,y)=(0,0))でしか、HitTestを行えません。
14
+
15
+ そんなことないと思いますけど、どう確認しましたか?
16
+
17
+ > rowとcolumnの情報を取得するにはどうしたらよいでしょうか!?
18
+
19
+ `HitTest``Grid`を選択したい」(取得したい?)と矛盾するというか、`Grid`の**中に**入っている何かの`HitTest`から、`Row`・`Column`を取得ならわかります。
20
+ しかし`Grid`自体を取ったところで、その位置の`Row`・`Column`を手軽に出す方法はないと思うのですが(計算することはできるでしょうけど)
21
+
22
+ > //無効(LayoutTransformのMSDより)
23
+
24
+ これのことですね。
25
+ > However, LayoutTransform ignores TranslateTransform operations.
26
+
27
+ [FrameworkElement.LayoutTransform Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.layouttransform#remarks)
28
+
29
+ ---
30
+
31
+ なにか質問に書かれていない、前提条件があるのでしょうか?
32
+ (コンパイル可能な)簡単なサンプルを提示いただくと、他人にとっても役に立つ質問になるかもしれません。
33
+
34
+ ```xml
35
+ <Window
36
+ x:Class="Questions369551.MainWindow"
37
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
39
+ Width="500"
40
+ Height="600">
41
+ <Grid>
42
+ <Grid.RowDefinitions>
43
+ <RowDefinition Height="2*" />
44
+ <RowDefinition Height="3*" />
45
+ </Grid.RowDefinitions>
46
+ <GroupBox Header="Canvas in Grid 移動テスト">
47
+ <Canvas
48
+ x:Name="canvas1"
49
+ Background="White"
50
+ MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
51
+ <Grid
52
+ x:Name="grid1"
53
+ Width="100"
54
+ Height="100"
55
+ Background="Blue" />
56
+ <Grid
57
+ x:Name="grid2"
58
+ Width="100"
59
+ Height="100"
60
+ Background="Blue">
61
+ <Grid.RenderTransform>
62
+ <TranslateTransform X="100" Y="100" />
63
+ </Grid.RenderTransform>
64
+ </Grid>
65
+ <Grid
66
+ x:Name="grid3"
67
+ Width="100"
68
+ Height="100"
69
+ Margin="200,0,0,0"
70
+ Background="Blue" />
71
+ <Grid
72
+ x:Name="grid4"
73
+ Canvas.Left="300"
74
+ Canvas.Top="100"
75
+ Width="100"
76
+ Height="100"
77
+ Background="Blue" />
78
+ </Canvas>
79
+ </GroupBox>
80
+
81
+ <GroupBox Grid.Row="1" Header="Shape HitTest テスト">
82
+ <Canvas x:Name="canvas2" MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
83
+ <Grid
84
+ Canvas.Left="30"
85
+ Canvas.Top="30"
86
+ Width="200"
87
+ Height="200"
88
+ Margin="20,20,0,0"
89
+ ShowGridLines="True">
90
+ <Grid.RenderTransform>
91
+ <TranslateTransform X="10" Y="10" />
92
+ </Grid.RenderTransform>
93
+ <Grid.RowDefinitions>
94
+ <RowDefinition />
95
+ <RowDefinition />
96
+ </Grid.RowDefinitions>
97
+ <Grid.ColumnDefinitions>
98
+ <ColumnDefinition />
99
+ <ColumnDefinition />
100
+ </Grid.ColumnDefinitions>
101
+ <Rectangle
102
+ x:Name="rectangle"
103
+ Grid.Column="1"
104
+ Fill="Green" />
105
+ <Ellipse
106
+ x:Name="ellipse"
107
+ Grid.Row="1"
108
+ Fill="HotPink" />
109
+ <Polygon
110
+ x:Name="polygon"
111
+ Grid.Row="1"
112
+ Grid.Column="1"
113
+ Fill="Orange"
114
+ Points="0,100 100,100 50,0" />
115
+ </Grid>
116
+ </Canvas>
117
+ </GroupBox>
118
+ </Grid>
119
+ </Window>
120
+ ```
121
+
122
+ ```cs
123
+ using System;
124
+ using System.Diagnostics;
125
+ using System.Windows;
126
+ using System.Windows.Controls;
127
+ using System.Windows.Input;
128
+ using System.Windows.Media;
129
+
130
+ namespace Questions369551
131
+ {
132
+ public partial class MainWindow : Window
133
+ {
134
+ public MainWindow() => InitializeComponent();
135
+
136
+ private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
137
+ {
138
+ if (sender is Canvas canvas)
139
+ {
140
+ var p = e.GetPosition(canvas);
141
+ if (VisualTreeHelper.HitTest(canvas, p)?.VisualHit is FrameworkElement element)
142
+ {
143
+ var r = Grid.GetRow(element);
144
+ var c = Grid.GetColumn(element);
145
+ Debug.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {element.Name} Row:{r} Column:{c}");
146
+ }
147
+ }
148
+ }
149
+ }
150
+ }
151
151
  ```

1

誤字

2021/11/16 10:31

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  や、
8
8
  [VisualTreeHelper.HitTest メソッド (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.visualtreehelper.hittest?view=net-5.0)
9
9
  のことですか?
10
- それとマウスクリック等のことですか?
10
+ それとマウスクリック等のことですか?
11
11
 
12
12
  > Canvas上のGridは見かけ上の位置しか変更できず
13
13
  > もともとの位置((x,y)=(0,0))でしか、HitTestを行えません。