寻求Winforms System.ComponentModel.Design设计图的BeginDrag/EndDrag事件挂钩 [英] Seeking a Winforms System.ComponentModel.Design Design Surface BeginDrag/EndDrag Event Hook
问题描述
我正在使用C#,Winforms,VS 2017 Enterprise和完整的.NET Framework 4.7.2.
I am using C#, Winforms, VS 2017 Enterprise, and the full .NET Framework 4.7.2.
[TLDR部分结尾!]
[TLDR section at end!]
我一直在使用System.ComponentModel.Design
命名空间进行广泛的工作,以创建类似于Visual Studio的Winforms设计器.为这样的最终用户表单设计者设置环境需要深入了解上述命名空间中的对象和接口(以及如何将它们全部连线)以及工具箱和属性网格组件.因此,我不可能发布代码示例.这个问题需要DesignSurface
类的知识( https://docs.microsoft.com/zh-cn/dotnet/api/system.componentmodel.design.designsurface?view=netframework-4.7.2 ).
I have been working extensively with the System.ComponentModel.Design
namespace to create working Visual Studio-like Winforms designer. Setting up the environment for such an end-user forms designer requires deep understanding of objects and interfaces in the aforementioned namespace (and how to wire-up them all) along with toolbox and property grid components. Therefore, it's not possible for me to post a sample of the code. This question requires knowledge of the DesignSurface
class (https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.design.designsurface?view=netframework-4.7.2).
当用户在设计器图面上拖动选定的控件时,设计图图似乎不会触发任何事件.我需要加入开始拖动"/结束拖动"事件,以便可以执行清除操作,然后重新渲染一个反映所有控件当前位置的信息面板. (我不想使用计时器来间歇性地刷新该信息.)
The design surface seems to fire no event when a user drags a selected control across the designer surface. I need to hook into "begin-drag"/"end-drag" events so that I can perform clear and then re-render an information panel that reflects the current position of all controls. (I do not want to use timers to intermittently refresh that information.)
有一个我已经实现的ISelectionService
接口.但这仅提供有关选择了哪些控件/组件的信息.它无助于捕获控件拖动操作开始或结束时触发的事件.
There is an ISelectionService
interface, which I've implemented. But that gives information only about which controls/components are selected. It doesn't help capture the event that fires when a control-drag operation begins or ends.
设计图面事件的详细信息显示在此处: https://docs. microsoft.com/en-us/dotnet/api/system.componentmodel.design?view=netframework-4.7.2
Details of design-surface events appear here: https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.design?view=netframework-4.7.2
我尝试利用IComponentChangeService's
ComponentChanged
事件,但这仅在控制拖动操作结束后才触发(并且我需要检测控制拖动操作何时开始和结束)...
I have tried to leverage IComponentChangeService's
ComponentChanged
event, but that fires only after a control-drag operation ends (and I need to detect when the control-drag operation begins and ends)...
作为最后的手段,我使用Spy ++来查看在选择控件并将其拖动到设计图面上时触发了哪些事件. Spy ++帮助我识别了最初的WM.LBUTTONDOWN
消息以及各种鼠标移动消息等,但是利用这些消息将需要大量额外的编码,以确保在设计器表面控件上单击了鼠标按钮,该控件是实际上已选中,并且鼠标按钮仍处于按下状态,依此类推-即便如此,我仍然无法保证控件的大小不会随移动而改变.当然,理想情况下,我希望了解响应于拖动开始事件的设计器表面的逻辑.
As a last resort, I used Spy++ to see what events fire when a control is selected and dragged across the design surface. Spy++ helped me identify the initial WM.LBUTTONDOWN
message and various mouse-move messages, etc., but leveraging those messages would require a lot of additional coding to ensure that the mouse button was clicked on a designer-surface control, that the control was in fact selected, and that the mouse button remains down, etc.--and even then, I still would have no assurance that the control isn't being resized versus moved. Of course, ideally, I would want to hook into the designer-surface's logic that responds to drag-begin event.
最后,我的要求是检测何时拖动单个选定控件或何时拖动多个选定控件为一个组.在这两种情况下,我都需要知道拖动何时开始以及何时结束. (要明确:我指的是设计图面上已经存在的控件,不是指工具箱上拖放到设计器图面上的控件...)
Finally, my requirement is to detect when a single selected control is dragged or when multiple-selected controls are dragged as a group. In both cases, I need to know when the drag starts and when it ends. (To be clear: I'm referring to controls that already are on the design surface--I am not referring to controls on the toolbox that are drag-dropped onto the designer surface...)
TLDR: 我正在寻找的是一种方法,可以将拖动到设计器图面上已经存在的控件或一组控件被拖动时立即触发的事件,以及一种将其拖动到具有拖动操作的事件中的方法结束了.
TLDR: What I'm seeking is a way to hook into the event that fires as soon as a control or a group of controls already on a designer surface is/are dragged, and a way to hook into the event that fires when that drag operation has ended.
有什么想法吗?
推荐答案
您可以获取
示例 您首先需要获取 You first need to get an instance of 然后订阅事件: 并处理事件: 这篇关于寻求Winforms System.ComponentModel.Design设计图的BeginDrag/EndDrag事件挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!BeginDrag
:在BehaviorService
开始拖放操作时发生.EndDrag
:在BehaviorService
完成拖动操作时发生.
BeginDrag
: Occurs when the BehaviorService
starts a drag-and-drop operation.EndDrag
: Occurs when the BehaviorService
completes a drag operation.BehaviorService
的实例,例如,如果您有权访问设计者,设计者主机或网站,则可以通过以下方式获取行为服务: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));
behaviorSvc.BeginDrag += BehaviorSvc_BeginDrag;
behaviorSvc.EndDrag += BehaviorSvc_EndDrag;
private void BehaviorSvc_EndDrag(object sender, BehaviorDragDropEventArgs e)
{
}
private void BehaviorSvc_BeginDrag(object sender, BehaviorDragDropEventArgs e)
{
}