简体   繁体   English

淡入面板-Windows窗体

[英]Fade a panel- Windows forms

I have a details panel which can be shown or hidden. 我有一个可以显示或隐藏的详细信息面板。

How can I made a simple fade effect for showing/hiding that panel (and of course its contents) ? 如何制作一个简单的淡入淡出效果以显示/隐藏该面板(当然还有其内容)?

I am using Windows forms, and controls don't have opacity property in windows forms. 我正在使用Windows窗体,并且控件在Windows窗体中没有opacity属性。

This is quite do-able in Winforms, it only has to look like a fade. 这在Winforms中是完全可行的,只需要看起来像是淡入淡出。 One technique is to use Control.DrawToBitmap() to create a bitmap of the control. 一种技术是使用Control.DrawToBitmap()创建控件的位图。 And then blend from a background bitmap to the foreground bitmap with a timer. 然后使用计时器将背景位图混合到前景位图。

I'll use a UserControl instead of a Panel so you can design the control with the Winforms designer. 我将使用UserControl而不是Panel,以便可以使用Winforms设计器来设计控件。 The code will however work in any kind of control. 但是,该代码可以在任何控件中工作。 Add a new class to your project and paste the code shown below. 将新类添加到您的项目中,然后粘贴以下代码。 Compile. 编译。 Create your own UserControl from this one with Project + Add New Item, Windows Forms node, "Inherited User Control" template and pick FadeControl from the popup list. 使用Project + Add New Item,Windows Forms节点,“ Inherited User Control”模板从此控件创建您自己的UserControl,然后从弹出列表中选择FadeControl。 Design the user control as normal. 正常设计用户控件。

As written, the control will automatically fade from the parent's BackColor to the control content as soon as you add the control to the parent. 按照书面说明,将控件添加到父级后,控件将自动从父级的BackColor淡入控件内容。 Call FadeOut() to make it blend back to the background. 调用FadeOut()以使其融合回背景。 Pass true if you want to automatically dispose the control when it is done fading. 如果要在完成淡入淡出时自动处理该控件,请传递true。 You can use FadeIn() and the Faded property for manual control of the fading. 您可以使用FadeIn()和Faded属性来手动控制衰落。 You can adjust the numbers in the lines commented with // tweakable to adjust the animation. 您可以调整//进行注释的行中的数字,以调整动画。 Additional work is needed if the parent has a non-opaque background. 如果父母的背景不透明,则需要进行其他工作。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

class FadeControl : UserControl {

    public FadeControl() {
        pbox = new PictureBox();
        pbox.BorderStyle = BorderStyle.None;
        pbox.Paint += new PaintEventHandler(pbox_Paint);
        fadeTimer = new Timer();
        fadeTimer.Interval = 15;   // tweakable
        fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
    }

    public bool Faded {
        get { return blend < 0.5f; }
    }
    public void FadeIn() {
        stopFade(false);
        createBitmaps();
        startFade(1);
    }
    public void FadeOut(bool disposeWhenDone) {
        stopFade(false);
        createBitmaps();
        disposeOnComplete = disposeWhenDone;
        startFade(-1);
    }

    private void createBitmaps() {
        bmpBack = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
        using (var gr = Graphics.FromImage(bmpBack)) gr.Clear(this.Parent.BackColor);
        bmpFore = new Bitmap(bmpBack.Width, bmpBack.Height);
        this.DrawToBitmap(bmpFore, this.ClientRectangle);
    }
    void fadeTimer_Tick(object sender, EventArgs e) {
        blend += blendDir * 0.02F;   // tweakable
        bool done = false;
        if (blend < 0) { done = true; blend = 0; }
        if (blend > 1) { done = true; blend = 1; }
        if (done) stopFade(true); 
        else pbox.Invalidate();
    }
    void pbox_Paint(object sender, PaintEventArgs e) {
        Rectangle rc = new Rectangle(0, 0, pbox.Width, pbox.Height);
        ColorMatrix cm = new ColorMatrix();
        ImageAttributes ia = new ImageAttributes();
        cm.Matrix33 = blend;
        ia.SetColorMatrix(cm);
        e.Graphics.DrawImage(bmpFore, rc, 0, 0, bmpFore.Width, bmpFore.Height, GraphicsUnit.Pixel, ia);
        cm.Matrix33 = 1F - blend;
        ia.SetColorMatrix(cm);
        e.Graphics.DrawImage(bmpBack, rc, 0, 0, bmpBack.Width, bmpBack.Height, GraphicsUnit.Pixel, ia);
    }

    private void stopFade(bool complete) {
        fadeTimer.Enabled = false;
        if (complete) {
           if (!Faded) this.Controls.Remove(pbox);
           else if (disposeOnComplete) this.Dispose();
        }
        if (bmpBack != null) { bmpBack.Dispose(); bmpBack = null; }
        if (bmpFore != null) { bmpFore.Dispose(); bmpFore = null; }
    }
    private void startFade(int dir) {
        this.Controls.Add(pbox);
        this.Controls.SetChildIndex(pbox, 0);
        blendDir = dir;
        fadeTimer.Enabled = true;
        fadeTimer_Tick(this, EventArgs.Empty);
    }

    protected override void OnCreateControl() {
        base.OnCreateControl();
        if (!DesignMode) FadeIn();
    }
    protected override void OnResize(EventArgs eventargs) {
        pbox.Size = this.ClientSize;
        base.OnResize(eventargs);
    }
    protected override void Dispose(bool disposing) {
        if (disposing) {
            stopFade(false);
            pbox.Dispose();
            fadeTimer.Dispose();
        }
        base.Dispose(disposing);
    }

    private PictureBox pbox;
    private Timer fadeTimer;
    private Bitmap bmpBack, bmpFore;
    private float blend;
    private int blendDir = 1;
    private bool disposeOnComplete;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM