c# ProgressBar avec du texte dedans
Bonjour,
Apres des jours et des jours de recherche afin de crée un contrôle personnalisé progressbar avec du texte dedans, j'ai trouvé plein de morceau de code a droite a gauche mais rien de convenable ou fonctionnel au final.
J'ai fini par développé un contrôle qui fonctionne bien.
Je partage le code de l'objet afin que cela serve à d'autre et leur évite de galéré sur le net comme moi.
En premier lieu dans le projet il faut crée un nouvelle class nommé TransparentLabel
voila le code qu'il faut mettre dedans
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| using System;
using System.Drawing;
using System.Windows.Forms;
namespace Custom_Control_Library
{
public class TransparentLabel : Control
{
public TransparentLabel() { TabStop = false; }
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent) { }
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush TBrush = new SolidBrush(ForeColor))
{
this.Width = (Int32)(e.Graphics.MeasureString(Text, Font).Width);
this.Height = (Int32)(e.Graphics.MeasureString(Text, Font).Height);
e.Graphics.DrawString(Text, Font, TBrush, -1, 0);
}
}
}
} |
Ensuite ajouter au projet un nouveau contrôle utilisateur nommé CustomProgressBar
Definir la hauteur de la propriété Size a 23. Mettre dans la zone une ProgressBar dont la propriétés dock et réglé sur Fill.Puis un TransparentLabel crée par la classe précédente.
enfin voila le code de l'objet CustomProgressBar
(La ProgressBar est nommé PgsBar et le TranparentLabel TLbl)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| using System;
using System.Drawing;
using System.Windows.Forms;
namespace Custom_Control_Library
{
public enum ProgressBarDisplayText
{
Percentage,
CustomText
}
public partial class CustomProgressBar : UserControl
{
Int32 Percent = 0;
// Constructor
public CustomProgressBar()
{
// Modify the ControlStyles flags
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
// Override the OnPaint event to set the correct position of Text
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
TLbl.Left = (Int32)((Double)((PgsBar.Width / 2) - (TLbl.Width / 2)));
TLbl.Top = (Int32)((Double)((PgsBar.Height / 2) - (TLbl.Height / 2)));
TLbl.Text = DisplayStyle == ProgressBarDisplayText.Percentage ? Percent.ToString() + " %" : CustomText;
TLbl.Invalidate();
}
// Property to set to decide wheter to print a % or text
public ProgressBarDisplayText DisplayStyle { get; set; }
// Property to hold the custom text
public String CustomText { get; set; }
// Property to access at ProgressBar Value property
public Int32 Value
{
get { return PgsBar.Value; }
set
{
this.PgsBar.Value = value;
Percent = (Int32)(((Double)(PgsBar.Value - PgsBar.Minimum) / (Double)(PgsBar.Maximum - PgsBar.Minimum)) * 100);
TLbl.Text = DisplayStyle == ProgressBarDisplayText.Percentage ? Percent.ToString() + " %" : CustomText;
TLbl.Refresh();
}
}
// Property to access at ProgressBar Minimum property
public Int32 Minimum
{
get { return PgsBar.Minimum; }
set { PgsBar.Minimum = value; }
}
// Property to access at ProgressBar Maximum property
public Int32 Maximum
{
get { return PgsBar.Maximum; }
set { PgsBar.Maximum = value; }
}
}
} |
Voila, plus qu'a placé l'objet dans votre projet et hop voila une progressbar avec du texte dedans. Réglé la propriété DisplayStyle sur Percentage et vous avait le pourcentage de la bar qui s'affiche (calcule automatique en fonction de value, minimum et maximum) ou sur CustomText pour affiché sur Texte (Propriété CustomText de l'objet CustomProgressBar). La position du texte dans la ProgressBar est calculer automatiquement.
En espérant que cela puisse servir.
Nephi