如何制作可以贴在屏幕边框上的表格

Dmi*_*sev 3 c# winforms

我的任务是制作一个粘性形式,它可以粘在屏幕的顶部或底部或左侧或右侧.因此,如果它粘在屏幕的左侧或右侧 - 它应该具有最大高度和固定宽度.如果它粘在顶部或底部 - 它应该具有固定的高度和最大宽度(屏幕宽度的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;


        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我理解为什么它没有调整到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);

        }


    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试在void FormWord_ResizeBegin中调整大小,但它不起作用.你能帮我把它搞定吗?

Chr*_*isF 5

你需要挂钩ResizeBeginResizeEnd事件.当表单移动和调整大小时,它们会被触发.

在这些中,您可以检查表单的当前位置,如果它X在屏幕边缘的像素内(您确定边距),请调用代码以根据规则调整表单的大小和位置.

您需要澄清规则触发的顺序并放入代码以确保第一个规则不会因第一次调整窗口大小而触发.