Workin with progress bars

  • Thread starter Thread starter Scirious
  • Start date Start date
S

Scirious

People, I want to do same things with a progress bar but I don't know how.
So, how do I change it to make it a solid bar, not a step byt step as the
default? And how do I make it a vertical bar? Also, how do I put a progress
bar withing a button?

TIA,
Scirious.
 
People, I want to do same things with a progress bar but I don't know how.
So, how do I change it to make it a solid bar, not a step byt step as the
default? And how do I make it a vertical bar? Also, how do I put a
progress
bar withing a button?

1) Launch your Internet browser (e.g. IE, FireFox, Netscape, Opera etc)

2) Navigate to http://www.google.com

3) Enter the text below in the box:

"C#" "progress bar"

4) Hit the button
 
Well, I've found the answers to almost all of my questions, but how to put
the progress bar within a button haven't been answered. Actually, to make
my question clear, is it possoble to put any control inside any other
control?

TIA,
Scirious.
 
Ok, sorry for the preveious behavior. I've already search Google and some
foruns for this but there is yet one question unanswered. How do I add a
progress bar to a button? Would Windows Buttons behave like a container
itself and allow me to add other components to it the same way SWING or QT
do?

TIA,
Scirious.
 
Scirious said:
Ok, sorry for the preveious behavior. I've already search Google and some
foruns for this but there is yet one question unanswered. How do I add a
progress bar to a button? Would Windows Buttons behave like a container
itself and allow me to add other components to it the same way SWING or QT
do?

You can add a control to a button, but the control will (by default) eat
mouse clicks etc., which is probably not what you want.

If you want the control to be just a decorator, you need to change how
the control reacts by overriding its response to the WM_NCHITTEST
message.

Try this code out:

---8<---
using System;
using System.Windows.Forms;
using System.Drawing;

class App
{
class SimpleProgressBar : Panel
{
private double _percent;

public double Percent
{
get { return _percent; }
set
{
if (value < 0)
_percent = 0;
else if (value > 100)
_percent = 100;
else
_percent = value;
Invalidate();
}
}

protected override void OnPaint(PaintEventArgs e)
{
int width = (int) (Width * _percent / 100.0);

Rectangle erase = ClientRectangle;
erase.Offset(width, 0);
e.Graphics.FillRectangle(SystemBrushes.Window, erase);
Rectangle selected = ClientRectangle;
selected.Width = width;
e.Graphics.FillRectangle(SystemBrushes.Highlight, selected);

base.OnPaint(e);
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0014: // WM_ERASEBKGND
m.Result = new IntPtr(0);
break;

case 0x0084: // WM_NCHITTEST
m.Result = new IntPtr(-1); // HTTRANSPARENT
break;

default:
base.WndProc(ref m);
break;
}
}
}

static void Main()
{
Form form = new Form();

Button button = new Button();
button.Parent = form;
button.Size = new Size(200, 200);

SimpleProgressBar bar = new SimpleProgressBar();
bar.Parent = button;
bar.Size = new Size(100, 10);
bar.Location = new Point(50, 95);

Timer tick = new Timer();
tick.Interval = 20;
tick.Tick += delegate
{
bar.Percent = (bar.Percent + 1) % 101;
};
tick.Enabled = true;

Application.Run(form);
}
}
--->8---

-- Barry
 
You can set a background image on the button, but would need to pre-build
the graphics and set them as progress bar changes.
WPF should give us many more options, such as make the button translucent
and put he PB behind the button, etc.
--
William Stacey [MVP]

| Ok, sorry for the preveious behavior. I've already search Google and some
| foruns for this but there is yet one question unanswered. How do I add a
| progress bar to a button? Would Windows Buttons behave like a container
| itself and allow me to add other components to it the same way SWING or QT
| do?
|
| TIA,
| Scirious.
 
Scirious said:
Ok, sorry for the preveious behavior. I've already search Google and some
foruns for this but there is yet one question unanswered. How do I add a
progress bar to a button? Would Windows Buttons behave like a container
itself and allow me to add other components to it the same way SWING or QT
do?

TIA,
Scirious.

Hi Scirious,

I've written you a class, that may do what you want. Perhaps it will be of
some use.

You need to add using clauses for System.Drawing and System.Windows.Forms,
along with the usual System and whatnot.

///
public class ProgressButton : Button
{
private int _max;
private int _value;

public ProgressButton ( )
{
this.TextAlign = ContentAlignment.TopCenter;

_max = 100;
_value = 0;
}

public int MaximumProgressValue
{
get { return _max; }
set
{
_max = value;

if ( this.ProgressValue > _max )
this.ProgressValue = _max;
}
}

public int ProgressValue
{
get { return _value; }
set
{
_value = value;

if ( _value > _max )
_value = _max;
else if ( _value < 0 )
_value = 0;

this.Invalidate();
}
}

protected override void OnPaint ( PaintEventArgs pevent )
{
base.OnPaint( pevent );

const int padding = 6;
const int barHeight = 16;

Rectangle progressRect = new Rectangle( padding,
this.Height - padding - barHeight,
this.Width - ( padding * 2 ),
barHeight );

ProgressBarRenderer.DrawHorizontalBar( pevent.Graphics,
progressRect );

progressRect.Inflate( -3, -3 );
progressRect.Width = (int)
( progressRect.Width * ( (double) _value / _max ) );

ProgressBarRenderer.DrawHorizontalChunks( pevent.Graphics,
progressRect );
}
}
///

Normally, I don't like putting parameters on seperate lines, but to conform
to news group standards, I've done it.

To use, add the control to your form as you normally would.
+ MaximumProgressValue is the maximum value of your progress bar.
+ ProgressValue is the value of the progress bar.

It looks okay using Windows XP themes. I haven't tested it anywhere else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top