我的任務是製作一個粘貼表格,它可以粘貼到屏幕的頂部或底部或左側或右側部分。所以,如果它粘在屏幕的左側或右側 - 它應該有最大高度和固定寬度。如果它堅持頂部或底部 - 它應該有一個固定的高度和最大化的寬度(屏幕寬度的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中調整大小,但它不起作用。你能幫我解決問題嗎?
你想影響最大化窗口的行爲嗎? – SLaks
如果你的窗口寬度最大,那麼它將被粘貼到屏幕的右邊和左邊,你確定你想用你的窗體做什麼? –
我寫到最大寬度和固定高度時,它需要堅持頂部或底部邊緣。 – dizpers