问题
I'm coding an app where I display a System.Drawing.Icon object on a System.Windows.Forms.Panel using a code that goes something like so:
Graphics g = _panel.CreateGraphics();
g.DrawIcon(this.NodeIcon, _rectangle);
I have code to move the icon around using drag-n-drop. My problem is that when the user move the icon around, it is anything but smooth. The icon looks distorted until the user stops moving the icon.
I have tried to find information on this around the net but I can't get it to be smooth. I have little previous experience of this particular kind of coding (using graphics) so I am a rookie to this.
If any kind soul could help me with some hints it would be much appreciated.
Thanks in advance!
回答1:
I believe what you're trying to do is to redraw your control on the MouseMove event handler. And looks like your problem is the flicker when redrawing the panel. First what you can try to do is set true to DoubleBuffered property of your panel. Doing this you would set the panel to redraw its surface using a secondary buffer to reduce or prevent flicker. This property is protected so you would need to create a new panel descendant:
public class TestPanel : Panel
{
public TestPanel()
{
DoubleBuffered = true;
}
}
as an alternative you can set the DoubleBuffered property for your panel through reflection
hope this helps, regards
来源:https://stackoverflow.com/questions/1732890/smooth-movement-of-icon-displayed-on-a-panel