Move control either horizontally or vertically, not combination of both

前端 未结 2 1068
一整个雨季
一整个雨季 2021-01-28 11:12

I was here burning the midnight oil around some lines of code trying to solve a problem.

The code bellow will setup the control to be able to move anywhere within its p

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 11:29

    You cannot make this reliable as intended. The common solution for this UI problem is to provide the user with an assist that he can turn on and off as desired. The Shift key is the common choice for that. Allow free movement when it is turned off, but snap in the dominant direction when it is held down.

    You use the Control.ModifierKeys property to check if the key is down in your MouseMove event handler. You need the KeyDown and KeyUp event handlers so you can see the Shift key being pressed and released. Significant changes are required to properly follow the mouse position, it is not necessarily hovering over the control anymore when you hold down the Shift key. Enough moving parts to encapsulate this in a helper class:

    class ControlMover {
        private Control control;
        private Point downPos;
        private Point startPos;
        enum Constrains { None, Hor, Ver };
        private Constrains constraint;
    
        public ControlMover(Control ctl) {
            control = ctl;
            startPos = control.Location;
            downPos = Cursor.Position;
            control.Capture = true;
            control.MouseMove += control_MouseMove;
            control.MouseUp += control_MouseUp;
            control.MouseCaptureChanged += control_MouseCaptureChanged;
            control.KeyDown += control_KeyDown;
            control.KeyUp += control_KeyUp;
        }
    
        void handleKey(Keys key, bool down) {
            Console.WriteLine((int)key);
            if (key == Keys.Escape) {
                control.Capture = false;
                control.Location = startPos;
            }
            else if ((key & Keys.KeyCode) == Keys.ShiftKey) {
                if (!down) constraint = Constrains.None;
                else if (constraint == Constrains.None) {
                    var curPos = Cursor.Position;
                    if (Math.Abs(curPos.X - downPos.X) >= Math.Abs(curPos.Y - downPos.Y))
                         constraint = Constrains.Hor;
                    else constraint = Constrains.Ver;
                }
                moveControl();
            }
        }
    
        void control_MouseCaptureChanged(object sender, EventArgs e) {
            // This ends it
            if (control.Capture) return;
            control.MouseMove -= control_MouseMove;
            control.MouseUp -= control_MouseUp;
            control.MouseCaptureChanged -= control_MouseCaptureChanged;
            control.KeyDown -= control_KeyDown;
            control.KeyUp -= control_KeyUp;
    
        }
        private void moveControl() {
            var curPos = Cursor.Position;
            if (constraint == Constrains.Hor) curPos.Y = downPos.Y;
            if (constraint == Constrains.Ver) curPos.X = downPos.X;
            curPos = control.Parent.PointToClient(curPos);
            // Keep it inside the parent
            curPos.X = Math.Max(0, curPos.X);
            curPos.Y = Math.Max(0, curPos.Y);
            curPos.X = Math.Min(control.Parent.ClientSize.Width - control.Width, curPos.X);
            curPos.Y = Math.Min(control.Parent.ClientSize.Height - control.Height, curPos.Y);
            control.Location = curPos;
        }
    
        void control_MouseUp(object sender, MouseEventArgs e) { control.Capture = false; }
        void control_MouseMove(object sender, MouseEventArgs e) { moveControl(); }
        void control_KeyDown(object sender, KeyEventArgs e) { handleKey(e.KeyData, true); }
        void control_KeyUp(object sender, KeyEventArgs e) { handleKey(e.KeyData, false); }
    }
    

    Sample usage:

        private void button1_MouseDown(object sender, MouseEventArgs e) {
            new ControlMover(button1);
        }
    

提交回复
热议问题