Recently, I needed a vertical progress bar for my win forms application. The derived class is like below. I also need to add text on progress bar. A label on it doesn't work beacuse of transperancy issues. After some research, I have found something. But the problem is, while progress bar is vertical, the text on it appears horizontal. I need it vertical too. How can I do that?
Thanks.
public class VProgressBar : ProgressBar
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6)
{
cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
}
return cp;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x000F)
{
using (Graphics graphics = CreateGraphics())
using (SolidBrush brush = new SolidBrush(ForeColor))
{
SizeF textSize = graphics.MeasureString(Text, Font);
graphics.DrawString(Text, Font, brush, (Width - textSize.Width) / 2, (Height - textSize.Height) / 2);
}
}
}
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
Refresh();
}
}
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
}
You need to use StringFormat to draw your string in vertical, using the flag. Try the following in your WndProc
method:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x000F)
{
using (Graphics graphics = CreateGraphics())
using (SolidBrush brush = new SolidBrush(ForeColor))
{
StringFormat format = new StringFormat(
StringFormatFlags.DirectionVertical);
SizeF textSize = graphics.MeasureString(Text, Font);
graphics.DrawString(
Text, Font, brush,
(Width / 2 - textSize.Height / 2),
(Height / 2 - textSize.Width / 2),
format);
}
}
}