WinForms - 调整大小事件后的操作

发布于 2024-09-06 17:37:38 字数 96 浏览 6 评论 0原文

是否可以在(用户控件的)调整大小事件之后执行特定操作,例如释放鼠标按钮时?我需要手动调整内部控件的大小,并且在事件的每次触发时都执行此操作,嗯,效率低下......

Is it possible to perform a specific action after the resize event (of the user control), for example when mouse button is released? I need to manually resize an inner control and doing it on every single firing of the event would be quite, hmm, inefficient...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素年丶 2024-09-13 17:37:38

只需使用 ResizeEnd 事件:

private void Form1_ResizeEnd(object sender, EventArgs e)
{
   // Your code here
}

来自 MSDN:


用户完成调整表单大小,
通常通过拖动其中之一
边框或尺寸控制点位于
表格的右下角,
然后释放它。了解更多
有关调整大小的信息
操作。

Just use the ResizeEnd event:

private void Form1_ResizeEnd(object sender, EventArgs e)
{
   // Your code here
}

From MSDN:

The ResizeEnd event is raised when the
user finishes resizing a form,
typically by dragging one of the
borders or the sizing grip located on
the lower-right corner of the form,
and then releasing it. For more
information about the resizing
operation.

一刻暧昧 2024-09-13 17:37:38

您可以像这样伪造本地 ResizeEnd:

public class Dummy:UserControl
{

    private readonly Timer _tDelayedResize;

    public Dummy()
    {
        this.Resize += this_Resize;
        _tDelayedResize = new Timer();
        _tDelayedResize.Interval = 5;
        _tDelayedResize.Tick += this_ResizeEnd;
    }

    void this_Resize(object sender, EventArgs e)
    {
        _tDelayedResize.Stop();
        _tDelayedResize.Start();
    }

    void this_ResizeEnd(object sender, EventArgs e)
    {
        _tDelayedResize.Stop();

        //Do your ResizeEnd logic here
        //...
    }

}

可以修改间隔。它越高,最后一次调整大小事件之后的延迟就越多。

You can fake a local ResizeEnd like this:

public class Dummy:UserControl
{

    private readonly Timer _tDelayedResize;

    public Dummy()
    {
        this.Resize += this_Resize;
        _tDelayedResize = new Timer();
        _tDelayedResize.Interval = 5;
        _tDelayedResize.Tick += this_ResizeEnd;
    }

    void this_Resize(object sender, EventArgs e)
    {
        _tDelayedResize.Stop();
        _tDelayedResize.Start();
    }

    void this_ResizeEnd(object sender, EventArgs e)
    {
        _tDelayedResize.Stop();

        //Do your ResizeEnd logic here
        //...
    }

}

The interval can be modified. The higher it is the more delay after the last resize event it will be.

不知在何时 2024-09-13 17:37:38

一个简单的解决方案是重写表单中的 OnResize 并阻止它调用基本方法。

请在 OnResizeEnd 阶段开始时调用此基本方法。

        protected override void OnResize(EventArgs e)
        {
            // base.OnResize(e);
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResize(e);
            base.OnResizeEnd(e);
        }

最大化或恢复的表单只能在 OnResize 中检测到。

为了适应这种情况,您需要跟踪窗口状态并允许 OnResize 在其更改时继续进行:

        private FormWindowState _LastWindowState;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _LastWindowState = WindowState;
        }

        protected override void OnResize(EventArgs e)
        {
            if (WindowState != _LastWindowState)
            {
                base.OnResize(e);
                _LastWindowState = WindowState;
            }
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResize(e);
            base.OnResizeEnd(e);
        }

另一方面,使用这样的代码应该是最后的手段。控件自然调整大小的问题通常表明设计/编码不当。

A simple solution is to override the OnResize in the form and prevent it calling the base method.

Call this base method at the start of the OnResizeEnd stage instead.

        protected override void OnResize(EventArgs e)
        {
            // base.OnResize(e);
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResize(e);
            base.OnResizeEnd(e);
        }

Forms being maximised or restored can only be detected in the OnResize.

To accommodate this, you need to track the window state and allow the OnResize to go ahead when it changes:

        private FormWindowState _LastWindowState;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _LastWindowState = WindowState;
        }

        protected override void OnResize(EventArgs e)
        {
            if (WindowState != _LastWindowState)
            {
                base.OnResize(e);
                _LastWindowState = WindowState;
            }
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResize(e);
            base.OnResizeEnd(e);
        }

On another note, using code like this should be a last resort. Problems with controls resizing naturally would usually indicate bad design/coding.

瑾夏年华 2024-09-13 17:37:38

如果您使用的是控件而不是表单,并且希望在调整大小完成后执行操作(用户停止调整控件大小),则还有另一种选择:

private int resizeTimeout = 0;
private Task resizeTask;

public void OnResize()
{
  resizeTimeout = 300; //Reset timeout
  //Only resize on completion. This after resizeTimeout if no interaction.
  if (resizeTask == null || resizeTask.IsCompleted)
  {
    resizeTask = Task.Run(async () =>
    {
      //Sleep until timeout has been reached
      while (resizeTimeout > 0)
      {
        await Task.Delay(100);
        resizeTimeout -= 100;
      }
      ResizeControl();
    });
  }
}

private void ResizeControl()
{
  if (this.InvokeRequired)
  {
    //Call the same method in the context of the main UI thread.
    this.Invoke((MethodInvoker)delegate { ResizeControl(); });
  }
  else
  {
    // Do resize actions here
  }
}

Another option if you are using a control and not a form and would like to perform actions after the resize has been completed (user stopped resizing control):

private int resizeTimeout = 0;
private Task resizeTask;

public void OnResize()
{
  resizeTimeout = 300; //Reset timeout
  //Only resize on completion. This after resizeTimeout if no interaction.
  if (resizeTask == null || resizeTask.IsCompleted)
  {
    resizeTask = Task.Run(async () =>
    {
      //Sleep until timeout has been reached
      while (resizeTimeout > 0)
      {
        await Task.Delay(100);
        resizeTimeout -= 100;
      }
      ResizeControl();
    });
  }
}

private void ResizeControl()
{
  if (this.InvokeRequired)
  {
    //Call the same method in the context of the main UI thread.
    this.Invoke((MethodInvoker)delegate { ResizeControl(); });
  }
  else
  {
    // Do resize actions here
  }
}
醉生梦死 2024-09-13 17:37:38

也许您可以使用 SizeChanged事件。但我不知道在调整大小期间调用它的频率或时间。

Maybe you can use the SizeChanged Event. But i don´t know how often or when it´s called during resizing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文