Smooth movement of icon displayed on a Panel

↘锁芯ラ 提交于 2020-01-06 05:38:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!