[英]Why is the opacity of my ellipse changing automatically when I don't actually change it in the code?
I have an Ellipse in my Windows Universal App and I want to change its fill color when the mouse enters and leaves that ellipse. 我的Windows Universal应用程序中有一个椭圆,当鼠标进入并离开该椭圆时,我想更改其填充颜色。 All I want to do is change the color and nothing else.
我只想改变颜色,别无其他。 However the opacity automatically decreases whenever I enter my mouse.
但是,每当我进入鼠标时,不透明度会自动降低。 Can you please find out the problem in my code.
您能在我的代码中找出问题吗?
Thank you. 谢谢。
XAML XAML
<Ellipse x:Name="glamourEllipse" HorizontalAlignment="Left" Height="226" Margin="594,260,0,0" Stroke="#FFB43F3F" VerticalAlignment="Top" Width="226" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" Tapped="glamour_Tapped">
<Ellipse.Fill>
<SolidColorBrush Color="#FFB43C3C"/>
</Ellipse.Fill>
<Ellipse.RenderTransform>
<CompositeTransform Rotation="-90.254"/>
</Ellipse.RenderTransform>
</Ellipse>
C# C#
SolidColorBrush hoverBrush = new SolidColorBrush();
private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(100, 140, 40, 40);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(100, 180, 60, 60);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
The problem is likely that you are changing the transparency in the MouseEnter
and MouseExit
events: 问题可能是您正在更改
MouseEnter
和MouseExit
事件中的透明度:
<SolidColorBrush Color="#FFB43C3C"/>
This line sets the colour to 255 alpha, 180 red, 60 green and 60 blue
. 该行将颜色设置为
255 alpha, 180 red, 60 green and 60 blue
。 Your code on the other hand: 另一方面,您的代码:
hoverBrush.Color = Color.FromArgb(100, 140, 40, 40);
hoverBrush.Color = Color.FromArgb(100, 180, 60, 60);
Both lines specify an alpha
of 100
. 这两行都指定
alpha
为100
。 Now, in our decimal
system of mathematics this would be perfectly valid and mean 100%
opacity, but in the 8-bit
and 32-bit
world of the computer, it's not. 现在,在我们的
decimal
数学系统中,这将是完全有效的,并且意味着100%
不透明度,但是在8-bit
和32-bit
计算机环境中,则不是。 Basically, that code sets the alpha to 64
hex. 基本上,该代码将alpha设置为
64
十六进制。 (Change the FF
in the XAML and you will see the transparency is the same.) (在XAML中更改
FF
,您将看到透明性是相同的。)
That overload of Color.FromArgb
expects that 0
is transparent, and 255
is opaque. Color.FromArgb
重载期望0
是透明的,而255
是不透明的。
https://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-CN/library/system.drawing.color.fromargb%28v=vs.110%29.aspx
Basically, change 100
to 255
in both of those Color.FromArgb
lines and you're golden. 基本上,将这两行
Color.FromArgb
行都更改为100
到255
,就可以了。
private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(255, 140, 40, 40);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(255, 180, 60, 60);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
Or, remove that value altogether. 或者,完全删除该值。
private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(140, 40, 40);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(180, 60, 60);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
Both options have the same effect overall. 这两个选项总体上具有相同的效果。 The second one also guarantees you don't accidentally do it again, as the
alpha
will always be 255
with it. 第二个也保证您不会再次出错,因为
alpha
始终为255
。
Edit: Lastly, for completeness, you could use Hexadecimal in the code as well, which may be the better option as it's all the same that way: 编辑:最后,为了完整起见,您也可以在代码中使用十六进制,这可能是更好的选择,因为所有方式都相同:
private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(0xFF, 0x8C, 0x28, 0x28);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(0xFF, 0xB4, 0x3C, 0x3C);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
Or: 要么:
private void mouse_Enter(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(0x8C, 0x28, 0x28);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
private void mouse_Exit(object sender, PointerRoutedEventArgs e)
{
hoverBrush.Color = Color.FromArgb(0xB4, 0x3C, 0x3C);
hoverBrush.Opacity = 1;
nsfwEllipse.Fill = hoverBrush;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.