1
我在調整窗體大小事件中的窗體大小時遇到問題。我試圖避免當用戶不能最大化窗體並且窗體無法調整大小(因爲窗口總是在屏幕外)時窗體熄滅的情況。儘管我現在似乎無法再現這種情況。無論如何,我想出了一些代碼來擺脫這種情況,如果它再次發生。問題是當表單未被最大化時,雖然達到了if語句中的代碼,但表單高度並未被設置。有一次,當我運行我的應用程序時,Top和Left屬性被損壞,兩者都變成了-32000。我再次提出了一些代碼來防止這造成問題。這裏是代碼,注意寬度是固定的:調整窗體大小事件中的大小代碼不起作用
public partial class MainForm : Form
{
Rectangle sr;
FormWindowState wp;
public MainForm()
{
sr = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
MaximumSize = new Size(Width, sr.Height);
wp = WindowState;
}
private void MainForm_Activated(object sender, EventArgs e)
// positions the form
{
Top = Properties.Settings.Default.Top;
if ((Top > sr.Height - 80) || (Top < 0))
Top = 80;
Left = Properties.Settings.Default.Left;
if ((Left > sr.Width - 80) || (Left < 0))
Left = 80;
Height = Properties.Settings.Default.Height;
}
private void MainForm_Deactivate(object sender, EventArgs e)
// remembers the forms position
{
Properties.Settings.Default.Top = Top;
Properties.Settings.Default.Left = Left;
Properties.Settings.Default.Height = Height;
Properties.Settings.Default.Save();
}
private void MainForm_Resize(object sender, EventArgs e)
{
Control control = (Control)sender;
if ((WindowState == FormWindowState.Normal) &&
(wp == FormWindowState.Maximized) &&
(control.Size.Height > sr.Height - 80))
// the following line has no effect:
control.Size = new Size(control.Size.Width, 400);
wp = WindowState;
}
謝謝。