繁体   English   中英

Form.FormClosing() 上的 Form.Width 不正确

[英]Form.Width incorrect on Form.FormClosing()

我试图在 FormClosing() 上获得准确的 Form.Width。

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

如果我通过向左拖动窗体的边缘来重新调整 Windows 界面中窗体的大小,宽度会减小得很好。 FormClosing 准确地报告重新调整大小的宽度。 比方说1107。

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

我无法处理这种行为。 我需要 FormClosing 来报告正确的大小。 我认为 FormClosing 在触发时将 Width 设置为 MaximumWidth。 我想我读了一些关于如何在 FormClosing 事件被触发时将 Form 设置为 Hidden 的内容。 也许被隐藏会将其大小恢复为 MaxWidth。

任何帮助将非常感激。 我已经为此苦苦挣扎了几个小时,但找不到任何东西。

谢谢。

这是相关的代码:

 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);            
    }

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

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

编辑 2:FormClosing() 还将 Horizo​​ntalScroll.Value 和 VerticalScroll.Value 重新设置为 0。这太糟糕了! 我想知道如何解决这个问题。 我也看过e.Cancel,但似乎也没有办法利用它。 似乎 e.Cancel 只是 re-shows() 处于未隐藏状态的表单。

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

在 FormClosing 事件中使用:

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

您正在使用表单类的另一个实例,Form.Show() 在此事件中不应该工作,因为 Dispose 函数被调用,如果您引用了同一个实例,它也将被处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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