开发者

Moving a Control By Mouse

I am going to Moving a Button with Mouse , Everything is ok , but when I move mouse on button window , left and top of the button ( top-left corner) will locate at cursor pos .

I don't want this to occur . where is the bug in my code ?

private void button1_MouseDown(obje开发者_Go百科ct sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        clicked = true;
    }

}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    if (clicked)
    {
        Point p = new Point();//in form coordinates
        p.X =  e.X + button1.Left;
        p.Y =  e.Y + button1.Top;
        button1.Left = p.X;
        button1.Top = p.Y ;

    }

}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    clicked = false;   
}


This is all you need

    private Point MouseDownLocation;

    private void MyControl_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void MyControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            this.Left = e.X + this.Left - MouseDownLocation.X;
            this.Top = e.Y + this.Top - MouseDownLocation.Y;
        }
    }


I have found it...

here is the full code :

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Point p = ConvertFromChildToForm(e.X, e.Y, button1);
        iOldX = p.X;
        iOldY = p.Y;
        iClickX = e.X;
        iClickY = e.Y;
        clicked = true;
    }

}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    if (clicked)
    {
        Point p = new Point();//in form coordinates
        p.X =  e.X + button1.Left;
        p.Y =  e.Y + button1.Top;
        button1.Left = p.X - iClickX;
        button1.Top = p.Y - iClickY;

    }

}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    clicked = false;   
}


I don't know if I got it right, but just in case... if the problem is to position the cursor in the center of the button (or another component), you can achive it by considering the width and height:

 private void button1_MouseMove(object sender, MouseEventArgs e) {
      if (clicked) {
        Point p = new Point(); //in form coordinates
        p.X = e.X + button1.Left - (button1.Width/2);
        p.Y = e.Y + button1.Top - (button1.Height/2);
        button1.Left = p.X;
        button1.Top = p.Y;
      }
    }


I think this is full code you need:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveButton
{
public partial class Form1 : Form
{
    bool clicked = false;
    Point iOld = new Point();
    Point iClick = new Point();
    public Form1()
    {
        InitializeComponent();
    }
    private Point ConvertFromChildToForm(int x, int y, Control control)
    {
        Point p = new Point(x, y);
        control.Location = p;
        return p;
    }
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point p = ConvertFromChildToForm(e.X, e.Y, button1);
            iOld.X = p.X;
            iOld.Y = p.Y;
            iClick.X = e.X;
            iClick.Y = e.Y;
            clicked = true;
        }

    }

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clicked)
        {
            Point p = new Point();//in form coordinates
            p.X = e.X + button1.Left;
            p.Y = e.Y + button1.Top;
            button1.Left = p.X - iClick.X;
            button1.Top = p.Y - iClick.Y;

        }

    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        clicked = false;
    }
   }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜