c# - Moving Object with Paint Event -
i have test tomorrow, , must use paint event redraw our objects, may not use timer.
as msdn says: "the paint event raised when control redrawn.", that,occurs known, when form minimized, or got invisible , visible.
my code:
public partial class form1 : form { public graphics drawarea; public int xpos, ypos; public form1() { initializecomponent(); } private void form1_paint(object sender, painteventargs e) { drawarea = e.graphics; drawuser(); } private void form1_keydown(object sender, keyeventargs e) { switch (e.keycode) { case keys.down: ypos++; break; case keys.up: ypos--; break; case keys.left: xpos--; break; case keys.right: xpos++; break; } } private void drawuser() { drawarea.fillrectangle(new solidbrush(color.red), xpos, ypos, 50, 50); } }
so, when press key arrows multiple times, object moves after re-size form. want move instantly, using paint event.
thanks
i found it!
by adding this.invalidate();
after key pressed. tell paint event redraw.
Comments
Post a Comment