2014-04-06 3 views
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; 
    } 

감사합니다.

답변

1

높이/너비 변경을 적용하려고 할 때 양식이 최대화 상태가 아닌지 확인하십시오.

- Athar

관련 문제