Form.Width 在 Form.FormClosesing() 上不正确

本文关键字:Form 不正确 FormClosesing Width | 更新日期: 2023-09-27 18:33:20

我正在尝试在FormClosesing((上获得准确的Form.Width。

我有一个 zoom(( 方法,用于缩放表单中的图片。 然后由我手动调整表单的宽度以适合。 然后,我将最大大小的宽度限制设置为表单的新宽度。 出于我们的目的,假设宽度和最大宽度现在是 1386。

如果我通过在 Windows 界面中通过向左拖动其边缘来调整表单的大小,则宽度会减小得很好。 窗体关闭准确地报告重新调整大小的宽度。 假设 1107。

但是,如果通过我编写的 zoom(( 方法,即使我向左拖动到同一位置,FormClosing 也会报告原始 1386 宽度。 我在 zoom(( 方法中为形成大小所做的唯一事情是设置宽度和最大宽度。

我不知所措地处理这种行为。 我需要表单关闭来报告正确的大小。 我认为FormCloseing在触发时将宽度设置为最大宽度。 我想我读过一些关于在触发 FormClosing 事件时表单如何设置为隐藏的内容。 也许隐藏会将其大小恢复为最大宽度。

任何帮助将不胜感激。 我已经为此挣扎了几个小时,什么也找不到。

谢谢。

以下是相关代码:

 private void zoom()
    {
        //  Re-size form width to slightly more than page width.
        //  This is percentage-based, meaning 100 = default size.
        Size picSize = new Size
        {
            Width = (int)(originalRenderingSize.Width * integerUpDownZoom.Value / 100),
            Height = (int)(originalRenderingSize.Height * integerUpDownZoom.Value / 100),
        };
        // some code here which I commented-out, and still, the problem exists.

        // Set maximum to prevent ugly sizing.
        this.MaximumSize = new Size(picSize.Width + 40, Screen.FromControl(this).WorkingArea.Height);

        // The problem is this:  When this method processes, as shown, a page is rendered and sized according to
        // a user-provided zoom level represented by an integerUpDown control.  Once rendered, the form should
        // re-size its width to match the newly-scaled page's width.  This works, too.  So far, so good.
        //
        // But!!!  If the user grabs the right side of the form and drags it to the left to reduce the width
        // of the form (mind you, this is Windows OS doing this), upon Form.Form_Closing(), you can see a
        // quick flash of the form fully-open and unscrolled.  Then, in the FIRST line of Form_Closing(), 
        // the debugger reports the form's width as the fully-open and UNSCROLLED width.
        //
        //  The goal is to set the width upon the conclusion of this zoom, but then, on closing
        //  get the width of the Form in order to persist it for use the next time the app is run.
        //
        //  2 options have been tried.  Both cause Form_Closing to erroneously report the fully-open width.
        //  But if BOTH are commented-out, the re-sizing occurs and Form_Closing() reports properly the
        //  form's scrolled width.  
        //   Bear in mind that Form_Closing is one of those things that can happen anytime or never.  This means
        //   the bug is triggered by EITHER and ONLY the two options below.
        //  Option 1:  Tried first.  It sets the width fine.
        //  
        //  this.Width = picSize.Width + 40;            
        //  Option 2:  It also sets the width fine.
        //  I think this option causes width to change (it invokes width change, and somewhere in the 
        //  OS's width change, the error occurs.
        //this.MinimumSize = new Size(MaximumSize.Width - 1, this.Height);
        //this.MinimumSize = new Size(0, 0);            
    }

编辑:我有新的信息应该有帮助。 罪魁祸首是 FormClosesing((。 以下是我所做的:我在 Windows 中重新调整了窗体的大小,直到它占据了大约 500 像素的宽度。 显示面板上的水平滚动条。 然后,在 FormClosing 事件中,我添加了行:Form.Show((。 之后,我放了一个MessageBox.Show("Hi."(。 果然,Show(( 导致表单以其完整的、未滚动的宽度重新绘制。 它没有保持其滚动状态。

我想我有解决方案,并在尝试后会回发。也就是说,我需要检查滚动值并对这些值进行操作。

编辑 2:FormClosesing(( 还将 HorizontalScroll.Value 和 VerticalScroll.Value 重新设置为 0。 这真是无赖! 我想知道如何解决这个问题。 我也看了e.Cancel,但似乎也没有办法利用它。 似乎e.Cancel只是重新显示((表单处于未隐藏状态。

抱歉,我无法发布代码。 反正也没意义。 想想吧。 Windows 操作系统处理大小调整。 不是我。 当用户重新调整窗体大小时,所有内容都在 Windows 上。 如果用户将表单大小调整为 500 像素宽,但 FormClosesing(( 报告它是 1386 像素宽,那么我的编码也不会向我们显示任何新内容。 这是一个行为问题。 没有代码可以显示 Windows 操作系统如何处理窗体大小调整。

Form.Width 在 Form.FormClosesing() 上不正确

在表单关闭事件中使用:

MessageBox.Show(this.Width.ToString());

您正在使用表单类的另一个实例,Form.Show(( 在这种情况下应该不起作用,因为调用了 Dispose 函数,如果您引用了相同的实例,它也将被释放。