Question:

C#, I need Help, please help me?

by  |  earlier

0 LIKES UnLike

this code draw a square and after drag it, but when I drag the object, there is a strange effect, a flicker effect, can you help me to remove it???

private int x;

private int y;

private int xscale = 150;

private int yscale = 150;

private void Aggiorna(Color colore)

{

Graphics d = panel1.CreateGraphics();

d.Clear(Color.White);

d.FillRectangle(new SolidBrush(colore), new Rectangle(x, y, xscale, yscale));

d.Dispose();

this.Invalidate(new Rectangle(x, y, xscale, yscale), false);

}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)

{

x = Convert.ToInt32(numericUpDown1.Value);

Aggiorna(Color.Red);

}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)

{

y = Convert.ToInt32(numericUpDown2.Value);

Aggiorna(Color.Red);

}

private void panel1_MouseMove(object sender, MouseEventArgs e)

{

if (MouseButtons == MouseButtons.Left)

{

numericUpDown1.Value = MousePosition.X - (150 / 2);

numericUpDown2.Value = MousePosition.Y - 20 - (150 / 2);

}

}

HEEEEEEEEEEELPPPP!!!!

 Tags:

   Report

1 ANSWERS


  1. Hi.

    Just a little disclaimer first:

    I'm not a C# guru (yet :-)), but have done (& taught) graphic programming for Windows using both C++ and Java. Same principles, but please take my answer with a grain of salt. The main thing is for it to make you think.

    Flickering is not that strange when you do animation. It comes from refreshing the graphics. So: the usual trick is to re-paint as little as possible. The more often you re-paint, and the larger surface, the more flicker.

    In case of your program, what meets my naked eye is that, in the Aggiorna function, you clear the entire panel for each mouse movement. I may be wrong, but, in worst case, this may even be happening twice - once per every change *both* along the X and the Y axis. I mean the ..._ValueChanged functions being called implicitly from your ..._MouseMove function - and each of these functions calls Aggiorna.

    You might consider recording both the new co-ordinates, then clearing just the area corresponding to the current position of the square before painting it at the new one. This should make the movement smoother.

    Also, I think you might save some millisecs on each call of Aggiorna by creating the Graphics object outside the function.

    And the last thing: you might make your code more maintainable and extensible (not to say beautiful) by avoiding the use of magic constants (150) . You've already defined the symbolic constants you could use instead.

    Good luck - and please let me know whether any of my suggestions were useful.

    Kind regards,

    Henryk

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions