【发布时间】:2011-06-19 12:09:50
【问题描述】:
我的任务是制作一个粘性表格,它可以粘贴在屏幕的顶部或底部或左侧或右侧。因此,如果它贴在屏幕的左侧或右侧 - 它应该具有最大高度和固定宽度。如果它粘在顶部或底部 - 它应该具有固定的高度和最大化的宽度(屏幕宽度的 100%)。如何在 c# 4.0 中制作它?也许有一些合适的现成解决方案?
更新1
好的,贴一个窗口是一个很好的链接。非常感谢!现在我在设置表单的宽度和高度时遇到问题,而它被鼠标选中并移动。
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 像素 - 因为我没有在 ResizeBegin 之后触发 ResizeEnd 事件...但是我怎样才能实现像我上面描述的那样?
更新2
所以,现在我有以下代码。此代码将表单粘贴到屏幕边缘。但是当用户尝试 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 中调整大小,但它不起作用。你能帮我搞定它吗?
【问题讨论】:
-
您想影响最大化窗口的行为吗?
-
如果您的窗口已最大化宽度,那么它将被卡在屏幕的左右边缘,您确定要对您的表单做什么吗?
-
我写了当它贴在顶部或底部边缘时所需的最大宽度和固定高度。
-
SLaks,在启动程序后主窗口被贴在屏幕的右边缘,它有一个固定的宽度(200px)和最大高度。通过对这个窗口的一些操作,我需要能够通过规则将这个窗口粘贴到屏幕的另一侧,我在上面的问题正文中对此进行了描述。所以,现在我认为一个好主意是将窗口缩小到小尺寸(例如,50px 宽度和 50px 高度),而当用户按标题移动窗口时。如果用户将这个窗口移动到非常靠近屏幕的某个边,表单就会卡住。