简体   繁体   English

当我实际上未在代码中进行更改时,为什么椭圆的不透明度会自动更改?

[英]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: 问题可能是您正在更改MouseEnterMouseExit事件中的透明度:

<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 . 这两行都指定alpha100 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-bit32-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行都更改为100255 ,就可以了。

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.

相关问题 单击椭圆时更改不透明度? - WPF - Change opacity when ellipse is clicked? - WPF 在 C# 中使用轨迹栏更改表单中的不透明度不起作用,为什么? - Changing opacity in form with trackbar in C# don't works, why? 出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么 - For some reason my code is spawning in my zombies when it is not supposed to and I don't know why 输入名称时,我的代码将返回StackOverFlowException ...我不明白为什么 - My code returns a StackOverFlowException when I enter a name… I don't understand why 当我用其他方法更改控件时,为什么不更新控件? - Why don't my controls update when I change them in another method? 为什么当我使用RegisterStartupScript或RegisterClientScriptBlock时,我的C#代码不起作用? - Why when I use RegisterStartupScript or RegisterClientScriptBlock my C# code don't works? 当我未在代码中调用它时,为什么我的方法被Unity调用? - Why is my method called by Unity when I don't call it in code? 为什么我的XDocument在我不想要时保存声明? - Why is my XDocument saving the declaration when I don't want it to? 在我的C#嗅探器中设置套接字选项时,不知道为什么我收到错误代码10022(无效参数) - Don't know why I'm getting Error Code 10022 (Invalid Argument) when setting socket options in my C# sniffer 我不明白为什么此代码有效? - I don't understand why this code works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM