自己做了一个进度条控件,在主线程中OK,在分线程中用Invoke调用之方法,却没有任何绘制反应。
public partial class ProcessBarEx : UserControl
{
public ProcessBarEx()
{
InitializeComponent();
maximum = 100;
minimum = 0;
value = 0;
borderColor = Color.Gray;
barColor = Color.Blue;
}
int maximum;
[Description("获取或设置控件范围的最大值")]
[Category("行为")]
public int Maximum
{
get
{ return maximum; }
set
{
if (value > minimum)
{
maximum = value;
}
}
} int minimum;
[Description("获取或设置控件范围的最小值")]
[Category("行为")]
public int Minimum
{
get
{ return minimum; }
set
{
if (value < maximum)
{
minimum = value;
}
}
} int value;
[Description("获取或设置控件范围的当前值")]
[Category("行为")]
public int Value
{
get
{ return value; }
set
{
if (value >= minimum && value <= maximum)
{
this.value = value;
//this.Invalidate();
//this.Update();
this.Refresh();
}
}
} Color borderColor;
[Description("获取或设置控件边框颜色")]
[Category("外观")]
public Color BorderColor
{
get
{ return borderColor; }
set
{
borderColor = value;
}
} Color barColor;
[Description("获取或设置控件进度条颜色")]
[Category("外观")]
public Color BarColor
{
get
{ return barColor; }
set
{
barColor = value;
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush br = new SolidBrush(this.BackColor);
SolidBrush ba = new SolidBrush(this.barColor); e.Graphics.FillRectangle(br,e.ClipRectangle); e.Graphics.DrawRectangle(System.Drawing.Pens.Gray, e.ClipRectangle.X, e.ClipRectangle.Y,
e.ClipRectangle.Width-32,e.ClipRectangle.Height-1); int w = (this.value - this.minimum) * (this.Width - 32) / this.maximum; //e.Graphics.FillRectangle(ba,1,2, w, this.Height - 4); LinearGradientBrush upBrush = new LinearGradientBrush(
new Point(10, 0),
new Point(10, this.Height / 2),
Color.FromArgb(224, 255, 224),
Color.FromArgb(0, 153, 0)); LinearGradientBrush downBrush = new LinearGradientBrush(
new Point(10, 0),
new Point(10, this.Height / 2),
Color.FromArgb(0, 153, 0),
Color.FromArgb(224, 255, 224)); e.Graphics.FillRectangle(upBrush, 1, 2, w, this.Height / 2 - 2);
e.Graphics.FillRectangle(downBrush, 1, this.Height / 2, w, this.Height / 2 - 2);
int h;
if ((this.Height - this.Font.Size) > 2)
{
h = (int)(this.Height - (int)this.Font.Size) / 2-1;
}
else
{ h = 1; } e.Graphics.DrawString(string.Format("{0:d}%",(this.value - this.minimum) * 100 / this.maximum) ,
this.Font, System.Drawing.Brushes.Black, this.Width - 30,h); br.Dispose();
ba.Dispose();
upBrush.Dispose();
downBrush.Dispose();
}
}//end class
在分线程中调用: //Schedule是ProcessBarEx对象的引用
public delegate void ScheduleHandler(int Schedule); public void ChangeSchedule(int Schedule)
{
this.Schedule.Value = Schedule;
} ScheduleHandler sh = new ScheduleHandler(ChangeSchedule); this.Schedule.Invoke(sh, new object[] { ... });a
主线程中设置Value属性,结果正常。分线程中调用Value属性,毫无反应。我把ProcessBarEx换成系统自带的进度条控件,则一切正常。(但我不想用系统自带的进度条控件) 大家帮我分析下原因,谢谢!
public partial class ProcessBarEx : UserControl
{
public ProcessBarEx()
{
InitializeComponent();
maximum = 100;
minimum = 0;
value = 0;
borderColor = Color.Gray;
barColor = Color.Blue;
}
int maximum;
[Description("获取或设置控件范围的最大值")]
[Category("行为")]
public int Maximum
{
get
{ return maximum; }
set
{
if (value > minimum)
{
maximum = value;
}
}
} int minimum;
[Description("获取或设置控件范围的最小值")]
[Category("行为")]
public int Minimum
{
get
{ return minimum; }
set
{
if (value < maximum)
{
minimum = value;
}
}
} int value;
[Description("获取或设置控件范围的当前值")]
[Category("行为")]
public int Value
{
get
{ return value; }
set
{
if (value >= minimum && value <= maximum)
{
this.value = value;
//this.Invalidate();
//this.Update();
this.Refresh();
}
}
} Color borderColor;
[Description("获取或设置控件边框颜色")]
[Category("外观")]
public Color BorderColor
{
get
{ return borderColor; }
set
{
borderColor = value;
}
} Color barColor;
[Description("获取或设置控件进度条颜色")]
[Category("外观")]
public Color BarColor
{
get
{ return barColor; }
set
{
barColor = value;
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush br = new SolidBrush(this.BackColor);
SolidBrush ba = new SolidBrush(this.barColor); e.Graphics.FillRectangle(br,e.ClipRectangle); e.Graphics.DrawRectangle(System.Drawing.Pens.Gray, e.ClipRectangle.X, e.ClipRectangle.Y,
e.ClipRectangle.Width-32,e.ClipRectangle.Height-1); int w = (this.value - this.minimum) * (this.Width - 32) / this.maximum; //e.Graphics.FillRectangle(ba,1,2, w, this.Height - 4); LinearGradientBrush upBrush = new LinearGradientBrush(
new Point(10, 0),
new Point(10, this.Height / 2),
Color.FromArgb(224, 255, 224),
Color.FromArgb(0, 153, 0)); LinearGradientBrush downBrush = new LinearGradientBrush(
new Point(10, 0),
new Point(10, this.Height / 2),
Color.FromArgb(0, 153, 0),
Color.FromArgb(224, 255, 224)); e.Graphics.FillRectangle(upBrush, 1, 2, w, this.Height / 2 - 2);
e.Graphics.FillRectangle(downBrush, 1, this.Height / 2, w, this.Height / 2 - 2);
int h;
if ((this.Height - this.Font.Size) > 2)
{
h = (int)(this.Height - (int)this.Font.Size) / 2-1;
}
else
{ h = 1; } e.Graphics.DrawString(string.Format("{0:d}%",(this.value - this.minimum) * 100 / this.maximum) ,
this.Font, System.Drawing.Brushes.Black, this.Width - 30,h); br.Dispose();
ba.Dispose();
upBrush.Dispose();
downBrush.Dispose();
}
}//end class
在分线程中调用: //Schedule是ProcessBarEx对象的引用
public delegate void ScheduleHandler(int Schedule); public void ChangeSchedule(int Schedule)
{
this.Schedule.Value = Schedule;
} ScheduleHandler sh = new ScheduleHandler(ChangeSchedule); this.Schedule.Invoke(sh, new object[] { ... });a
主线程中设置Value属性,结果正常。分线程中调用Value属性,毫无反应。我把ProcessBarEx换成系统自带的进度条控件,则一切正常。(但我不想用系统自带的进度条控件) 大家帮我分析下原因,谢谢!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); //创建一个线程
Thread thread = new Thread(new ThreadStart(BeginThread));
thread.Start();
} public void BeginThread()
{
int i = 0;
while (true)
{
Thread.Sleep(500);
i++;
this.Invoke(new ScheduleHandler(ChangeSchedule), i);
if (i == 100)
i = 0;
}
} public delegate void ScheduleHandler(int Schedule); public void ChangeSchedule(int Schedule)
{
this.Schedule.Value = Schedule; }
}
http://dylike.czdown.com/love1.html
就是这个样子,打开网页后稍等片刻刷新即可看到控件.WINFORM的,但是可以用在网页上的.