Can I have to PaintEventHandlers in my program
-
Hi All, I am trying to draw a rectangle and then draw a circle within the rectangle. At this point I have the rectangle drawing but the circle. The circle within the rectangle is going to move based on motion of the mouse. I am going to try to take the input of the mouse and make those my X and Y coordinates. If anyone has any experience with either have 2 paint event handlers or with having to update the circle depending on location, it would be great! Any sample code and guidance would be greatly appreciated! See below for event handlers. I am developing use C# and Visual Studio 2005.
//In constructor, after intialize component call first Paint Event Handler f1_paint. private static void f1_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen p = new Pen(Color.White, 3); dc.DrawRectangle(p, 478, 10, 487, 352); p.Dispose(); } private static void f2_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen o = new Pen(Color.Orange, 5); dc.DrawEllipse(o, 717, 176, 5, 5); o.Dispose(); } //In timer function will take in mouse input and update circle so need to call f2_paint. this.Paint += new PaintEventHandler(f2_paint);
Thanks, Laura -
Hi All, I am trying to draw a rectangle and then draw a circle within the rectangle. At this point I have the rectangle drawing but the circle. The circle within the rectangle is going to move based on motion of the mouse. I am going to try to take the input of the mouse and make those my X and Y coordinates. If anyone has any experience with either have 2 paint event handlers or with having to update the circle depending on location, it would be great! Any sample code and guidance would be greatly appreciated! See below for event handlers. I am developing use C# and Visual Studio 2005.
//In constructor, after intialize component call first Paint Event Handler f1_paint. private static void f1_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen p = new Pen(Color.White, 3); dc.DrawRectangle(p, 478, 10, 487, 352); p.Dispose(); } private static void f2_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen o = new Pen(Color.Orange, 5); dc.DrawEllipse(o, 717, 176, 5, 5); o.Dispose(); } //In timer function will take in mouse input and update circle so need to call f2_paint. this.Paint += new PaintEventHandler(f2_paint);
Thanks, LauraDon't use a timer. Use the MouseEnter, MouseMove and MouseLeave events to trigger the update the control. Those events give you a MouseEventArgs object that contains the coordinates of the mouse pointer. Store the coordinates in member variables so that you can use them in the paint event. There is no use in having more than one paint event handler for a control. They will just run after each other, so you can just put all the code in the same event handler.
laura1316 wrote:
//In timer function will take in mouse input and update circle so need to call f2_paint. this.Paint += new PaintEventHandler(f2_paint);
That will not call the event handler, it will instead add another event handler to the event. If you do that in a timer event, you soon will have thousands of event handlers in the paint event, each one drawing the same thing on top of the other. When you need the control to be redrawn, just call the Invalidate method of the control. This will cause a paint event.
--- single minded; short sighted; long gone;