Control in WinForm einblenden  
Frank Dzaebel, erstellt am: 2.4.2006, zuletzt geändert:  2.4.2006
Kategorie: Windows Forms, .NET-Version: 1.1/2.0, Download

Man kann ein Control (oder mehrere) in eine Windows Form langsam einblenden, indem man eine überdeckende Form mit einer sich über Timer ändernden Opacity-Eigenschaft anzeigt. Dies ist eine Möglichkeit von mehreren.

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

namespace FadeIn
{
  public partial class Form1 : Form //für 1.1: "partial" weglassen
  {
    public Form1()
    {
      InitializeComponent();
    }

    const double opacityEpsion = 0.05;
    const int timerIntervall   = 60;

    private void timer1_Tick(object sender,EventArgs e)
    {
      fPan.Location = new Point(Left + pOrig.Left,Top + pOrig.Top + upperHeight);
      fPan.Opacity -= opacityEpsion;
      if (fPan.Opacity <= opacityEpsion)
      {
        timer1.Enabled = false; fPan.Hide(); return;
      }
      if (!fPan.Visible) fPan.Visible = true; 
      if (!panel1.Visible) panel1.Visible = true;
      this.Focus();
    }

    Rectangle pOrig;
    int upperHeight;
    private void Form1_Load(object sender,EventArgs e)
    {
      upperHeight = Height - ClientRectangle.Height;
      panel1.Visible = false; pOrig = panel1.Bounds;
      fPan.FormBorderStyle = FormBorderStyle.None;
      fPan.Opacity = 0.0; fPan.Size = new Size(0,0);
      fPan.Hide();
    }

    Form fPan = new Form();
    private void button1_Click(object sender,EventArgs e)
    {
      fPan.Location = new Point(Left + pOrig.Left,Top + pOrig.Top + upperHeight);
      fPan.Visible = true; fPan.TopMost = true; fPan.ShowInTaskbar = false;
      fPan.Location = new Point(Left + pOrig.Left,Top + pOrig.Top + upperHeight);
      fPan.Opacity = 1.0; fPan.Size = pOrig.Size;
      timer1.Interval = timerIntervall; timer1.Enabled = true;
    }

    private void button2_Click(object sender,EventArgs e)
    {
      panel1.Visible = false;
    }
  }
}