[英]Moving a Control By Mouse
我要使用“鼠標移動按鈕”,一切正常,但是當我在按鈕窗口上移動鼠標時,按鈕的左側和頂部(左上角)將位於光標pos處。
我不希望這種情況發生。 我的代碼中的錯誤在哪里?
private void button1_MouseDown(object 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;
}
這就是你所需要的
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;
}
}
我發現了...
這是完整的代碼:
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;
}
我不知道我是否正確,但以防萬一...如果問題是將光標定位在按鈕(或其他組件)的中央,則可以通過考慮寬度和高度來實現:
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;
}
}
我認為這是您需要的完整代碼:
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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.