Autor Beitrag
-CrimeTime-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 55



BeitragVerfasst: Fr 12.11.10 08:48 
Hallo,
ich habe mehrere Progressbar's in meinem Projekt, bei 2 von diesen wird auf der Progressbar ein Text Angezeigt, doch leider bei der 3. nicht

hier der Code von der Funktion womit ich mir den Text Anzeigen lassen möchte:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
if (progressBar4.Value >= Me.NextLevel)
            {
                System.Drawing.Graphics blah;
                blah = base.CreateGraphics();
                float x = (progressBar4.Width / 2) - (blah.MeasureString(Me.Experience + "/" + Me.NextLevel, new Font("Arial", (float)8.25)).Width / 2);
                float y = (progressBar4.Height / 2) - (blah.MeasureString(Me.Experience + "/" + Me.NextLevel, new Font("Arial", (float)8.25)).Height / 2);
                progressBar4.CreateGraphics().DrawString(Me.Experience + "/" + Me.NextLevel, new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(x, y));
                progressBar4.Value = Me.Experience;
              //  return;
            }


in der Progressbar werden dann Zahlen Angezeigt z.B. 25798/30000

Gruß Crime

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 12.11.10 11:13 
Hallo,

die Standard-ProgressBar bietet leider keine Unterstützung für Überzeichnen (d.h. das Paint-Event wird nicht ausgelöst). Der Hack mit "CreateGraphics" ist furchtbar und führt, wie du siehst, nicht immer zum richtigen Ergebnis.

Einzige Alternative wäre ein eigenes ProgressBar-Control, wie z.B. www.codeproject.com/...ess/ProgBarPlus.aspx
(der Code ist zwar mit VB.NET entwickelt, kann aber selbstverständlich auch mit C# benutzt werden -)

Edit: hier noch weitere ProgressBar-Controls zur Auswahl:
www.codeproject.com/...sbar-percentage.aspx
www.codeproject.com/...ndedProgressbar.aspx

oder noch sehr funny: www.codeproject.com/...ncedProgressBar.aspx
-CrimeTime- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 55



BeitragVerfasst: Fr 12.11.10 11:23 
mit den Selfmade Progressbars habe ich oft die Erfahrung gemacht das man dort dann keinen Text mehr sehen kann, aber ich werde es Testen.
-CrimeTime- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 55



BeitragVerfasst: Fr 12.11.10 11:37 
sorry wegen Doppelpost, aber könntest du mir erklären wie ich www.codeproject.com/...sbar-percentage.aspx in meinem Projekt einfüge? habe das vor langer Zeit einmal gemacht, irgendwie habe ich es vergessen.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 12.11.10 12:31 
Einfach die Assembly (dll) als Verweis (Reference) zu deinem Projekt hinzufügen (Ordner "Verweise" -> Kontextmenü -> "Verweis hinzufügen...")

Danach das Projekt kompilieren und dann sollte die Komponente in der ToolBox auszuwählen sein (wenn nicht, dann kannst du die Komponente auch explizit der ToolBox hinzufügen).
-CrimeTime- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 55



BeitragVerfasst: Fr 12.11.10 12:36 
also um an die *.dll ran zu kommen muss ich das erstmal Kompilieren, aber dabei treten Fehler auf da ich das Projekt in Visual Studio 2010 umgewandelt habe der Error ist ; Unable to find manifest signing certificate in the certificate store. ProgressBarWithPercentage

Edit:
also benutzte nun den 2ten Link, funktioniert auch alles ganz gut doch meine Frage jetzt ist

was muss ich hier in diesem Code

ausblenden volle Höhe C#-Quelltext
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:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;

namespace ProgressEx
{
  /// <summary>
  /// Represents an extended Windows progress bar control.
  /// </summary>
  [DefaultProperty("PercentageMode")]
  public class ProgressEx : System.Windows.Forms.UserControl
  {
    private int maxValue;                    // Maximum value
    private int minValue;                    // Minimum value
    private float _value;                    // Value property value
    private int stepValue;                    // Step value
    private float percentageValue;                // Percent value
    private int drawingWidth;        // Drawing width according to the logical Value property
    private Color drawingColor;                  // Color used for drawing activities
    private ColorBlend gradientBlender;              // Color mixer object
    private PercentageDrawingMode percentageDrawingMode;    // Percent Drawing type
    private SolidBrush writingBrush;              // Percent writing brush
    private Font writingFont;                  // Font to write Percent with
    private LinearGradientBrush _Drawer;
    /// <summary>
    ///  Specifies how the percentage value should be drawn
    /// </summary>
    public enum PercentageDrawingMode:int
    {
      None=0,              // No Percentage shown
      Center,              // Percentage alwayes centered
      Movable              // Percentage moved with the progress activities
    }
    
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
      
    /// <summary>
    /// Initialize new instance of the ProgressEx control
    /// </summary>
    public ProgressEx()
    {
      InitializeComponent();    // Designer stuff

      maxValue = this.Width;
      minValue = 0;
      stepValue = 1;
      percentageDrawingMode = PercentageDrawingMode.Center;

      // Color Mixer contain 3 colors for (top, center, bottom)
      gradientBlender = new ColorBlend(3);
      // Position of mixing pints is (top, middle, bottom)
      gradientBlender.Positions = new float[]{0.0F, 0.5F, 1.0F};

      DrawingColor = Color.Blue;
      writingBrush = new SolidBrush(Color.Black);
      writingFont = new Font("Arial"10, FontStyle.Bold);

      _Drawer = new LinearGradientBrush(  this.ClientRectangle,
        Color.Black,
        Color.White,
        LinearGradientMode.Vertical);
      // Cancel Reflection while drawing
      this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | 
        ControlStyles.DoubleBuffer, true);
      // Allow Transparent backcolor
      this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 

      this.BackColor = Color.Transparent;

    }

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
        
      writingBrush.Dispose();      // Release Percentage writer brush
      writingFont.Dispose();      // Release Percentage font
      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()
    {
      // 
      // ProgressEx
      // 
      this.BackColor = System.Drawing.SystemColors.Control;
      this.Name = "ProgressEx";
      this.Size = new System.Drawing.Size(25624);
      this.Resize += new System.EventHandler(this.ProgressEx_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.ProgressEx_Paint);

    }
    #endregion

    
    private void ProgressEx_Resize(object sender, System.EventArgs e)
    {
      if(this.Height > 40)
        this.Height = 40;
      if(this.Height < 15)
        this.Height = 15;    // Prevent a Progress shorter than its own percentage
      if(this.Width < 50)
        this.Width = 50;    // Prevent a Progress smaller than its own percentage
      this.Refresh();
    }

    private void ProgressEx_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      // Why to draw outside the control !! ?
      if(_value != 0 & _value <= maxValue)
      {
        // Calculate the right side of drawing edge
        drawingWidth=(int)(this.Width * _value) / (maxValue - minValue);
        // Calculate the Percentage according to the logical value reached 
        percentageValue = (_value / maxValue) * 100;
          
        // Tie our color mixer with the just created brush
        _Drawer.InterpolationColors = gradientBlender;

        // Now we ready to draw, so do the actual drawing
        e.Graphics.FillRectangle(_Drawer, 00, drawingWidth, this.Height);

        // Prepare for Percentage writing only when required
        if(percentageDrawingMode != PercentageDrawingMode.None)
        {
          string st=((int)percentageValue).ToString() + "%";
          // Calculate Percentage rectangle size
          SizeF s=e.Graphics.MeasureString(st,writingFont);

          if(percentageDrawingMode == PercentageDrawingMode.Movable)
          {
            e.Graphics.DrawString( st,writingFont, writingBrush, drawingWidth,
              (e.ClipRectangle.Height / 2 - s.Height / 2));
          }
          else if(percentageDrawingMode == PercentageDrawingMode.Center)
          {
            e.Graphics.DrawString(st, writingFont, writingBrush,
              new PointF((e.ClipRectangle.Width / 2 - s.Width / 2),
              (e.ClipRectangle.Height / 2 - s.Height / 2)));
          }
        }
      }
    }
    /// <summary>
    /// Increment the progress one step
    /// </summary>
    public void StepForword()
    {
      if((_value + stepValue) < maxValue)    // If valid increment the value by step size
      {
        _value += stepValue;
        this.Refresh();
      }
      else                  // If not dont exceed the maximum allowed
      {
        _value = maxValue;          
        this.Refresh();
      }
    }
    /// <summary>
    /// Decrement the progress one step
    /// </summary>
    public void StepBackword()
    {
      if((_value - stepValue) > minValue)    // If valid decrement the value by step size
      {
        _value -= stepValue;
        this.Refresh();
      }
      else                  // If not dont exceed the minimum allowed
      {    
        _value = minValue;          
        this.Refresh();
      }
    }

    /// <summary>
    /// Gets or Sets a value determine how to display Percentage value
    /// </summary>
    [Category("Behavior"),Description("Specify how to display the Percentage value")]
    public PercentageDrawingMode PercentageMode
    {
      get
      {
        return percentageDrawingMode;
      }
      set
      {
        percentageDrawingMode = value;
        this.Refresh();
      }
    }
    /// <summary>
    /// Gets or Sets the color used to draw the Progress activities
    /// </summary>
    [Category("Appearance"),Description("Specify the color used to draw the progress activities")]
    public Color DrawingColor
    {
      get
      {
        return drawingColor;
      }
      set
      {
        drawingColor = value;
        // If assigned then remix the colors used for gradient display
        gradientBlender.Colors[0] = ControlPaint.Dark(value);
        gradientBlender.Colors[1] = ControlPaint.Light(value);
        gradientBlender.Colors[2] = ControlPaint.Dark(value);
        this.Refresh();
      }
    }
    /// <summary>
    ///  Gets or sets the maximum value of the range of the control. 
    /// </summary>
    [Category("Layout"),Description("Specify the maximum value the progress can increased to")]
    public int Maximum
    {
      get
      {
        return maxValue;
      }
      set
      {
        maxValue = value;
        this.Refresh();
      }
    }
    /// <summary>
    /// Gets or sets the minimum value of the range of the control.
    /// </summary>
    [Category("Layout")]
    public int Minimum
    {
      get
      {
        return minValue;
      }
      set
      {
        minValue = value;
        this.Refresh();
      }
    }
    /// <summary>
    ///  Gets or sets the amount by which a call to the System.Windows.Forms.ProgressBar.
    ///  StepForword method increases the current position of the progress bar.
    /// </summary>
    [Category("Layout")]
    public int Step
    {
      get
      {
        return stepValue;
      }
      set
      {
        stepValue = value;
        this.Refresh();
      }
    }
    /// <summary>
    /// Gets or sets the current position of the progress bar. 
    /// </summary>
    /// <exception cref="System.ArgumentException">The value specified is greater than the value of
    /// the System.Windows.Forms.ProgressBar.Maximum property.  -or- The value specified is less
    /// than the value of the System.Windows.Forms.ProgressBar.Minimum property</exception>
    [Category("Layout")]
    public int Value
    {
      get
      {
        return (int)_value;
      }
      set
      {
        // Protect the value and refuse any invalid values
        // 
        // Here we may just handle invalid values and dont bother the client by exceptions
        if(value > maxValue | value < minValue)
        {
          throw new ArgumentException("Invalid value used");
        }
        _value = value;
        this.Refresh();
      }
    }
    /// <summary>
    /// Gets the Percent value the Progress activities reached
    /// </summary>
    public int Percent
    {
      get
      {
        // Its float value, so to be accurate round it then return
        return (int)Math.Round(percentageValue);
      }
    }
      
    // This property exist in the parent, ovveride it for our own good
    /// <summary>
    /// Gets or Sets the color used to draw the Precentage value
    /// </summary>
    [Category("Appearance"),Description("Specify the color used to draw the Percentage value")]
    public override Color ForeColor
    {
      get
      {
        return writingBrush.Color;
      }
      set
      {
        writingBrush.Color = value;
        this.Invalidate(false);
      }
    }

  }
}


Ändern damit ich dort statt der ProzentAnzeige meine Zahlen sehen kann?

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt