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;
}
谢谢。