我在表单大小事件中调整表单时遇到问题。我试图避免当用户取消最大化窗体并且窗体无法调整大小(因为角落总是在屏幕外)时窗体离开屏幕的情况。虽然我现在似乎无法重现这种情况。无论如何,我提出了一些代码,以便在它再次发生时摆脱这种情况。问题是,尽管达到了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;
}
感谢。
答案 0 :(得分:1)
当您尝试应用高度/宽度更改时,请确保窗体未处于最大化状态。
- 阿塔尔