moving ellipse
-
Hello everybody. I am working with MVS2005.My project is C# project How can I make e table with 10 raws and 10 columns and draw a moving ellipse over table. Thanks for all.
-
Hello everybody. I am working with MVS2005.My project is C# project How can I make e table with 10 raws and 10 columns and draw a moving ellipse over table. Thanks for all.
Hi, I am not sure what you mean by table. If its just a visual thing, you might consider using a Panel, and drawing in it: - 9 or 11 horizontal lines - 9 or 11 vertical lines - your ellipse (redrawn based on a Forms.Timer for animation) :)
Luc Pattyn [My Articles]
-
Hi, I am not sure what you mean by table. If its just a visual thing, you might consider using a Panel, and drawing in it: - 9 or 11 horizontal lines - 9 or 11 vertical lines - your ellipse (redrawn based on a Forms.Timer for animation) :)
Luc Pattyn [My Articles]
Now I do this. private void timer1_Tick(object sender, EventArgs e) { X1 += 5; Y1 += 5; Invalidate(); SolidBrush myBrush = new SolidBrush(Color.Green); dataGridView1.Refresh(); Graphics grfx = dataGridView1.CreateGraphics(); grfx.FillEllipse(myBrush, X1, Y1, 15, 15); } But I ask is there any other way to do this
-
Now I do this. private void timer1_Tick(object sender, EventArgs e) { X1 += 5; Y1 += 5; Invalidate(); SolidBrush myBrush = new SolidBrush(Color.Green); dataGridView1.Refresh(); Graphics grfx = dataGridView1.CreateGraphics(); grfx.FillEllipse(myBrush, X1, Y1, 15, 15); } But I ask is there any other way to do this
Hi, some remarks: 1. you should call Dispose() on all objects you create from classes that offer a public Dispose() method as soon as you dont need the objects any more. It applies to both myBrush and grfx. 2. you could keep myBrush alive and reuse it over and over. you should not do that with grfx 3. I would do the painting in a paint handler, not the timer tick (but of course the x1/y1 update and the Invalidate() belong inside the tick handler). AS a result the ellipse gets redrawn also every time it got damaged (e.g. when moving another window over it without having to wait for the next timer tick (dont know what its period is). BTW The PaintEventArgs offer you a free graphics, so you then dont need CreateGraphics anymore, and dont have to Dispose() it. :)
Luc Pattyn [My Articles]
-
Hi, some remarks: 1. you should call Dispose() on all objects you create from classes that offer a public Dispose() method as soon as you dont need the objects any more. It applies to both myBrush and grfx. 2. you could keep myBrush alive and reuse it over and over. you should not do that with grfx 3. I would do the painting in a paint handler, not the timer tick (but of course the x1/y1 update and the Invalidate() belong inside the tick handler). AS a result the ellipse gets redrawn also every time it got damaged (e.g. when moving another window over it without having to wait for the next timer tick (dont know what its period is). BTW The PaintEventArgs offer you a free graphics, so you then dont need CreateGraphics anymore, and dont have to Dispose() it. :)
Luc Pattyn [My Articles]
Hi Can you send me a sample code how can I do this in paint handler X and Y is updated in timer tick at 100ms.
-
Hi Can you send me a sample code how can I do this in paint handler X and Y is updated in timer tick at 100ms.
Hi, create and wire a paint handler for your dataGridView1 or add the relevant lines to an existing one:
private SolidBrush myBrush = new SolidBrush(Color.Green);
private void dataGridView1_Paint(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
g.FillEllipse(myBrush, X1, Y1, 15, 15);
}That should do it. :)
Luc Pattyn [My Articles]
-
Hi, create and wire a paint handler for your dataGridView1 or add the relevant lines to an existing one:
private SolidBrush myBrush = new SolidBrush(Color.Green);
private void dataGridView1_Paint(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
g.FillEllipse(myBrush, X1, Y1, 15, 15);
}That should do it. :)
Luc Pattyn [My Articles]
I'm so sorry but something wrong. I try this any times today. Is there something else in timer tick except to update X and Y value and to Invalidate().Because my dataGridView is repainted only when over it move another window. Sorry but I'm beginer.
-
I'm so sorry but something wrong. I try this any times today. Is there something else in timer tick except to update X and Y value and to Invalidate().Because my dataGridView is repainted only when over it move another window. Sorry but I'm beginer.
Hi, if dataGridView1 is the only thing changed by your timer, then it also is the only thing that needs to be invalidated, so try dataGridView1.Invalidate() there. Dont be afraid to try a couple of things, you learn more by having a good balance between doing some experiments and asking some questions... :)
Luc Pattyn [My Articles]
-
Hi, if dataGridView1 is the only thing changed by your timer, then it also is the only thing that needs to be invalidated, so try dataGridView1.Invalidate() there. Dont be afraid to try a couple of things, you learn more by having a good balance between doing some experiments and asking some questions... :)
Luc Pattyn [My Articles]
It's my last question. :) The effect is the same when in timer tick I make dataGridView1.Refresh(); Byt my dataGridView1 blink every timer tick
-
It's my last question. :) The effect is the same when in timer tick I make dataGridView1.Refresh(); Byt my dataGridView1 blink every timer tick
Hi, that is no surprise, each control that gets redrawn by default starts of with clearing the background, then painting the foreground. There is an easy way to make this invisible, it is known as "double buffering" (which means the drawing is made first in an off-screen buffer, then that one is copied over the relevant part of the screen). There is Control.DoubleBuffered starting .NET 2.0; for .NET 1.x you can achieve the same effect using SetStyles on an inherited Control. Lots of articles on CP use these techniques. :)
Luc Pattyn [My Articles]