我想在private void button1_Click(object sender, EventArgs e)里面调用 private void pictureBox1_Paint(object sender, PaintEventArgs e)将float型的参数a,b,c传递给后者,请问高手如何实现?
解决方案 »
- 我用VS2010开发一个WinForm程序,程序调试没有问题,但是我打包安装以后程序却不能运行
- C#泛型,求每句代码的详细解释
- 两个 datatable 如何合并
- 扫描枪扫描条码(含特殊字符如:◆)的问题
- 高分请人帮忙优化下代码的。。。。[在线等======]
- 求一个正则表达式
- 请教:如何用程序获得Access数据表里的字段信息?
- C# 读取csv文件到sql server 身份证编号变成科学读数法,求解
- 以前的8位的CHAR在C#里怎么表示?(40分!)
- 征募C#游戏爱好者,美工进行游戏项目试开发(报名帖)
- wpf的ObjectDataProvider IsAsynchronous为真时,在读取完后怎样触发某个事件或执行某段代码?
- C#(winform)在控件间切换焦点时发生什么事件?如何取某控件的默认颜色?
顺便问一下5楼,如何扩展PaintEventArgs类型参数,传入pictureBox1_Paint方法。
可以用static来定义(VB里是Shared)
——————————————————————————————————————————————————
可以的。你非要麻烦,也可以的
public XPaintEventArgs:PaintEventArgs
{
public XPaintEventArgs():base()
{}
public float a = 0;
public float b = 0;
public float c = 0;
}
XPaintEventArgs vgs= new XPaintEventArgs();
vgs.a=12;
vgs.b =23;
vgs.c=34;pictureBox1_Paint(sender,vgs);在pictureBox1_Paint里在转换为XPaintEventArgs 类型。就可以获取到了。
用重载就可以了
private void pictureBox1_Paint(object sender,PaintEventArgs e)
{
pictureBox1_Paint(sender,e,0f,0f,0f);
}private void pictureBox1_Paint(object sender,PaintEventArgs e,float a,float b,float c)
{
做你的操作
}
pictureBox1_Paint(null,null,1f,2f,3f)
就可以了。
pictureBox1_Paint(null,null,1f,2f,3f)
方法里是不是使用了第一个或第二个参数?如果用到了,当然会报这个错误了,
你最好把你的代码帖出来看看。
不使用PictureBox控件,而是建一个新的控件。在这个控件中定义这3个参数的属性,然后Override这个控件的OnPaint事件,在这里进行画图。
在主程序中加这个控件,并在button1_Click中将得到的a,b,c值设置到这个控件的3个参数属性,再调用这个控件的Invalidate()方法即可。public class MyPicture: System.Windows.Forms.UserControl
{
public MyPicture()
{
SetStyle(ControlStyles.AllPaintingInWmPaint
|ControlStyles.DoubleBuffer
|ControlStyles.ResizeRedraw
|ControlStyles.UserPaint,true);
}
... //定义3个属性 protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged (e);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
//在这里画图
}
}在主程序的button1_Click中计算出a,b,c值,然后
MyPicture1.属性a=a;
MyPicture1.属性b=b;
MyPicture1.属性x=c;
MyPicture1.Invalidate();
就可以了。这样做,还 有一个好处就是可以在控件改变尺寸时,自动刷新,并且可以减少画图时的闪烁。
float tcp=1;
float udp2=1;
float gbb=1;
float sum=0; private void pictureBox1_Paint(object sender, PaintEventArgs e)
{//绘制饼图
sum = tcp + udp2 + gbb;
tcp = tcp*360 / sum;
udp2 = udp2*360 / sum;
gbb = gbb*360 / sum;
Graphics g;
g = e.Graphics;
g.FillPie(new SolidBrush(Color.Blue), 80, 25, 200, 200, 0, tcp);
Graphics g1;
g1 = e.Graphics;
g1.FillPie(new SolidBrush(Color.Red), 80, 25, 200, 200, tcp, udp2);
Graphics g2;
g2 = e.Graphics;
g2.FillPie(new SolidBrush(Color.Yellow), 80, 25, 200, 200, tcp + udp2, gbb);
g.FillRectangle(new SolidBrush(Color.Blue), 45, 240, 20, 20);
g.FillRectangle(new SolidBrush(Color.Red), 145, 240, 20, 20);
g.FillRectangle(new SolidBrush(Color.Yellow), 245, 240, 20, 20);
System.Drawing.Point myTCP = new System.Drawing.Point(65, 243);
System.Drawing.Point myUDP = new System.Drawing.Point(165, 243);
System.Drawing.Point myGBB = new System.Drawing.Point(265, 243);
g.DrawString("TCP比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myTCP);
g.DrawString("UDP比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myUDP);
g.DrawString("广播比例", new System.Drawing.Font("宋体", 10), Brushes.Black, myGBB);
}
private void button1_Click(object sender, EventArgs e)
{//图象分析按钮
//清空TIP表内的内容
string StrSQLC = "delete from ZL ";
this.oleDbCommand1.CommandText = StrSQLC;
this.oleDbCommand1.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbCommand1.ExecuteNonQuery();
this.oleDbConnection1.Close();
//统计TIP表中的数据并写入ZL表中
// string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ipdata.mdb";
string StrSQL = "insert into ZL(TCP总量,UDP总量,广播总量) select sum(TCP包总量) ,sum(UDP包总量) ,sum(广播包总量) from TIP ";
this.oleDbCommand1.CommandText = StrSQL;
this.oleDbCommand1.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbCommand1.ExecuteNonQuery();
this.oleDbConnection1.Close(); //从ZL表中选择所有记录显示
string ConnString2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ipdata.mdb";
string SQLString = "select * from ZL";
System.Data.OleDb.OleDbConnection OleDBConn1 = new System.Data.OleDb.OleDbConnection(ConnString2);
DataSet DataSet1 = new DataSet();
System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1);
OleDBConn1.Open();
OleDbDataAdapter1.Fill(DataSet1, "ZL");
//设置DataGridView的行高度
DataGridViewRow row = this.dataGridView1.RowTemplate;
row.Height = 15;
row.MinimumHeight = 12;
dataGridView2.DataSource = DataSet1.Tables["ZL"];
tcp = 2;
udp2 = 1;
gbb = 3;
pictureBox1_Paint(null,null);
//清空ZL表内的内容
string StrSQLC2 = "delete from ZL ";
this.oleDbCommand1.CommandText = StrSQLC2;
this.oleDbCommand1.Connection = this.oleDbConnection1;
this.oleDbConnection1.Open();
this.oleDbCommand1.ExecuteNonQuery();
this.oleDbConnection1.Close();
}
pictureBox1.InvaliDate();
pictureBox1.Invalidate();
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 画图_如何传参数到paint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int x = 0, y = 0, x1 = 15, y1 = 15;
private void button1_Click(object sender, EventArgs e)
{
x = 4;
y = 4;
x1 = 100;
y1 = 100;
panel1.Invalidate();
} private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new Pen(Color.Red );
Graphics g = e.Graphics;
g.DrawLine(myPen , x, y, x1, y1); }
}
}