<分区>
无论如何,我可以淡化面板吗?
我想在单击按钮时淡化菜单中的整个面板。
有什么帮助吗?
最佳答案
这是一个示例,它截取了 Panel
的内容,然后将其淡化到 Panel
或其 Parent
> 的 BackColor
..:
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
控件,并不是真的要在上面绘制,我们应该使用 Picturebox
或 Label
(使用 Autosize=false
);两者都有开箱即用的 DoubleBuffered
属性,并且比 Panels
更好地支持绘图。
关于c# - 如何在 C# 中淡入和淡出面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956407/