在拖放期间重绘

Gre*_*fey 7 .net c# drag-and-drop

我想在DragEnter和DragLeave事件期间触发拖动目标控件以重绘自身(通过无效或刷新).代码看起来像:

protected override void OnDragEnter (DragEventArgs drgargs)
{
  //-- Set a property that affects drawing of control, then redraw
  this.MyProperty = true;
  this.Refresh(); //-- Does nothing???      
}

protected override void OnDragLeave (EventArgs e)
{
  //-- Set a property that affects drawing of control, then redraw
  this.MyProperty = false;
  this.Refresh(); //-- Does nothing???      
}
Run Code Online (Sandbox Code Playgroud)

它实际上不会重绘控件,这些事件中的Refresh()不会调用OnPaint方法.有没有办法做到这一点?我不能在这里理解一些东西.

更新: jasonh提供的答案实际上并不起作用.使用Invalidate()或Invalidate(rect)时,控件实际上不会更新.这是拖放操作期间进行的调用.还有其他想法吗?拖放过程中可以触发重绘控件吗?谢谢!

更新2:我创建了一个示例项目,但无法使其无效.叹了口气......我终于将它追溯到导致问题的OnPaint中的一些代码.所以,事实证明,我更多的是不了解调试器是如何工作的(它从未在OnPaint中遇到断点......仍然不知道为什么).Invalidate(),Refresh()都有效.JasonH得到答案,因为它最终是正确的,并且还展示了如何使控件的一部分无效......我不知道这一点.

感谢你的帮助!

jas*_*onh 4

调用this.Invalidate()以让窗体/控件重绘自身。如果您知道特定区域,则调用其中一个重载方法来指定要无效的内容。例如:

Rectangle toInvalidate = new Rectangle(drgargs.X - 50, drgargs.Y - 50, 50, 50);
this.Invalidate(toInvalidate);
Run Code Online (Sandbox Code Playgroud)

这将使拖动目标周围 50 像素的区域无效。

  • 这不起作用。问题是专门关于在拖放事件期间执行此操作的。如果它对你有用,那一定是我做错了什么......<confused>...... (2认同)