2016-02-04 21 views
-1

我想在FormClosing()上得到一個精確的Form.Width。Form.FormClosing上的Form.Width不正確()

我有一個縮放()方法我用來縮放窗體中的圖片。表格的寬度然後由我手動調整大小以適應。然後,將MaximumSize的寬度限制設置爲表單的新寬度。對於我們的目的,假設寬度和最大寬度現在是1386.

如果我通過將窗體邊緣向左拖動來重新調整窗體大小,寬度就會減小。 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(「嗨。」)。果然,Show()導致窗體以完整的,未滾動的寬度重新繪製。它沒有保持其滾動狀態。

我想我有解決方案,並會嘗試後回發。也就是說,我需要檢查滾動值並對這些值進行操作。

編輯2:FormClosing()也重新設置Horizo​​ntalScroll.Value和VerticalScroll.Value爲0.這是一個無賴!我想知道如何解決這個問題。我也看過e.Cancel,但似乎也沒有辦法利用它。看起來e.Cancel只是在非隱藏狀態下重新顯示()該表單。

對不起,我無法發佈代碼。無論如何,這將毫無意義。想想看。 Windows操作系統處理調整大小。不是我。當用戶重新調整表單大小時,全部都在Windows上。如果用戶將窗體大小重新設置爲500像素寬,但FormClosing()報告寬度爲1386像素,那麼我的編碼量就不會向我們展示任何新內容。這是一個行爲問題。沒有代碼可以顯示Windows操作系統如何處理窗體大小調整。

+1

也許你可以向你展示代碼..從視覺編碼的角度來看,你會更容易看到你在做什麼和不在做什麼 – MethodMan

+0

代碼很好,我相信。我在最後的「編輯」中添加了新的信息。我正在測試這個。寬度。顯然不是滾動狀態下顯示的寬度。現在,我意識到寬度是寬度,而滾動大小可能是將寬度應用於滾動值的組合。 –

+0

如果代碼是好的,那麼你不會有這個問題,這就是爲什麼總是最好發佈代碼,以問問你有什麼問題或問題..也許你想閱讀以下內容[如何提問好問題](http://stackoverflow.com/questions/ask) – MethodMan

回答

0

上的FormClosing事件中使用:

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

您使用您的窗體類的另一個實例,Form.Show()不應該在此事件原因處置功能工作被稱爲,如果你有引用相同它也將被處置。