简体   繁体   中英

c# move panel horizontally with MouseMove event

I have a winforms app. Inside, I have one panel (panel1), and inside this panel, another panel (panel2) with buttons inside. I want to move panel2 horizontally inside panel1 when I mousedown in some button. I've made this in each button inside panel2.

this.button4.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btMouseDown);
        this.button4.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btMouseMove);
        this.button4.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btMouseUp);

and

void btMouseMove(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
            panel2.Location = PointToClient(this.panel2.PointToScreen(new Point(e.X - _mousePos.X, e.Y - _mousePos.Y)));            
    }
    void btMouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _mouseDown = true;
            _mousePos = new Point(e.X, e.Y);
        }
    }
    void btMouseUp(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
        {
            _mouseDown = false;
        }
    }

This code moves the panel2 correctly inside the panel1, but I want to move the panel only horizontally, and this code moves to mouse location. I tried to put

Point(e.X - _mousePos.X, 3)

Instead of

Point(e.X - _mousePos.X, e.Y - _mousePos.Y)

But panel2 disappears. I would like to know how to move the panel2 inside the panel1 only horizontally.

Thanks a lot.

    void btMouseMove(object sender, MouseEventArgs e) {
        if (_mouseDown) {
            int deltaX = e.X - _mousePos.X;
            int deltaY = e.Y - _mousePos.Y;
            panel2.Location = new Point(panel2.Left + deltaX, panel2.Top /* + deltaY */);
        }
    }

this is not the cleanest implementation, but if i understand correctly what you are trying to do, it works:

        int _x = 0;

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if(_x == 0)
        {
            _x = e.X;
        }

        int move = 0;
        Point p;

        if (e.X <= _x)
        {
            move = _x - e.X;
            p = new Point(panel2.Location.X - move, panel2.Location.Y);
        }
        else
        {
            move = e.X - _x;
            p = new Point(panel2.Location.X + move, panel2.Location.Y);
        }

        panel2.Location = p;
    }

You need to account for the current location of panel2 while moving. And you don't need to convert the mouse location between client and screen coordintes, because you only need the delta.

In addition, if you are letting users drag stuff around, I highly recommend you do not move the panel unless the drag exceeds a small threshold. It is very easy to accidentally move the mouse a few pixels while clicking on the screen.

For example:

if (delta > 3)  { // only drag if the user moves the mouse over 3 pixels
    panel2.Location = ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM