Моя задача - сделать липкую форму, которая может прилипать к верхней или нижней или левой или правой части экрана.Таким образом, если он придерживается левой или правой стороны экрана - он должен иметь максимальную высоту и фиксированную ширину.Если он придерживается верхней или нижней стороны - он должен иметь фиксированную высоту и максимальную ширину (100% ширины экрана).Как я могу сделать это в C # 4.0?Может быть, есть какое-то подходящее готовое решение?
UPDATE1
Хорошо, это хорошая ссылка, чтобы прикрепить окно.Большое спасибо!Теперь у меня проблема с настройкой ширины и высоты формы, когда она выбирается мышью и перемещается.
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 - потому что я не запускаю событие ResizeEnd послеResizeBegin ... Но как я могу реализовать что-то, как я описал выше?
UPDATE2
Итак, теперь у меня есть следующий код.Этот код придерживается формы экранов.Но я хочу, чтобы эта форма изменила размер (до 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, но это не работает.Не могли бы вы помочь мне заставить это работать?