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

回答編集履歴

1

見直しキャンペーン中

2023/07/21 08:44

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -1,83 +1,83 @@
1
- 画像のクリックスルーは、それなりに面倒そうですね。
2
- [WPF: Detect Image click only on non-transparent portion - Stack Overflow](https://stackoverflow.com/questions/4800597/wpf-detect-image-click-only-on-non-transparent-portion)
3
-
4
- リンクの`OpaqueClickableImage`を使ったもの
5
- ```xaml
6
- <Window
7
- x:Class="Questions246137.MainWindow"
8
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
10
- xmlns:local="clr-namespace:Questions246137"
11
- Width="800"
12
- Height="450">
13
- <Grid>
14
- <Button Click="Button_Click">
15
- <Button.Template>
16
- <ControlTemplate TargetType="{x:Type Button}">
17
- <ContentPresenter Content="{TemplateBinding Content}" />
18
- </ControlTemplate>
19
- </Button.Template>
20
- <local:OpaqueClickableImage Source="star.png" Stretch="None" />
21
- </Button>
22
- </Grid>
23
- </Window>
24
- ```
25
- マウスオーバーやクリックの変化はありません。
26
-
27
- ---
28
-
29
- 簡単な図形なら`Microsoft Blend for Visual Studio`で、作ってしまうのはどうでしょう?
30
-
31
- 雑にBlendで作ったもの(DLLが入ってしまうのが難点)
32
- ```xaml
33
- <Window
34
- x:Class="Questions246137.MainWindow"
35
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
37
- xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
38
- Width="800"
39
- Height="450">
40
- <Window.Resources>
41
- <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
42
- <Setter Property="Template">
43
- <Setter.Value>
44
- <ControlTemplate TargetType="{x:Type Button}">
45
- <Grid>
46
- <ed:RegularPolygon
47
- Width="150"
48
- Height="150"
49
- Fill="Yellow"
50
- InnerRadius="0.66"
51
- PointCount="6"
52
- Stretch="Fill"
53
- Stroke="Black"
54
- StrokeThickness="8" />
55
- <ed:RegularPolygon
56
- x:Name="shadow"
57
- Width="150"
58
- Height="150"
59
- Fill="Black"
60
- InnerRadius="0.66"
61
- Opacity="0"
62
- PointCount="6" />
63
- </Grid>
64
- <ControlTemplate.Triggers>
65
- <Trigger Property="IsMouseOver" Value="true">
66
- <Setter TargetName="shadow" Property="Opacity" Value="0.1" />
67
- </Trigger>
68
- <Trigger Property="IsPressed" Value="true">
69
- <Setter TargetName="shadow" Property="Opacity" Value="0.2" />
70
- </Trigger>
71
- </ControlTemplate.Triggers>
72
- </ControlTemplate>
73
- </Setter.Value>
74
- </Setter>
75
- </Style>
76
- </Window.Resources>
77
- <Grid>
78
- <Button Style="{DynamicResource ButtonStyle1}" Click="Button_Click" />
79
- </Grid>
80
- </Window>
81
- ```
82
-
1
+ 画像のクリックスルーは、それなりに面倒そうですね。
2
+ [WPF: Detect Image click only on non-transparent portion - Stack Overflow](https://stackoverflow.com/questions/4800597/wpf-detect-image-click-only-on-non-transparent-portion)
3
+
4
+ リンクの`OpaqueClickableImage`を使ったもの
5
+ ```xml
6
+ <Window
7
+ x:Class="Questions246137.MainWindow"
8
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
10
+ xmlns:local="clr-namespace:Questions246137"
11
+ Width="800"
12
+ Height="450">
13
+ <Grid>
14
+ <Button Click="Button_Click">
15
+ <Button.Template>
16
+ <ControlTemplate TargetType="{x:Type Button}">
17
+ <ContentPresenter Content="{TemplateBinding Content}" />
18
+ </ControlTemplate>
19
+ </Button.Template>
20
+ <local:OpaqueClickableImage Source="star.png" Stretch="None" />
21
+ </Button>
22
+ </Grid>
23
+ </Window>
24
+ ```
25
+ マウスオーバーやクリックの変化はありません。
26
+
27
+ ---
28
+
29
+ 簡単な図形なら`Microsoft Blend for Visual Studio`で、作ってしまうのはどうでしょう?
30
+
31
+ 雑にBlendで作ったもの(DLLが入ってしまうのが難点)
32
+ ```xml
33
+ <Window
34
+ x:Class="Questions246137.MainWindow"
35
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
37
+ xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
38
+ Width="800"
39
+ Height="450">
40
+ <Window.Resources>
41
+ <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
42
+ <Setter Property="Template">
43
+ <Setter.Value>
44
+ <ControlTemplate TargetType="{x:Type Button}">
45
+ <Grid>
46
+ <ed:RegularPolygon
47
+ Width="150"
48
+ Height="150"
49
+ Fill="Yellow"
50
+ InnerRadius="0.66"
51
+ PointCount="6"
52
+ Stretch="Fill"
53
+ Stroke="Black"
54
+ StrokeThickness="8" />
55
+ <ed:RegularPolygon
56
+ x:Name="shadow"
57
+ Width="150"
58
+ Height="150"
59
+ Fill="Black"
60
+ InnerRadius="0.66"
61
+ Opacity="0"
62
+ PointCount="6" />
63
+ </Grid>
64
+ <ControlTemplate.Triggers>
65
+ <Trigger Property="IsMouseOver" Value="true">
66
+ <Setter TargetName="shadow" Property="Opacity" Value="0.1" />
67
+ </Trigger>
68
+ <Trigger Property="IsPressed" Value="true">
69
+ <Setter TargetName="shadow" Property="Opacity" Value="0.2" />
70
+ </Trigger>
71
+ </ControlTemplate.Triggers>
72
+ </ControlTemplate>
73
+ </Setter.Value>
74
+ </Setter>
75
+ </Style>
76
+ </Window.Resources>
77
+ <Grid>
78
+ <Button Style="{DynamicResource ButtonStyle1}" Click="Button_Click" />
79
+ </Grid>
80
+ </Window>
81
+ ```
82
+
83
83
  凝ったものは`Microsoft Expression Design 4`がまだダウンロードできますので、画像から`Path`に変換するのもいいかもしれません。