BeginResize/EndResize Event for Control on WinForms Design Surface

一曲冷凌霜 提交于 2020-05-27 09:26:30

问题


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.


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.

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.

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...

Any thoughts?


回答1:


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

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.

Example

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:

Here is the code:

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.";
        }
    }
}

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.



来源:https://stackoverflow.com/questions/59943637/beginresize-endresize-event-for-control-on-winforms-design-surface

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