ProgressBar颜色设定及绘制文字显示
来源:互联网 发布:慧思达软件多少钱 编辑:程序博客网 时间:2024/06/05 08:27
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var progressBar = new MyProgressBar()
{
Location = new Point(20, 20),
Size = new Size(100, 14),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
ForeColor = Color.Blue
};
var progressBar2 = new MyProgressBar()
{
Location = new Point(20, 60),
Size = new Size(100, 14),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
ForeColor = Color.Red
};
this.Controls.Add(progressBar);
this.Controls.Add(progressBar2);
var timer = new Timer {Interval = 100};
timer.Tick += (s, e) => { progressBar.Value = progressBar.Value % 100 + 2; progressBar2.Value = progressBar2.Value % 100 + 2; };
timer.Start();
}
}
public class MyProgressBar : ProgressBar
{
public MyProgressBar()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush brush = null;
Rectangle bounds = new Rectangle(0, 0, base.Width, base.Height);
//...
//e.Graphics.FillRectangle(new SolidBrush(Color.Black), 1, 1, bounds.Width - 2, bounds.Height - 2);
ProgressBarRenderer.DrawHorizontalBar(e.Graphics, bounds);
bounds.Height -= 4;
bounds.Width = ((int)(bounds.Width * (((double)base.Value) / ((double)base.Maximum)))) - 4;
brush = new SolidBrush(this.ForeColor);
e.Graphics.FillRectangle(brush, 2, 2, bounds.Width, bounds.Height);
Rectangle rect = ClientRectangle;
Graphics g = e.Graphics;
//ProgressBarRenderer.DrawHorizontalBar(g, rect);
//rect.Inflate(-3, -3);
//if (Value > 0)
//{
// var clip = new Rectangle(rect.X, rect.Y, (int)((float)Value / Maximum * rect.Width), rect.Height);
// ProgressBarRenderer.DrawHorizontalChunks(g, clip);
//}
string text = Value + "%";
using (var font = new Font(FontFamily.GenericSerif, 10))
{
SizeF sz = g.MeasureString(text, font);
var location = new PointF(rect.Width / 2 - sz.Width / 2, rect.Height / 2 - sz.Height / 2 + 2);
g.DrawString(text, font, Brushes.Red, location);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var progressBar = new MyProgressBar()
{
Location = new Point(20, 20),
Size = new Size(100, 14),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
ForeColor = Color.Blue
};
var progressBar2 = new MyProgressBar()
{
Location = new Point(20, 60),
Size = new Size(100, 14),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
ForeColor = Color.Red
};
this.Controls.Add(progressBar);
this.Controls.Add(progressBar2);
var timer = new Timer {Interval = 100};
timer.Tick += (s, e) => { progressBar.Value = progressBar.Value % 100 + 2; progressBar2.Value = progressBar2.Value % 100 + 2; };
timer.Start();
}
}
public class MyProgressBar : ProgressBar
{
public MyProgressBar()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush brush = null;
Rectangle bounds = new Rectangle(0, 0, base.Width, base.Height);
//...
//e.Graphics.FillRectangle(new SolidBrush(Color.Black), 1, 1, bounds.Width - 2, bounds.Height - 2);
ProgressBarRenderer.DrawHorizontalBar(e.Graphics, bounds);
bounds.Height -= 4;
bounds.Width = ((int)(bounds.Width * (((double)base.Value) / ((double)base.Maximum)))) - 4;
brush = new SolidBrush(this.ForeColor);
e.Graphics.FillRectangle(brush, 2, 2, bounds.Width, bounds.Height);
Rectangle rect = ClientRectangle;
Graphics g = e.Graphics;
//ProgressBarRenderer.DrawHorizontalBar(g, rect);
//rect.Inflate(-3, -3);
//if (Value > 0)
//{
// var clip = new Rectangle(rect.X, rect.Y, (int)((float)Value / Maximum * rect.Width), rect.Height);
// ProgressBarRenderer.DrawHorizontalChunks(g, clip);
//}
string text = Value + "%";
using (var font = new Font(FontFamily.GenericSerif, 10))
{
SizeF sz = g.MeasureString(text, font);
var location = new PointF(rect.Width / 2 - sz.Width / 2, rect.Height / 2 - sz.Height / 2 + 2);
g.DrawString(text, font, Brushes.Red, location);
}
}
}
}
0 0
- ProgressBar颜色设定及绘制文字显示
- c# progressBar进度条改变颜色及显示进度数字
- 绘制文字部分颜色
- 在ProgressBar上显示文字
- 重写ProgressBar, 显示百分比文字
- 设定MyEclipse编辑代码区域文字的大小及非关键字的字体、字形和颜色
- iOS设定指定文字颜色为红色
- QT按钮背景颜色设置及文字显示位置设置
- jquery 文字(颜色)闪烁显示
- Xcode绘制文字及图片
- ProgressBar颜色
- Dreamweaver中如何设定文字——大小、字体、颜色
- 在ProgressBar上加文字----显示百分比的进度条
- 在ProgressBar上加文字----显示百分比的进度条
- 在ProgressBar上加文字----显示百分比的进度条
- 在ProgressBar上加文字----显示百分比的进度条
- 在ProgressBar上加文字----显示百分比的进度条
- 在ProgressBar上加文字----显示百分比的进度条
- Hashtable和HashMap的区别
- HMM学习笔记2
- java.lang.IllegalStateException: The content of the adapter has changed but
- 关于lnmp如何删除.user.ini文件
- MySQL插入百万条测试数据
- ProgressBar颜色设定及绘制文字显示
- [Leetcode] 4Sum
- 利用原生Js方法实现发送验证码倒计时
- <精灵萌宠总动>隐私政策
- Android 验证码倒计时
- 类别,属性,成员变量,arc学习总结
- CodeForces 148D Bag of mice [概率DP]
- 跳转到指定的viewcontroller
- IIR无限冲击响应滤波器设计