如何制作可以粘贴到屏幕边框的表单

本文关键字:屏幕 边框 表单 何制作 | 更新日期: 2023-09-27 17:59:26

我的任务是制作一个粘性表单,它可以粘贴到屏幕的顶部或底部,也可以粘贴到左侧或右侧。所以,如果它粘在屏幕的左侧或右侧,它应该有最大的高度和固定的宽度。如果它粘在顶部或底部,它应该有一个固定的高度和最大的宽度(100%的屏幕宽度)。如何在c#4.0中实现它?也许有一些合适的现成解决方案?

更新1

好的,这是粘贴窗口的一个很好的链接。大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像素-因为我不会在ResizeBegin之后触发ResizeEnd事件。。。但是我怎样才能实现我上面描述的东西呢?

更新2

所以,现在我有了以下代码。此代码粘贴到一个屏幕边缘。但我希望这个表单在用户尝试取消勾选时调整大小(到大小(200200))。因为如果他移动大的拉伸窗口,它将根据下一条规则再次粘贴。。。

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中调整大小,但不起作用。你能帮我把它修好吗?

如何制作可以粘贴到屏幕边框的表单

您需要挂接到ResizeBeginResizeEnd事件。当窗体被移动和调整大小时,这些会被激发。

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

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

您可以这样做:

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;                
        }
    }     
}

希望这正是你所需要的!