我想利用C#本身产生的Form1,Form1上放一个按钮btnDrawRed,
鼠标单击此按钮后,会调用产生事件的函数OnFormDraw,OnFormDraw会产生一个事件FormDraw,
把此事件和Graphics传入自制的画图类MyClass,画图类得到Graphics后,在Form1上画一个矩形。
不知能不能实现。下面是我的程序,还有一处编译出错,已经标明,我也不知该怎么改,我在自己写的语句旁都加了注释。
谢谢大家!我实在是不会了!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace UseClass
{
public partial class Form1 : Form
{ public event EventHandler FormDraw;//定义事件
MyClass mClass = new MyClass(this);//传入Form1对象,产生自己的画图类对象,此处编译出错,好象不能用this,可我怎么传Form1对象啊?
PaintEventArgs p;
//EventArgs p;
public void OnFormDraw(PaintEventArgs e)//产生事件的函数
{
if( FormDraw != null)
FormDraw(this, e);//产生事件
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void btnDrawRed_Click(object sender, EventArgs e)
{
OnFormDraw(p);//调用产生事件的函数
}
}
}
namespace UseClass
{
class MyClass
{
//Form1 form1 = null;
public MyClass(Form1 F1)
{
F1.FormDraw += new EventHandler(DrawGreen);//画图类的事件处理程序和Form1的事件相关联
// this.form1 = F1;
} public void DrawGreen(object sender, EventArgs e)
{
if (e is PaintEventArgs)
{
PaintEventArgs pp = e as PaintEventArgs;
Graphics g = pp.Graphics; //产生一个Graphics对象
Pen pe = new Pen(Color.Green, 2);//定义了一个绿色,宽度为的画笔
g.DrawRectangle(pe, 10, 10, 100, 100);//画一个矩形
}
}
}
}
鼠标单击此按钮后,会调用产生事件的函数OnFormDraw,OnFormDraw会产生一个事件FormDraw,
把此事件和Graphics传入自制的画图类MyClass,画图类得到Graphics后,在Form1上画一个矩形。
不知能不能实现。下面是我的程序,还有一处编译出错,已经标明,我也不知该怎么改,我在自己写的语句旁都加了注释。
谢谢大家!我实在是不会了!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace UseClass
{
public partial class Form1 : Form
{ public event EventHandler FormDraw;//定义事件
MyClass mClass = new MyClass(this);//传入Form1对象,产生自己的画图类对象,此处编译出错,好象不能用this,可我怎么传Form1对象啊?
PaintEventArgs p;
//EventArgs p;
public void OnFormDraw(PaintEventArgs e)//产生事件的函数
{
if( FormDraw != null)
FormDraw(this, e);//产生事件
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void btnDrawRed_Click(object sender, EventArgs e)
{
OnFormDraw(p);//调用产生事件的函数
}
}
}
namespace UseClass
{
class MyClass
{
//Form1 form1 = null;
public MyClass(Form1 F1)
{
F1.FormDraw += new EventHandler(DrawGreen);//画图类的事件处理程序和Form1的事件相关联
// this.form1 = F1;
} public void DrawGreen(object sender, EventArgs e)
{
if (e is PaintEventArgs)
{
PaintEventArgs pp = e as PaintEventArgs;
Graphics g = pp.Graphics; //产生一个Graphics对象
Pen pe = new Pen(Color.Green, 2);//定义了一个绿色,宽度为的画笔
g.DrawRectangle(pe, 10, 10, 100, 100);//画一个矩形
}
}
}
}
改为: public MyClass(Form F1)
{
F1.FormDraw += new EventHandler(DrawGreen);//画图类的事件处理程序和Form1的事件相关联
// this.form1 = F1;
}
参数不能用自己窗体的Form1 F1!感觉你的程序好乱啊,画个圆而已,搞这么复杂干什么?
直接点击button画圆不行吗?
这个不是定义在其他dll中的吧?把具体错误发出来啊
public event EventHandler FormDraw;if (FormDraw!=null)
{
FormDraw(this, null);
}Form1:Form 2 f=new Form2();
f.FormDraw+=new EventHandler(this.FormDraw)
...private void FormDraw(object sender, EventArgs e)
{
//
}
参考一下
http://download.csdn.net/source/2794833
public event EventHandler FormDraw;if (FormDraw!=null)
{
FormDraw(this, null);
}
不是放在form2的程序中吗?这段程序如何得到?