using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MoveControlLocation
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(32, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
this.comboBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.comboBox1_MouseMove);
//
// button1
//
this.button1.Location = new System.Drawing.Point(200, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.comboBox1.Location=new Point(2,76);
} private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X,e.Y);
} private void comboBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X+this.comboBox1.Left,e.Y+this.comboBox1.Top);
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MoveControlLocation
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(32, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
this.comboBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.comboBox1_MouseMove);
//
// button1
//
this.button1.Location = new System.Drawing.Point(200, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.comboBox1.Location=new Point(2,76);
} private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X,e.Y);
} private void comboBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X+this.comboBox1.Left,e.Y+this.comboBox1.Top);
}
}
}
解决方案 »
- 有奖问答! 这个问题应该不算难吧?
- 在WINFORM(C#)如何将数据库中的表中的数据导出,生成EXCEL文件
- C#中从数据库查询的问题
- asp.net 网站后台登陆其它问题
- 未将对象引用设置到对象的实例。 急求解?????
- C#(Win Form)中如何把一个值传到Google上
- 哪位好心的大哥帮我解决一下多线程文件操作的问题,附代码。(2)
- 用vs2005做单元测试,出现TestManagement.ExecutionException异常,报文件不受信任
- ■■升了一个小内裤■■散分
- 每次调试都全部重新生成一次解决方案下的所有项目,不管修改没修改.我的解决方案太大,每次都重新生成太麻烦了.有没有哪位大神遇到过这种情况?
- datagrid问题
- 如何更新datagrid的数据源?
我给别人写的代码;和你的问题差不多; 把那个画的长方形改成你要拖动的控件就成!!
要自己多实践;using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication37
{
/// <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 Point location;
private Point offset;
private bool flag; 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.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
int iLeft,iTop;
iLeft = 0;
iTop = 0; Graphics dc = this.CreateGraphics();
//Pen RedPen = new Pen(Color.Red,1);
dc.FillRectangle(System.Drawing.Brushes.Red ,iLeft,iTop,100,50);
location.X =iLeft;
location.Y =iTop;
}
private void Form1_MouseDown(object sender, System.Windows .Forms .MouseEventArgs e)
{ if ((location.X < e.X) && (e.X < (location.X +100)))
{
if (((location.Y+50) > e.Y) && (e.Y > location.Y))
{
flag=true;
offset.X =e.X ;
offset.Y =e.Y ;
}
}
else
{
flag=false;
}
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (flag==true)
{
offset.X =e.X-offset.X ;
offset.Y =e.Y- offset.Y ;
Graphics dc =this.CreateGraphics();
location.X =location.X +offset.X ;
location.Y =location.Y +offset.Y ;
//MessageBox.Show (location.ToString ());
//this.Invalidate ();
dc.FillRectangle(System.Drawing.Brushes.Red ,location.X ,location.Y ,100,50 );
} } private void Form1_Load(object sender, System.EventArgs e)
{
location=new Point();
offset=new Point ();
}
}
}