I am trying to write a program where multiple instances of a class will draw themselves onto a PictureBox. (If there is a better control to use then that would be useful to know!). The paint method in the main form looks like :
public
virtual void pbWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e){...
The override in the class of which multiple instances will occur is :
public override void pbWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
System.Drawing.Pen pen = new Pen(System.Drawing.Color.Black, 1);
g.DrawRectangle(pen, posX, posY, 2, 2);
}
What I need the program to do is, everytime a new instance is created, update the main PictureBox with the new object, and then everytime the position of that object changes, update the area around the object (so as not to redraw the whole screen). I assume that I need to create a specific event for this and link it to the overridden paint method?!
I hope I have included enough info here - if not, just let me know what else you need to know to help me out on this one!
Many thanks,
Bertie.
Chris BertrandPosted May 4, 2007, 4:47 AM
The object is in it's own class under the same namespace. It has a method to update it's position, and what I want to be able to do, is when that position is altered, fire a paint event from within the object's move method. (And also in it's constructor on creation).
My thought was to create an event handler and event args and set it up that way. But when I try and do this, it complains that the eventhandler doesn't have the right parameters - because I'm using my own eventargs and not System.Windows.Forms.PaintEventArgs. So, this is roughly how it looks :
PaintCode.cs -
public
class ObjectPaintEventArgs : EventArgs{
int left;
int top;
int right;
int bottom; public ObjectPaintEventArgs(Rectangle r, System.Windows.Forms.PaintEventArgs e)
{
left = r.Left;
top = r.Top;
right = r.Right;
bottom = r.Bottom;
}
} public delegate void ObjectPaintEventHandler(object sender, ObjectPaintEventArgs e);
agent.cs -
public
event ObjectPaintEventHandler paintAgent;public
Agent(int x, int y, int village){...
ObjectPaintEventArgs paintAgentEventArgs =
new ObjectPaintEventArgs(new Rectangle(x, y, 2, 2));paintAgent += new ObjectPaintEventHandler(MainForm_Paint);
paintAgent(this, paintAgentEventArgs);
...
}
public
override void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e){...
}
Mike GoldPosted May 3, 2007, 7:28 PM