簡體   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