我的任务是制作一个粘性表格,它可以粘在屏幕的顶部或底部或左侧或右侧。因此,如果它粘在屏幕的左侧或右侧 - 它应该具有最大高度和固定宽度。如果它粘在顶部或底部 - 它应该具有固定的高度和最大宽度(屏幕宽度的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;
}
}
}
我理解为什么它没有调整到50x50 px - 因为我在ResizeBegin之后没有激活ResizeEnd事件......但是我怎么能实现像上面描述的那样?
UPDATE2
所以,现在我有以下代码。此代码粘贴到屏幕边缘。但是当用户尝试UNSTICK时,我希望这个表单调整大小(大小(200,200))。因为如果他移动大的拉链窗口,它将再次被下一个规则粘住...
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);
}
}
}
我尝试在void FormWord_ResizeBegin中调整大小,但它不起作用。你能帮我把它搞定吗?
答案 0 :(得分:5)
您需要加入ResizeBegin
和ResizeEnd
事件。这些表格在移动和调整大小时会被触发。
在这些中,您可以检查表单的当前位置,如果它位于屏幕边缘的X
像素范围内(您确定边距),请根据您的规则调用代码以调整表单大小并定位表单
您需要澄清规则触发的顺序并输入代码,以确保第一个规则不会因第一次调整窗口大小而触发。
答案 1 :(得分: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;
}
}
}
希望这就是你所需要的!