c# - 如何在 C# 中淡入和淡出面板?

标签 c# winforms

<分区>

无论如何,我可以淡化面板吗?

我想在单击按钮时淡化菜单中的整个面板。

[1]: /image/OnpQo.png

有什么帮助吗?

最佳答案

这是一个示例,它截取了 Panel 的内容,然后将其淡化到 Panel 或其 Parent > 的 BackColor..:

enter image description here

class FadePanel : Panel
{
    DrawPanel Layer = new DrawPanel();
    Bitmap image = null;
    float alpha = 0;
    float delta = 1;
    Timer timer = new Timer() { Interval = 25 };

    public bool FadeToParent { get; set; }
    public Color FadeColor { get; set; }
    public bool StaticContent { get; set; }
    public bool Hidden { get; private set; }

    public FadePanel()
    {  
        DoubleBuffered = true;
        Layer.Size = Size.Empty;
        Layer.Parent = this;
        Layer.BackColor = Color.Transparent;
        Layer.Paint += Layer_Paint;
        BackColor = Color.DodgerBlue;
        FadeToParent = false;
        Color FadeColor  = BackColor;
        Hidden = false;
        StaticContent = true;
        Layer.BackgroundImageLayout = ImageLayout.None;
        timer.Tick += timer_Tick;
    }


    void Layer_Paint(object sender, PaintEventArgs e)
    {
        if (alpha >= 0 && alpha <= 255)
        {
            using (SolidBrush brush = 
               new SolidBrush(Color.FromArgb((int)alpha, FadeColor)))
                e.Graphics.FillRectangle(brush, Layer.ClientRectangle);
        }
    }

    public void CaptureLayer()
    {
        if (image == null) image = new Bitmap(ClientSize.Width, ClientSize.Height);
        DrawToBitmap(image, ClientRectangle);
        Layer.BackgroundImage = image;
    }

    public void FadeOut(int ms)
    {
        alpha = 0;
        delta = 256f / ms * timer.Interval;
        Fade(delta);
    }

    public void FadeIn(int ms)
    {
        alpha = 255;
        delta = -256f / ms * timer.Interval;
        Fade(delta);
    }

    void Fade(float delta)
    {
        if (image == null || !StaticContent) CaptureLayer();
        BringToFront();
        FadeColor = FadeToParent ? Parent.BackColor : FadeColor;
        Layer.BringToFront();
        Layer.Size = ClientSize;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        alpha += delta;
        if (alpha >= 255 || alpha <= 0)
        {
            alpha = delta > 0 ? 255 : 0;
            timer.Stop();
            if (delta < 0) BringToFront(); else SendToBack();
            Hidden = delta > 0;
            Layer.Size = delta > 0 ? ClientSize : Size.Empty;
        }
        Layer.Invalidate();
    }

}

DoubleBuffered 帮助程序 Panel 子类:

class DrawPanel : Panel
{
    public DrawPanel()
    {
        DoubleBuffered = true;
    }
}

更新:代替 Panel,它是一个 Container 控件,并不是真的要在上面绘制,我们应该使用 PictureboxLabel(使用 Autosize=false);两者都有开箱即用的 DoubleBuffered 属性,并且比 Panels 更好地支持绘图。

关于c# - 如何在 C# 中淡入和淡出面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956407/

相关文章:

c# - PointCollection (C#) 中 X 和 Y 坐标的最小值

c# - 设置 ASP :ContentPlaceHolder Content programmatically

c# - C# 如何生成 GUID?

c# - 获取文本框行号

c# - 以编程方式从一个选项卡切换到另一个选项卡

c# - 有没有一种简单的方法来合并登录屏幕?

c# - 从另一个窗体调用方法c#

c# - 如何从 .NET WPF 运行 C 程序的 .exe?

winforms - 是否可以避免 Winform 上的多次按钮点击?

c# - ITextSharp 压模损坏 pdf