我有一个细节面板,可以显示或隐藏。
我该如何为显示/隐藏该面板(以及其内容)制作简单的淡入淡出效果?
我正在使用Windows表单,在Windows表单中控件没有opacity
属性。
在Winforms中,这是相当可行的,它只需要看起来像一个淡入淡出效果。一种技术是使用Control.DrawToBitmap()创建控件的位图。然后用计时器从背景位图混合到前景位图。
我将使用一个UserControl而不是Panel,这样您可以使用Winforms设计器设计控件。但是,这段代码将在任何类型的控件中起作用。将下面显示的代码粘贴到您的项目中并添加一个新类。编译。使用“继承的用户控件”模板从项目+添加新项、Windows窗体节点、“Inherited User Control”弹出列表中选择FadeControl,从这个控件创建自己的UserControl,并像平常一样设计用户控件。
如写的那样,控件将在您将其添加到父级时自动从父级的BackColor淡入淡出到控件内容。调用FadeOut()使其混合回背景。如果您想在淡出完成后自动处理控件,则传递true。您可以使用FadeIn()和Faded属性手动控制淡入淡出。您可以调整以“//tweakable”注释的数字以调整动画。如果父级具有非不透明背景,则需要进行其他工作。
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;
}
panel
没有任何opacity
属性。这是不可能的。你应该使用WPF
来实现! - ahmadali shafiee