简体   繁体   中英

BeginResize/EndResize Event for Control on WinForms Design Surface

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM