11 个解决方案
#1
最好有图有注释。
#3
牛人啊
#4
MouseMove
MouseDown
MouseUp
这三个事件处理
MouseDown
MouseUp
这三个事件处理
#5
学习了~
#6
运行后可以用鼠标拖动静态文本、按钮、列表框、单选、复选控件的例子,N年前写的。。。
能找到真是个奇迹。。。
能找到真是个奇迹。。。
//csj...
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Drop
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool Mousedown = false; //鼠标按下为true
private int CurX = 0,CurY = 0;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.ListBox listBox1; //保存鼠标按下时鼠标在窗口的坐标
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "按钮";
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
//
// label1
//
this.label1.Location = new System.Drawing.Point(192, 208);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 1;
this.label1.Text = "静态文本";
this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(192, 168);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
this.textBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseUp);
this.textBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseMove);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(128, 128);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "checkBox1";
this.checkBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseUp);
this.checkBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseMove);
this.checkBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseDown);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(128, 96);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 4;
this.radioButton1.Text = "radioButton1";
this.radioButton1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseUp);
this.radioButton1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseMove);
this.radioButton1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseDown);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(344, 48);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(128, 64);
this.listBox1.TabIndex = 5;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseUp);
this.listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseMove);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 365);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
#7
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.button1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.button1.Capture = true;
}
private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.button1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void radioButton1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.radioButton1.Capture = true;
}
private void radioButton1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.radioButton1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void radioButton1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.radioButton1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.checkBox1.Capture = true;
}
private void checkBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.checkBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void checkBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.checkBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.textBox1.Capture = true;
}
private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.textBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void textBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.textBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.listBox1.Capture = true;
}
private void listBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.listBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.listBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.label1.Capture = true;
}
private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.label1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.label1.Capture = false;
}
}
}
#8
我看了一下你给推荐的文章,代码我也按上面的写了一下,可是,运行之后没有任何反应。
我是在WinForm里写的,是不是我写错地方了。你可否贴几张图上来。
#9
你的这段代码是在Form.cs下写的么?我怎么看有点像在Form.Designer.cs下写的。
而且,Form.Designer.cs下的代码好像是自动生成的,不需要我们去改写吧。
#10
这段代码应该是在Form.cs下写的,我试了一下,确实可以实现控件的移动。不过有一个问题,重新运行后控件又回到了原来的位置。
还有,代码中的///后面的字是灰色的,不是注释么?能否解释一下为什么在事件里面///后面的注释还是绿色的,而在事件外的任意地方,当输入///后,或自动出现
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
光标自动停在第二行,等待输入信息,这应该是注释吧。如果是注释的话,用//就可以,不明白为什么要用///,而且字还是灰色的。
我对这只是好奇而已,所以冒昧的问一下。
#11
#1
最好有图有注释。
#2
#3
牛人啊
#4
MouseMove
MouseDown
MouseUp
这三个事件处理
MouseDown
MouseUp
这三个事件处理
#5
学习了~
#6
运行后可以用鼠标拖动静态文本、按钮、列表框、单选、复选控件的例子,N年前写的。。。
能找到真是个奇迹。。。
能找到真是个奇迹。。。
//csj...
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Drop
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool Mousedown = false; //鼠标按下为true
private int CurX = 0,CurY = 0;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.ListBox listBox1; //保存鼠标按下时鼠标在窗口的坐标
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "按钮";
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
//
// label1
//
this.label1.Location = new System.Drawing.Point(192, 208);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 1;
this.label1.Text = "静态文本";
this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(192, 168);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
this.textBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseUp);
this.textBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseMove);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(128, 128);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "checkBox1";
this.checkBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseUp);
this.checkBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseMove);
this.checkBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseDown);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(128, 96);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 4;
this.radioButton1.Text = "radioButton1";
this.radioButton1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseUp);
this.radioButton1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseMove);
this.radioButton1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.radioButton1_MouseDown);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(344, 48);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(128, 64);
this.listBox1.TabIndex = 5;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseUp);
this.listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseMove);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 365);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
#7
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.button1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.button1.Capture = true;
}
private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.button1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void radioButton1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.radioButton1.Capture = true;
}
private void radioButton1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.radioButton1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void radioButton1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.radioButton1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.checkBox1.Capture = true;
}
private void checkBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.checkBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void checkBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.checkBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.textBox1.Capture = true;
}
private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.textBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void textBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.textBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.listBox1.Capture = true;
}
private void listBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.listBox1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.listBox1.Capture = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
CurY = e.Y;
Mousedown = true;
this.label1.Capture = true;
}
private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Mousedown)
{
//Cursor.Position .X Cursor.Position .Y 是屏幕坐标
//用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
Point np = this.PointToClient(sp);
this.label1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
}
}
private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Mousedown = false;
this.label1.Capture = false;
}
}
}
#8
我看了一下你给推荐的文章,代码我也按上面的写了一下,可是,运行之后没有任何反应。
我是在WinForm里写的,是不是我写错地方了。你可否贴几张图上来。
#9
你的这段代码是在Form.cs下写的么?我怎么看有点像在Form.Designer.cs下写的。
而且,Form.Designer.cs下的代码好像是自动生成的,不需要我们去改写吧。
#10
这段代码应该是在Form.cs下写的,我试了一下,确实可以实现控件的移动。不过有一个问题,重新运行后控件又回到了原来的位置。
还有,代码中的///后面的字是灰色的,不是注释么?能否解释一下为什么在事件里面///后面的注释还是绿色的,而在事件外的任意地方,当输入///后,或自动出现
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
光标自动停在第二行,等待输入信息,这应该是注释吧。如果是注释的话,用//就可以,不明白为什么要用///,而且字还是灰色的。
我对这只是好奇而已,所以冒昧的问一下。