110,536
社区成员
发帖
与我相关
我的任务
分享
/// <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;
}
}
}
//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