2011-06-19 76 views
3

我的任務是製作一個粘貼表格,它可以粘貼到屏幕的頂部或底部或左側或右側部分。所以,如果它粘在屏幕的左側或右側 - 它應該有最大高度和固定寬度。如果它堅持頂部或底部 - 它應該有一個固定的高度和最大化的寬度(屏幕寬度的100%)。我怎樣才能使它在C#4.0?也許有一些合適的現成解決方案?如何製作可以粘貼到屏幕邊框的表格

UPDATE1

好吧,這是一個很好的鏈接,堅持一個窗口。大thx!現在,我在使用鼠標移動時設置窗體的寬度和高度時遇到問題。

namespace WordLearn 
{ 
    public partial class FormWord : Form 
    { 
     private const int SnapDist = 70; 
     private int currWidth = 0; 
     private int currHeight = 0; 

     public FormWord() 
     { 
      InitializeComponent(); 

     } 



     private bool DoSnap(int pos, int edge) 
     { 
      int delta = pos - edge; 
      return delta > 0 && delta <= SnapDist; 
     } 

     protected override void OnResizeEnd(EventArgs e) 
     { 
      base.OnResizeEnd(e); 
      Boolean key = false; 

      Screen scn = Screen.FromPoint(this.Location); 
      if (DoSnap(this.Left, scn.WorkingArea.Left)) 
      { 
       key = true;  
       this.Width = 200; 
       this.Height = scn.WorkingArea.Height; 
       this.Left = scn.WorkingArea.Left; 
      } 
      if (DoSnap(this.Top, scn.WorkingArea.Top)) 
      { 
       key = true; 
       this.Height = 200; 
       this.Width = scn.WorkingArea.Width; 
       this.Top = scn.WorkingArea.Top; 
      } 
      if (DoSnap(scn.WorkingArea.Right, this.Right)) 
      { 
       key = true; 
       this.Width = 200; 
       this.Height = scn.WorkingArea.Height; 
       this.Left = scn.WorkingArea.Right - this.Width; 
      } 
      if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) 
      { 
       key = true; 
       this.Height = 200; 
       this.Width = scn.WorkingArea.Width; 
       this.Top = scn.WorkingArea.Bottom - this.Height; 
      } 
      if (!key) 
      { 
       this.Width = currWidth; 
       this.Height = currHeight; 
      } 

     } 

     protected override void OnResizeBegin(EventArgs e) 
     { 
      base.OnResizeBegin(e); 

      currWidth = this.Width; 
      currHeight = this.Height; 

      this.Width = 50; 
      this.Height = 50; 


     } 
    } 
} 

我明白爲什麼它不調整爲50×50像素 - 因爲我ResizeBegin後不火ResizeEnd事件......但我怎麼能實現像我上面描述?

UPDATE2

所以,現在我有下面的代碼。這段代碼粘貼到屏幕邊緣。但我想這種形式來調整(大小(200,200)),當用戶嘗試扯去it.Because如果他移動大streched窗口也將在下一個規則再次貼...

namespace WordLearn 
{ 
    public partial class FormWord : Form 
    { 

     private const int stickDist = 100; 

     private Screen scn = null; 
     private int maxW = 0; 
     private int maxH = 0; 

     private int fixedW = 300; 
     private int fixedH = 300; 

     public FormWord() 
     { 
      InitializeComponent(); 
     } 

     private void FormWord_Load(object sender, EventArgs e) 
     { 
      this.scn = Screen.FromPoint(this.Location); 
      maxW = scn.WorkingArea.Width; 
      maxH = scn.WorkingArea.Height; 
      Point p = new Point(0, 0); 
      this.Size = new Size(fixedW, maxH); 
      this.Location = p; 
     } 

     private void FormWord_Move(object sender, EventArgs e) 
     { 
      if (maxH != 0 && maxW != 0 && this.Location.X != 0 && this.Location.Y != 0 && this.Location.X != maxW-fixedW && this.Location.Y != maxH - fixedH) 
      { 
       label1.Text = this.Location.X.ToString() + ":" + this.Location.Y.ToString(); 
       if (this.Location.Y < stickDist) 
       { 
        Point p = new Point(0, 0); 
        this.Size = new Size(maxW, fixedH); 
        this.Location = p; 
       } 
       else if ((this.Location.Y + this.Height) > (maxH - stickDist)) 
       { 
        Point p = new Point(0, (maxH - fixedH)); 
        this.Size = new Size(maxW, fixedH); 
        this.Location = p; 
       }else if (this.Location.X < stickDist) 
       { 
        Point p = new Point(0, 0); 
        this.Size = new Size(fixedW, maxH); 
        this.Location = p; 
       } 
       else if ((this.Location.X + this.Width) > (maxW - stickDist)) 
       { 
        Point p = new Point((maxW - fixedW), 0); 
        this.Size = new Size(fixedW, maxH); 
        this.Location = p; 
       } 
      }  
     } 

     private void FormWord_ResizeBegin(object sender, EventArgs e) 
     { 
      this.Size = new Size(200,200); 
      int x = 0; 
      int y = 0; 
      if (this.Location.Y == 0) 
      { 
       y = stickDist * 2; 
      } 
      else 
      { 
       y = this.Location.Y - stickDist * 2; 
      } 
      if (this.Location.X == 0) 
      { 
       x = stickDist * 2; 
      } 
      else 
      { 
       x = this.Location.X - stickDist * 2; 
      } 

      this.Location = new Point(x, y); 
      Cursor.Position = new Point(x + 2, y + 2); 

     } 


    } 
} 

我嘗試做這在無效的FormWord_ResizeBegin中調整大小,但它不起作用。你能幫我解決問題嗎?

+1

你想影響最大化窗口的行爲嗎? – SLaks

+1

如果你的窗口寬度最大,那麼它將被粘貼到屏幕的右邊和左邊,你確定你想用你的窗體做什麼? –

+0

我寫到最大寬度和固定高度時,它需要堅持頂部或底部邊緣。 – dizpers

回答

5

您需要掛入ResizeBeginResizeEnd事件。當表單移動以及調整大小時,這些會被觸發。

在這些中,您可以檢查表單的當前位置,如果它位於屏幕邊緣(您確定邊距的位置)的X像素範圍內,請根據您的規則調用您的代碼以調整大小和放置表單。

您需要澄清規則觸發和放入代碼的順序,以確保第二個規則不會因第一個調整窗口大小而觸發。

1

你可以做這樣的事情:

private void Form1_Load(object sender, EventArgs e) // On Form Load 
{ 
    this.WindowState = FormWindowState.Maximized; 
    if (this.WindowState == FormWindowState.Maximized) 
    { 
     maxW = this.Size.Width; 
     maxH = this.Size.Height; 
    } 
    this.WindowState = FormWindowState.Normal; 
} 

private void Form1_Move(object sender, EventArgs e) 
{ 
    if (maxH != 0 && maxW != 0) 
    { 
     if (this.Location.Y < 100) 
     { 
      Point p = new Point(0, 0); 
      this.Size = new Size(maxW, 700); 
      this.Location = p; 
     } 
     else if (this.Location.Y > (maxH - 100)) 
     { 
      Point p = new Point(0, (maxH - 700)); 
      this.Size = new Size(maxW, 700); 
      this.Location = p;     
     } 
    }  
} 

希望這是你需要的東西!

相關問題