gpt4 book ai didi

c# - 淡化面板 - Windows 窗体

转载 作者:太空狗 更新时间:2023-10-29 18:11:05 24 4
gpt4 key购买 nike

我有一个可以显示或隐藏的详细信息面板。

我怎样才能制作一个简单的淡入淡出效果来显示/隐藏该面板(当然还有它的内容)?

我正在使用 Windows 窗体,控件在 Windows 窗体中没有 opacity 属性。

最佳答案

这在 Winforms 中非常可行,它只需要 看起来 像淡入淡出。一种技术是使用 Control.DrawToBitmap() 创建控件的位图。然后使用计时器从背景位图混合到前景位图。

我将使用 UserControl 而不是 Panel,这样您就可以使用 Winforms 设计器设计控件。然而,该代码将适用于任何类型的控件。向您的项目添加一个新类并粘贴如下所示的代码。编译。使用 Project + Add New Item、Windows Forms 节点、“Inherited User Control”模板从这个创建您自己的 UserControl,然后从弹出列表中选择 FadeControl。正常设计用户控件。

正如所写的那样,只要您将控件添加到父级,控件就会自动从父级的 BackColor 淡入到控件内容。调用 FadeOut() 使其混合回背景。如果你想在控件完成淡入淡出时自动处理控件,请传递 true 。您可以使用 FadeIn() 和 Faded 属性来手动控制淡入淡出。您可以调整注释为//tweakable 的行中的数字以调整动画。如果 parent 有一个非不透明的背景,则需要额外的工作。

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;
}

关于c# - 淡化面板 - Windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com