Seeking a Winforms System.ComponentModel.Design Design Surface BeginDrag/EndDrag Event Hook

瘦欲@ 提交于 2019-12-04 13:02:01
Reza Aghaei

You can get BehaviorService and subscribe to its BeginDrag and EndDrag events.

  • BeginDrag: Occurs when the BehaviorService starts a drag-and-drop operation.
  • EndDrag: Occurs when the BehaviorService completes a drag operation.

Example

You first need to get an instance of BehaviorService, for example if you have access to a designer, or a designer host or a site, you can get the behavior service this way:

var behaviorSvc = (BehaviorService)Site.GetService(typeof(BehaviorService));

Then subscribe to the events:

behaviorSvc.BeginDrag += BehaviorSvc_BeginDrag;
behaviorSvc.EndDrag += BehaviorSvc_EndDrag;

and handle the events:

private void BehaviorSvc_EndDrag(object sender, BehaviorDragDropEventArgs e)
{  
}

private void BehaviorSvc_BeginDrag(object sender, BehaviorDragDropEventArgs e)
{
}

To supplement Reza's fine and correct answer (which I accepted as an answer) on this rather specialized topic:

There many possible/viable ways to wire-up the Winforms designer and its dependent objects (toolbox, property grid, etc.), so the sequence/timing of when particular designer services are added to the design surface's service container will differ. Some services must be explicitly added (such as INameCreationService or IToolboxService); others are added by the framework.

I don't have not yet concluded exactly when the framework adds the BehaviorService to the service container, but I have concluded that this service is added to any working design service (that is, one that lets you drag already-added controls around the design surface using the mouse).

Additionally, many objects in the System.ComponentModel.Design namespace offer access to the collection of available services by exposing the GetService() method. (Reza's answer leverages the Site object's GetService method.) If you find that the GetService(typeof(BehaviorService)) fails to return a valid BehaviorService object, it usually means that the BehaviorService hasn't yet been added to the design surface... I say "yet", because dragging controls already placed on the designer surface requires the presence of a BehaviorService in the host's service container. So if your attempt to get an instance of the BehaviorService using the GetService method fails, chances are that the BehaviorService simply has not yet been added by the designer framework. To solve that problem, you'll usually just need to move your code for hooking BehaviorService events somewhere downstream.

To inspect which services already have been added to the design surface, you can inspect the design surface object's ServiceContainer object's Services collection, which contains the comprehensive list of services. As Reza suggests, a good place to add the code needed to hook into BehaviorService's events is in the base form's OnHandleCreated event handler.

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