开发者

How to make form, which can stick to screen borders

My task is to make a sticky form, which can stick to top or bottom or left or right part of screen. So, if it sticks to left or right side of a screen - it should have maximal height and fixed width. If it sticks to top or bottom side - it should have a fixed height and maximized width (100% of a screen width). How can I make it in c# 4.0? Maybe there are some suitable off-the-shelf solution?

UPDATE1

Ok, it's a good link to stick a window. Big thx! Now I have a problem with setting width and height of the form while it is picked by mouse and moving.

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;


        }
    }
}

I understand why it isn't resized to 50x50 px - because i dont fire ResizeEnd event after ResizeBegin... But how can I realize something like I described above?

UPDATE2

So, now I have the following code. This code stick form to a screens edges. But I want this form to resize (to size (200,200)) when user try to UNSTICK it.Because if he move big streched window it will sticked again by the next rule...

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开发者_JAVA技巧 * 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);

        }


    }
}

I try to do this resize in void FormWord_ResizeBegin, but it don't work. Could you help me have it work?


You need to hook into the ResizeBegin and ResizeEnd events. These get fired when the form is moved as well as resized.

In these you can check the current position of the form and if it's within X pixels of the edge of the screen (where you determine the margin) call your code for resizing and positioning the form according to your rules.

You'll need to clarify the order the rules fire and put code in to ensure that the second rule doesn't fire as a result of the first resizing the window.


you can do something like this:

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

hope thats what you needed!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜