WinForms设计图面上控件的BeginResize/EndResize事件 [英] BeginResize/EndResize Event for Control on WinForms Design Surface

查看:46
本文介绍了WinForms设计图面上控件的BeginResize/EndResize事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:我想知道如何为设计器图面上的设计时控件实例创建一个指向begin-resize和end-resize事件的钩子.

TLDR: I would like to know how I can create a hook into a begin-resize and an end-resize event for a design-time control instance on the designer surface.

详细信息:特别是,我正在使用System.Design和System.Component.Design .NET命名空间中由BasicLoader生成的设计图.具体来说,我正在设计TableLayoutPanel的设计时实例.该控件公开了一个 SizeChanged 事件和一个 Resize 事件-哎呀,这两个事件都在 调整操作期间(即 while)触发调整控件的大小以及完成调整大小操作的时间.因此,我无法确定调整大小操作何时开始以及何时正式结束.

Detail: Specifically, I am working with a design surface produced by a BasicLoader in the System.Design and System.Component.Design .NET namespaces. Specifically, I'm working a design-time instance of the TableLayoutPanel. That control exposes a SizeChanged event and a Resize event--alas, both fire during the resize operation--that is, while the control is being resized--as well as when the resize operation is complete. I therefore have no way of know when the resize operation began and when it officially ended.

解决此问题的一种方法是检测鼠标按下事件和调整大小事件-但是我不清楚如何在任何抓柄上检测到鼠标按下事件 调整大小的控件.

One way to tackle this would be to detect a mouse-down event along with a resize event--but it's unclear to me how I can detect a mouse-down event on any of the grab handles of a control being resized.

出于记录目的,我重新访问了BehaviorService,并发现它公开了 BeginDrag EndDrag 同步-我什么都没看到可以帮助我解决BeginResize/EndResize事件的服务.

For the records, I revisited the BehaviorService and saw that it exposes BeginDrag, EndDrag, and Synchronize--I see nothing in that service that would help me with BeginResize/EndResize events.

因此,理想情况下,我想为Winform控件的任何设计器实例订阅BeginResize/EndResize事件,但是如果提供的答案仅满足我将这些事件附加到设计器实例的需要,我将感到高兴.TableLayoutPanel控件...

So, ideally, I would like to subscribe to BeginResize/EndResize events for any designer instance of a Winform control, but I would be happy if the provided answer covered only my need to have these events attached to a designer instance of the TableLayoutPanel control...

有什么想法吗?

推荐答案

开始调整大小时,将启动具有特定说明的设计器事务,而当设计结束时,将关闭该事务.

When a resize starts, a designer transaction with a specific description starts and when the design ends, the transaction will be closed.

您可以依赖 IDesignerHost TransactionOpened 事件,并检查 TransactionDescription ,看看它是否以"Resize" ,将标志 resizing 设置为 true .然后在 TransactionClosed 中,您可以检查该标志是否为 true ,这意味着它发生了调整大小的结束.

You can rely on TransactionOpened event of the IDesignerHost and check the TransactionDescription to see if it starts with "Resize", set a flag resizing to true. Then in TransactionClosed you can check if the flag is true, it means it's a resize end has happened.

示例

这是一个 PoC ,以显示其工作原理.将以下控件添加到Windows Forms项目中,并在构建项目后,将 MyControl 的实例拖放到窗体上.然后,如果您尝试调整表单的大小,您将在表单的标题栏上看到开始调整大小.调整大小结束.文本:

Here is a PoC to show how it works. Add the following control into a Windows Forms project and after building the project, drop an instance of MyControl on the form. Then if you try to resize the form, you will see the Resize started. and Resize ended. text on title-bar of the form:

这是代码:

using System;
using System.ComponentModel.Design;
using System.Windows.Forms;
public class MyControl : Control
{
    IDesignerHost host;
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        if(DesignMode)
        {
            host = (IDesignerHost)Site.GetService(typeof(IDesignerHost));
            host.TransactionOpened += Host_TransactionOpened;
            host.TransactionClosed += Host_TransactionClosed;
        }
    }
    bool resizing = false;
    private void Host_TransactionOpened(object sender, EventArgs e)
    {
        if (host?.TransactionDescription?.StartsWith("Resize") == true)
        {
            resizing = true;
            ((Control)host.RootComponent).Text = "Resize Started.";
        }
    }
    private void Host_TransactionClosed(object sender,
        DesignerTransactionCloseEventArgs e)
    {
        if (resizing)
        {
            resizing = false;
            ((Control)host.RootComponent).Text = "Resize ended.";
        }
    }
}

如果要在使用此解决方案之前进行一些研发,则可能需要查看 System.Design 程序集中的以下类(主要是内部类): GrabHandleGlyph ResizeBehavior .

If you want to do some R&D before going with this solution, you may want to take a look at the following classes (mostly internal) in System.Design assembly: GrabHandleGlyph, ResizeBehavior.

这篇关于WinForms设计图面上控件的BeginResize/EndResize事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆