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

C# 如何制作可以粘贴到屏幕边框的表单,c#,winforms,C#,Winforms,我的任务是制作一个粘性窗体,它可以粘贴到屏幕的顶部、底部、左侧或右侧。所以,如果它粘在屏幕的左侧或右侧,它应该有最大的高度和固定的宽度。如果它粘在顶部或底部-它应该有一个固定的高度和最大宽度(100%的屏幕宽度)。我如何在c#4.0中实现它?也许有一些合适的现成解决方案 更新1 好的,这是一个粘贴窗口的好链接。大thx!现在我有一个问题,设置宽度和高度的形式,而它是由鼠标拾取和移动 namespace WordLearn { public partial class FormWord :

我的任务是制作一个粘性窗体,它可以粘贴到屏幕的顶部、底部、左侧或右侧。所以,如果它粘在屏幕的左侧或右侧,它应该有最大的高度和固定的宽度。如果它粘在顶部或底部-它应该有一个固定的高度和最大宽度(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;


        }
    }
}
namespace-WordLearn
{
公共部分类FormWord:Form
{
私有常量int SnapDist=70;
私有int currWidth=0;
私有int currHeight=0;
公共FormWord()
{
初始化组件();
}
私有布尔多斯纳普(内部位置,内部边缘)
{
int delta=位置-边缘;

返回delta>0&&delta您需要钩住
ResizeBegin
ResizeEnd
事件。这些事件在移动表单和调整表单大小时触发

在这些对话框中,您可以检查表单的当前位置,如果它在屏幕边缘的
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;                
        }
    }     
}
private void Form1\u Load(对象发送方,EventArgs e)//在表单加载时
{
this.WindowState=FormWindowState.Maximized;
if(this.WindowState==FormWindowState.Maximized)
{
maxW=this.Size.Width;
maxH=this.Size.Height;
}
this.WindowState=FormWindowState.Normal;
}
私有void Form1\u移动(对象发送方,事件参数e)
{
如果(maxH!=0&&maxW!=0)
{
如果(该位置Y<100)
{
点p=新点(0,0);
该尺寸=新尺寸(最大值,700);
这个位置=p;
}
else if(this.Location.Y>(maxH-100))
{
点p=新点(0,(最大值-700));
该尺寸=新尺寸(最大值,700);
这个位置=p;
}
}     
}

希望这就是你所需要的!

你想影响最大化窗口的行为吗?如果你的窗口具有最大化的宽度,那么它将被粘贴到屏幕的左右边缘,你确定你想对表单做什么吗?我写了当它粘贴到顶部或底部边缘时需要的最大化宽度和固定高度。SLaks,启动后程序主窗口粘贴在屏幕的右边缘,它有固定的宽度(200px)和最大高度。通过对该窗口进行一些操作,我需要能够根据规则将该窗口粘贴到屏幕的另一侧,这一点我在问题正文中已经描述过。因此,现在我认为一个好主意是将窗口缩小到较小的尺寸(例如,50px宽度和50px高度),当用户按页眉移动窗口时。如果用户将此窗口移动到非常靠近屏幕一侧,则形成木棍。可能的
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;                
        }
    }     
}