我想每次进度条都随着文本框中的数字而动态的现实,我自己写的代码不能实现,感觉不到动态效果,每次产生一个随机数,进度条都重新画一次,我不知道每次重画怎么实现?各位高手帮帮忙吧,这个问题弄了好久了,还是不行?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace wenduji
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random ro = new Random();
textBox1.Text = ro.Next(10, 50).ToString();
Graphics dc = this.CreateGraphics();
this.Show();
Pen p1 = new Pen(Color.Black);
Rectangle rec = new Rectangle(20, 100, 200, 30);
dc.DrawRectangle(p1, rec);
dc.DrawEllipse(p1, 0, 100, 30, 30);
dc.DrawString("o", this.Font, Brushes.Black, 225, 70);
dc.DrawString("C", this.Font, Brushes.Black, 230, 75);
////绘制最短的刻度线
for (int i = 20; i <= 220; i += 4)
{
dc.DrawLine(p1, i, 90, i, 100);
}
for (int i = 20; i <= 220; i += 40)
{
dc.DrawLine(p1, i, 80, i, 100);
//绘制刻度旁边的数字
switch (i)
{
case 20:
dc.DrawString("0", this.Font, Brushes.Red, i - 5, 65);
break;
case 60:
dc.DrawString("10", this.Font, Brushes.Red, i - 5, 65);
break;
case 100:
dc.DrawString("20", this.Font, Brushes.Red, i - 5, 65);
break;
case 140:
dc.DrawString("30", this.Font, Brushes.Red, i - 5, 65);
break;
case 180:
dc.DrawString("40", this.Font, Brushes.Red, i - 5, 65);
break;
case 220:
dc.DrawString("50", this.Font, Brushes.Red, i - 5, 65);
break; } }
//绘制稍长的刻度线
for (int i = 40; i <= 200; i += 40)
{
dc.DrawLine(p1, i, 85, i, 100);
}
SolidBrush b1 = new SolidBrush(Color.Red); //dc.FillRectangle(b1,rec);
float d = float.Parse(textBox1.Text.ToString());
float a = d * 4 ; //温度对应的矩形宽度
dc.FillRectangle(b1, 20, 100, a, 30);
//画一个圆
dc.FillEllipse(b1, 0, 100, 30, 30);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace wenduji
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random ro = new Random();
textBox1.Text = ro.Next(10, 50).ToString();
Graphics dc = this.CreateGraphics();
this.Show();
Pen p1 = new Pen(Color.Black);
Rectangle rec = new Rectangle(20, 100, 200, 30);
dc.DrawRectangle(p1, rec);
dc.DrawEllipse(p1, 0, 100, 30, 30);
dc.DrawString("o", this.Font, Brushes.Black, 225, 70);
dc.DrawString("C", this.Font, Brushes.Black, 230, 75);
////绘制最短的刻度线
for (int i = 20; i <= 220; i += 4)
{
dc.DrawLine(p1, i, 90, i, 100);
}
for (int i = 20; i <= 220; i += 40)
{
dc.DrawLine(p1, i, 80, i, 100);
//绘制刻度旁边的数字
switch (i)
{
case 20:
dc.DrawString("0", this.Font, Brushes.Red, i - 5, 65);
break;
case 60:
dc.DrawString("10", this.Font, Brushes.Red, i - 5, 65);
break;
case 100:
dc.DrawString("20", this.Font, Brushes.Red, i - 5, 65);
break;
case 140:
dc.DrawString("30", this.Font, Brushes.Red, i - 5, 65);
break;
case 180:
dc.DrawString("40", this.Font, Brushes.Red, i - 5, 65);
break;
case 220:
dc.DrawString("50", this.Font, Brushes.Red, i - 5, 65);
break; } }
//绘制稍长的刻度线
for (int i = 40; i <= 200; i += 40)
{
dc.DrawLine(p1, i, 85, i, 100);
}
SolidBrush b1 = new SolidBrush(Color.Red); //dc.FillRectangle(b1,rec);
float d = float.Parse(textBox1.Text.ToString());
float a = d * 4 ; //温度对应的矩形宽度
dc.FillRectangle(b1, 20, 100, a, 30);
//画一个圆
dc.FillEllipse(b1, 0, 100, 30, 30);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "10";
} private void timer1_Tick(object sender, EventArgs e)
{
Random ro = new Random();
textBox1.Text = ro.Next(10, 50).ToString();
this.Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen p1 = new Pen(Color.Black);
Rectangle rec = new Rectangle(20, 100, 200, 30);
dc.DrawRectangle(p1, rec);
dc.DrawEllipse(p1, 0, 100, 30, 30);
dc.DrawString("o", this.Font, Brushes.Black, 225, 70);
dc.DrawString("C", this.Font, Brushes.Black, 230, 75);
////绘制最短的刻度线
for (int i = 20; i <= 220; i += 4)
{
dc.DrawLine(p1, i, 90, i, 100);
}
for (int i = 20; i <= 220; i += 40)
{
dc.DrawLine(p1, i, 80, i, 100);
//绘制刻度旁边的数字
switch (i)
{
case 20:
dc.DrawString("0", this.Font, Brushes.Red, i - 5, 65);
break;
case 60:
dc.DrawString("10", this.Font, Brushes.Red, i - 5, 65);
break;
case 100:
dc.DrawString("20", this.Font, Brushes.Red, i - 5, 65);
break;
case 140:
dc.DrawString("30", this.Font, Brushes.Red, i - 5, 65);
break;
case 180:
dc.DrawString("40", this.Font, Brushes.Red, i - 5, 65);
break;
case 220:
dc.DrawString("50", this.Font, Brushes.Red, i - 5, 65);
break;
}
}
//绘制稍长的刻度线
for (int i = 40; i <= 200; i += 40)
{
dc.DrawLine(p1, i, 85, i, 100);
}
SolidBrush b1 = new SolidBrush(Color.Red); //dc.FillRectangle(b1,rec);
float d = float.Parse(textBox1.Text);
float a = d * 4; //温度对应的矩形宽度
dc.FillRectangle(b1, 20, 100, a, 30);
//画一个圆
dc.FillEllipse(b1, 0, 100, 30, 30);
base.OnPaint(e);
}
}
}
1.添加一个“用户控件”,编写如下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
/*双缓冲以减少闪烁*/
SetStyle(ControlStyles.OptimizedDoubleBuffer|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint, true);
}
private float m_Dvalue = 0f;
/// <summary>
/// 温度对应的矩形宽度
/// </summary>
public float Dvalue
{
get { return m_Dvalue; }
set { m_Dvalue = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen p1 = new Pen(Color.Black);
Rectangle rec = new Rectangle(20, 100, 200, 30);
dc.DrawRectangle(p1, rec);
dc.DrawEllipse(p1, 0, 100, 30, 30);
dc.DrawString("o", this.Font, Brushes.Black, 225, 70);
dc.DrawString("C", this.Font, Brushes.Black, 230, 75);
////绘制最短的刻度线
for (int i = 20; i <= 220; i += 4)
{
dc.DrawLine(p1, i, 90, i, 100);
}
for (int i = 20; i <= 220; i += 40)
{
dc.DrawLine(p1, i, 80, i, 100);
//绘制刻度旁边的数字
switch (i)
{
case 20:
dc.DrawString("0", this.Font, Brushes.Red, i - 5, 65);
break;
case 60:
dc.DrawString("10", this.Font, Brushes.Red, i - 5, 65);
break;
case 100:
dc.DrawString("20", this.Font, Brushes.Red, i - 5, 65);
break;
case 140:
dc.DrawString("30", this.Font, Brushes.Red, i - 5, 65);
break;
case 180:
dc.DrawString("40", this.Font, Brushes.Red, i - 5, 65);
break;
case 220:
dc.DrawString("50", this.Font, Brushes.Red, i - 5, 65);
break;
}
}
//绘制稍长的刻度线
for (int i = 40; i <= 200; i += 40)
{
dc.DrawLine(p1, i, 85, i, 100);
}
SolidBrush b1 = new SolidBrush(Color.Red); //dc.FillRectangle(b1,rec);
float d = m_Dvalue;
float a = d * 4; //温度对应的矩形宽度
dc.FillRectangle(b1, 20, 100, a, 30);
//画一个圆
dc.FillEllipse(b1, 0, 100, 30, 30);
base.OnPaint(e);
}
}
}
2.生成解决方案,这时你会在工具栏的最上方多了一个“UserControl1”的控件,把他拉到窗体上,它的属性栏里有个
“Dvalue”的属性,那个就是上面的温度对应的矩形宽度,你可以在这里直接赋值,也可以编写代码赋值,如下:
private void timer1_Tick(object sender, EventArgs e)
{
Random ro = new Random();
float d = ro.Next(10, 50);
textBox1.Text =d .ToString();
userControl11.Dvalue = d;
userControl11.Refresh();//刷新控件
}
3,运行程序,^_^