while visual update studio sirve que progressbar proceso para mientras ejemplos ejecuta bar c# winforms progress-bar

visual - progressbar mientras se ejecuta un proceso c#



¿Cómo pongo texto en ProgressBar? (7)

He usado ProgressBar Control en mi aplicación de escritorio c #. Lo he usado en un hilo distinto del hilo en el que se ha declarado el control. Funciona bien. Ahora me pregunto cómo puedo mostrar algo de texto dentro del control de la barra de progreso como "Iniciando el registro", etc. También quiero usarlo como barra de progreso de marquesina. Por favor, ayúdenme.


EVITE EL TEXTO DE FLICKERING

La solution proporcionada por Barry arriba es excelente, pero está el "problema de parpadeo".

Tan pronto como el Valor esté por encima de cero, OnPaint será envidado repetidamente y el texto parpadeará.

Hay una solución a esto. No necesitamos VisualStyles para el objeto ya que lo dibujaremos con nuestro propio código.

Agregue el siguiente código al objeto personalizado que Barry escribió y evitará el parpadeo:

[DllImportAttribute("uxtheme.dll")] private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist); protected override void OnHandleCreated(EventArgs e) { SetWindowTheme(this.Handle, "", ""); base.OnHandleCreated(e); }

Yo no escribí esto yo mismo. Lo encontró aquí: https://.com/a/299983/1163954

Lo he probado y funciona.


¡Utilizo este código simple, y funciona!

for (int i = 0; i < N * N; i++) { Thread.Sleep(50); progressBar1.BeginInvoke(new Action(() => progressBar1.Value = i)); progressBar1.CreateGraphics().DrawString(i.ToString() + "%", new Font("Arial", (float)10.25, FontStyle.Bold), Brushes.Red, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7)); }

Solo tiene un problema: cuando la barra de progreso comienza a aumentar, algunas veces el porcentaje se oculta y luego vuelve a aparecer. Yo no lo escribí yo mismo. Lo encontré aquí: texto en la barra de progreso en c #

Lo usé y trabajé.


Aliterativamente, puede intentar colocar un control Label y colocarlo encima del control de la barra de progreso. Luego puede establecer el texto que desee en la etiqueta. No he hecho esto yo mismo. Si funciona, debería ser una solución más simple que anular en la pintura.


Creé un control InfoProgressBar que usa un control TransparentLabel. Probando en un formulario con un temporizador, obtengo algunas fallas técnicas que muestran el texto cada 30-40 cambios de valor si se usa un intervalo de temporizador de menos de 250 milisegundos (probablemente debido al tiempo requerido para actualizar la pantalla es mayor que el intervalo del temporizador) .

Sería posible modificar el método UpdateText para insertar todos los valores calculados en CustomText, pero aún no es algo que haya necesitado. Esto eliminaría la necesidad de la propiedad DisplayType y enumeraría.

La clase TransparentLabel se creó agregando un nuevo UserControl y cambiándolo para heredar de Label con la siguiente implementación:

using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Utils.GUI { public partial class TransparentLabel : Label { // hide the BackColor attribute as much as possible. // setting the base value has no effect as drawing the // background is disabled [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] public override Color BackColor { get { return Color.Transparent; } set { } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x20; // WS_EX_TRANSPARENT return cp; } } public override string Text { get { return base.Text; } set { base.Text = value; if(Parent != null) Parent.Invalidate(Bounds, false); } } public override ContentAlignment TextAlign { get { return base.TextAlign; } set { base.TextAlign = value; if(Parent != null) Parent.Invalidate(Bounds, false); } } public TransparentLabel() { InitializeComponent(); SetStyle(ControlStyles.Opaque, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, false); base.BackColor = Color.Transparent; } protected override void OnMove(EventArgs e) { base.OnMove(e); RecreateHandle(); } protected override void OnPaintBackground(PaintEventArgs pevent) { // do nothing } } }

No hice ningún cambio en el código de diseñador relacionado, pero aquí está para completarlo.

namespace Utils.GUI { partial class TransparentLabel { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if(disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); } #endregion } }

Luego creé otro UserControl nuevo y lo cambié para derivar de ProgressBar con la siguiente implementación:

using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.Design; using System.Windows.Forms.Design.Behavior; namespace Utils.GUI { [Designer(typeof(InfoProgressBarDesigner))] public partial class InfoProgressBar : ProgressBar { // designer class to add font baseline snapline by copying it from the label private class InfoProgressBarDesigner : ControlDesigner { public override IList SnapLines { get { IList snapLines = base.SnapLines; InfoProgressBar control = Control as InfoProgressBar; if(control != null) { using(IDesigner designer = TypeDescriptor.CreateDesigner(control.lblText, typeof(IDesigner))) { if(designer != null) { designer.Initialize(control.lblText); ControlDesigner boxDesigner = designer as ControlDesigner; if(boxDesigner != null) { foreach(SnapLine line in boxDesigner.SnapLines) { if(line.SnapLineType == SnapLineType.Baseline) { snapLines.Add(new SnapLine(SnapLineType.Baseline, line.Offset, line.Filter, line.Priority)); break; } } } } } } return snapLines; } } } // enum to select the type of displayed value public enum ProgressBarDisplayType { Custom = 0, Percent = 1, Progress = 2, Remain = 3, Value = 4, } private string _customText; private ProgressBarDisplayType _displayType; private int _range; [Bindable(false)] [Browsable(true)] [DefaultValue("{0}")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] // {0} is replaced with the result of the selected calculation public string CustomText { get { return _customText; } set { _customText = value; UpdateText(); } } [Bindable(false)] [Browsable(true)] [DefaultValue(ProgressBarDisplayType.Percent)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public ProgressBarDisplayType DisplayType { get { return _displayType; } set { _displayType = value; UpdateText(); } } [Bindable(false)] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] // don''t use the lblText font as if it is null, it checks the parent font (i.e. this property) and gives an infinite loop public override Font Font { get { return base.Font; } set { base.Font = value; } } [Bindable(false)] [Browsable(true)] [DefaultValue(100)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public new int Maximum { get { return base.Maximum; } set { base.Maximum = value; _range = base.Maximum - base.Minimum; UpdateText(); } } [Bindable(false)] [Browsable(true)] [DefaultValue(0)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public new int Minimum { get { return base.Minimum; } set { base.Minimum = value; _range = base.Maximum - base.Minimum; UpdateText(); } } [Bindable(false)] [Browsable(true)] [DefaultValue(ContentAlignment.MiddleLeft)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public ContentAlignment TextAlign { get { return lblText.TextAlign; } set { lblText.TextAlign = value; } } [Bindable(false)] [Browsable(true)] [DefaultValue(typeof(Color), "0x000000")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public Color TextColor { get { return lblText.ForeColor; } set { lblText.ForeColor = value; } } [Bindable(false)] [Browsable(true)] [DefaultValue(0)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public new int Value { get { return base.Value; } set { base.Value = value; UpdateText(); } } public InfoProgressBar() { InitializeComponent(); CustomText = "{0}"; DisplayType = ProgressBarDisplayType.Percent; Maximum = 100; Minimum = 0; TextAlign = ContentAlignment.MiddleLeft; TextColor = Color.Black; Value = 0; // means the label gets drawn in front of the progress bar lblText.Parent = this; _range = base.Maximum - base.Minimum; } protected void UpdateText() { switch(DisplayType) { case ProgressBarDisplayType.Custom: { lblText.Text = _customText; break; } case ProgressBarDisplayType.Percent: { if(_range > 0) { lblText.Text = string.Format(_customText, string.Format("{0}%", (int)((Value * 100) / _range))); } else { lblText.Text = "100%"; } break; } case ProgressBarDisplayType.Progress: { lblText.Text = string.Format(_customText, (Value - Minimum)); break; } case ProgressBarDisplayType.Remain: { lblText.Text = string.Format(_customText, (Maximum - Value)); break; } case ProgressBarDisplayType.Value: { lblText.Text = string.Format(_customText, Value); break; } } } public new void Increment(int value) { base.Increment(value); UpdateText(); } public new void PerformStep() { base.PerformStep(); UpdateText(); } } }

Y el código de diseñador:

namespace Utils.GUI { partial class InfoProgressBar { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if(disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.lblText = new Utils.GUI.TransparentLabel(); this.SuspendLayout(); // // lblText // this.lblText.BackColor = System.Drawing.Color.Transparent; this.lblText.Dock = System.Windows.Forms.DockStyle.Fill; this.lblText.Location = new System.Drawing.Point(0, 0); this.lblText.Name = "lblText"; this.lblText.Padding = new System.Windows.Forms.Padding(3, 0, 3, 0); this.lblText.Size = new System.Drawing.Size(100, 23); this.lblText.TabIndex = 0; this.lblText.Text = "transparentLabel1"; this.ResumeLayout(false); } #endregion private TransparentLabel lblText; } }


Creo que crear un control llamado, por ejemplo, InfoProgresBar, que proporcione esta funcionalidad con una etiqueta o dos (Trabajo principal, Trabajo actual) y ProgressBar y utilizarlo en lugar de ProgressBar.


Deberá anular el método OnPaint, llamar a la implementación base y pintar su propio texto.

Tendrá que crear su propia CustomProgressBar y luego anular OnPaint para dibujar el texto que desee.

Clase de barra de progreso personalizado

namespace ProgressBarSample { public enum ProgressBarDisplayText { Percentage, CustomText } class CustomProgressBar: ProgressBar { //Property to set to decide whether to print a % or Text public ProgressBarDisplayText DisplayStyle { get; set; } //Property to hold the custom text public String CustomText { get; set; } public CustomProgressBar() { // Modify the ControlStyles flags //http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); } protected override void OnPaint(PaintEventArgs e) { Rectangle rect = ClientRectangle; Graphics g = e.Graphics; ProgressBarRenderer.DrawHorizontalBar(g, rect); rect.Inflate(-3, -3); if (Value > 0) { // As we doing this ourselves we need to draw the chunks on the progress bar Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height); ProgressBarRenderer.DrawHorizontalChunks(g, clip); } // Set the Display text (Either a % amount or our custom text string text = DisplayStyle == ProgressBarDisplayText.Percentage ? Value.ToString() + ''%'' : CustomText; using (Font f = new Font(FontFamily.GenericSerif, 10)) { SizeF len = g.MeasureString(text, f); // Calculate the location of the text (the middle of progress bar) // Point location = new Point(Convert.ToInt32((rect.Width / 2) - (len.Width / 2)), Convert.ToInt32((rect.Height / 2) - (len.Height / 2))); Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2)); // The commented-out code will centre the text into the highlighted area only. This will centre the text regardless of the highlighted area. // Draw the custom text g.DrawString(text, f, Brushes.Red, location); } } } }

Ejemplo de aplicación WinForms

using System; using System.Linq; using System.Windows.Forms; using System.Collections.Generic; namespace ProgressBarSample { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Set our custom Style (% or text) customProgressBar1.DisplayStyle = ProgressBarDisplayText.CustomText; customProgressBar1.CustomText = "Initialising"; } private void btnReset_Click(object sender, EventArgs e) { customProgressBar1.Value = 0; btnStart.Enabled = true; } private void btnStart_Click(object sender, EventArgs e) { btnReset.Enabled = false; btnStart.Enabled = false; for (int i = 0; i < 101; i++) { customProgressBar1.Value = i; // Demo purposes only System.Threading.Thread.Sleep(100); // Set the custom text at different intervals for demo purposes if (i > 30 && i < 50) { customProgressBar1.CustomText = "Registering Account"; } if (i > 80) { customProgressBar1.CustomText = "Processing almost complete!"; } if (i >= 99) { customProgressBar1.CustomText = "Complete"; } } btnReset.Enabled = true; } } }


Intenté colocar una etiqueta con fondo transparente sobre una barra de progreso, pero nunca lo hice funcionar correctamente. Así que encontré la solución de Barry aquí muy útil, aunque me perdí la hermosa barra de progreso del estilo Vista. Así que fusioné la solución de Barry con http://www.dreamincode.net/forums/topic/243621-percent-into-progress-bar/ y logré mantener la barra de progreso nativa, mientras mostraba el porcentaje de texto o texto personalizado sobre ella. No veo ningún parpadeo en esta solución tampoco. Perdón por desenterrar y el hilo viejo, pero necesitaba esto hoy y para que otros lo puedan necesitar también.

public enum ProgressBarDisplayText { Percentage, CustomText } class ProgressBarWithCaption : ProgressBar { //Property to set to decide whether to print a % or Text private ProgressBarDisplayText m_DisplayStyle; public ProgressBarDisplayText DisplayStyle { get { return m_DisplayStyle; } set { m_DisplayStyle = value; } } //Property to hold the custom text private string m_CustomText; public string CustomText { get { return m_CustomText; } set { m_CustomText = value; this.Invalidate(); } } private const int WM_PAINT = 0x000F; protected override void WndProc(ref Message m) { base.WndProc(m); switch (m.Msg) { case WM_PAINT: int m_Percent = Convert.ToInt32((Convert.ToDouble(Value) / Convert.ToDouble(Maximum)) * 100); dynamic flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis; using (Graphics g = Graphics.FromHwnd(Handle)) { using (Brush textBrush = new SolidBrush(ForeColor)) { switch (DisplayStyle) { case ProgressBarDisplayText.CustomText: TextRenderer.DrawText(g, CustomText, new Font("Arial", Convert.ToSingle(8.25), FontStyle.Regular), new Rectangle(0, 0, this.Width, this.Height), Color.Black, flags); break; case ProgressBarDisplayText.Percentage: TextRenderer.DrawText(g, string.Format("{0}%", m_Percent), new Font("Arial", Convert.ToSingle(8.25), FontStyle.Regular), new Rectangle(0, 0, this.Width, this.Height), Color.Black, flags); break; } } } break; } } }