Feedback

C# - Erweiterte Lade-Form

Veröffentlicht von am 1/18/2014
(0 Bewertungen)
Diese Klasse ist mehr oder weniger die Erweiterung dieses Snippets: http://dotnet-snippets.de/snippet/lade-anzeige-fuer-form-load/1719
Zur Erklärung: Man kann mit der Klasse schnell und einfach eine tämporäre "Lade-Anzeige" anzeigen. Diese enthält Text, kann aber auch ein Bild enthalten, dessen vertikale Ausrichtung einstellbar ist. Außerdem wächst die Größe dynamisch, wofür sich die maximalbreite des Textes in Pixeln angeben lässt. Bei Übercshreitung dieser wird eine neue Zeile angefangen.

Beispiel
LoadingForm lf = new LoadingForm(); // Neue Instanz
lf.Image = new Bitmap("image.png"); // Bild zuweisen
lf.Text = "Dies ist ein etwas längerer und daher Mehrzeiliger Text. (Je nach Max-Breite-Einstellung)"; // Text zuweisen
lf.MaxTextWidth = 200; // Maximale Textbreite zuweisen
lf.ImageVerticalCenter = true; // Wert setzen, ob Bild oben oder mittig ausgerichtet ist
lf.TextFont = new Font(FontFamily.GenericSansSerif, 10); // Schriftart zuweisen
lf.ShowForm(); // Form anzeigen
// Tu was
lf.CloseForm(); // Form schließen

Beispiel-Bild
Ein Bild mit Screenshots verschiedener Einstellungsmöglichkeiten findet sich unter: https://dl.dropboxusercontent.com/s/dg2glrmaut939xd/loading_form.png
Das erste Bild ist das, was dem aufgeführten Beispiel entspricht.
Möchte man nicht die Maximalbreite des Textes sondern der ganzen Form festlegen muss man ein ganz kleines bisschen "Tricksen":
void SetMaxFormWidth(int with)
{
Lf.MaxTextWidth = width - 45 - lf.Image.Width;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

/// <summary>
/// Klasse zum Anzeigen einer temporären Informationsanzeige mit Bild und Text.
/// </summary>
public class LoadingForm
{
    /// <summary>
    /// Instanziert eine neue LoadingForm
    /// </summary>
    public LoadingForm()
    {
        loading = new Form();
    }
    /// <summary>
    /// Das Bild das links neben dem Text angezeigt wird.
    /// </summary>
    public Image Image
    {
        get { return image; } set { image = value; }
    }
    /// <summary>
    /// Angabe, ob das Bild in vertikaler Richtung mittig angezeigt werden soll. Wenn nicht wird es oben links angezeigt.
    /// </summary>
    public bool ImageVerticalCenter
    {
        get { return imageCenter; }
        set { imageCenter = value; }
    }
    /// <summary>
    /// Der Text, der angezeigt wird.
    /// </summary>
    public string Text
    {
        get { return text; }
        set { text = value; }
    }
    /// <summary>
    /// Die Schriftart des anzuzeigenden Textes.
    /// </summary>
    public Font TextFont
    {
        get { return font; }
        set { font = value; }
    }
    /// <summary>
    /// Die  Anzahl von Pixeln bei deren Überschreitung (durch die Textbreite) eine neue Zeile begonnen wird.
    /// </summary>
    public int MaxTextWidth
    {
        get { return maxTextWidth; }
        set { maxTextWidth = value; }
    }

    private Form loading;
    private Image image = null;
    private bool imageCenter = false;
    private string text = "Loading...";
    private Font font = new Font(FontFamily.GenericSansSerif, 10);
    private int maxTextWidth = 200;

    private void InitiallizeForm()
    {
        if (image == null)
            image = new Bitmap(1, 1);
        loading = new Form();
        loading.ControlBox = false;
        loading.FormBorderStyle = FormBorderStyle.FixedSingle;
        loading.StartPosition = FormStartPosition.CenterScreen;
        loading.ShowInTaskbar = false;

        Label lbLoad = new Label();
        lbLoad.AutoSize = true;
        lbLoad.Top = 15;
        lbLoad.MaximumSize = new Size(maxTextWidth, 1000);
        lbLoad.Font = font;
        lbLoad.Text = text;
        loading.Controls.Add(lbLoad);

        Label lbImage = new Label();
        lbImage.Size = image.Size;
        lbImage.Image = image;
        if (!imageCenter)
            lbImage.ImageAlign = ContentAlignment.TopCenter;
        lbImage.Location = new Point(15, 15);
        loading.Controls.Add(lbImage);

        lbLoad.Left = lbImage.Left + lbImage.Width + 15;
        int loadWidth = lbLoad.Width;
        int labelsHeight = Math.Max(lbLoad.Height, lbImage.Height);
        lbLoad.AutoSize = false;
        lbLoad.Height = labelsHeight;
        lbLoad.Width = loadWidth;
        lbLoad.TextAlign = ContentAlignment.MiddleLeft;
        lbImage.Height = labelsHeight;
        loading.Width = lbLoad.Left + lbLoad.Width + 15;
        loading.Height = lbLoad.Top + lbLoad.Height + 15;
    }
        
    /// <summary>
    /// Zeigt das Formular an.
    /// </summary>
    public void ShowForm()
    {
        ShowForm(false);
    }

    /// <summary>
    /// Zeigt das Formular an.
    /// </summary>
    /// <param name="ShowDialog">Gibt an, ob das Formular als Dialog angezeigt wird (nicht empfohlen).</param>
    public void ShowForm(bool ShowDialog)
    {
        InitiallizeForm();
        if (ShowDialog)
            loading.ShowDialog();
        else
        {
            loading.Show();
            loading.Update();
        }
    }

    /// <summary>
    /// Schließt das Formular.
    /// </summary>
    public void CloseForm()
    {
        loading.Dispose();
        loading.Close();
    }
}
Abgelegt unter Form, Anzeige, Laden, Erweitert, Extended, Load.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!