如何在WinForm里创建一个可移动的控件

liu447862556 2011-06-23 12:31:42
在WinForm里创建一个控件,例如button1,运行程序之后,可以用鼠标在界面上进行拖动,而且,大小可以调整,功能不变。
...全文
557 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
luluyy 2011-06-23
  • 打赏
  • 举报
回复
MouseMove

MouseDown

MouseUp

这三个事件处理
xuexiaodong2009 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]

http://blog.csdn.net/dekko/archive/2007/09/24/1797903.aspx
[/Quote]牛人啊
liu447862556 2011-06-23
  • 打赏
  • 举报
回复
最好有图有注释。
liu447862556 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 isjoe 的回复:]
C# code


/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

……
[/Quote]

这段代码应该是在Form.cs下写的,我试了一下,确实可以实现控件的移动。不过有一个问题,重新运行后控件又回到了原来的位置。
还有,代码中的///后面的字是灰色的,不是注释么?能否解释一下为什么在事件里面///后面的注释还是绿色的,而在事件外的任意地方,当输入///后,或自动出现
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
光标自动停在第二行,等待输入信息,这应该是注释吧。如果是注释的话,用//就可以,不明白为什么要用///,而且字还是灰色的。
我对这只是好奇而已,所以冒昧的问一下。
liu447862556 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 isjoe 的回复:]
运行后可以用鼠标拖动静态文本、按钮、列表框、单选、复选控件的例子,N年前写的。。。
能找到真是个奇迹。。。

C# code

//csj...
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows……
[/Quote]

你的这段代码是在Form.cs下写的么?我怎么看有点像在Form.Designer.cs下写的。
而且,Form.Designer.cs下的代码好像是自动生成的,不需要我们去改写吧。
liu447862556 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
http://blog.csdn.net/dekko/archive/2007/09/24/1797903.aspx
[/Quote]

我看了一下你给推荐的文章,代码我也按上面的写了一下,可是,运行之后没有任何反应。
我是在WinForm里写的,是不是我写错地方了。你可否贴几张图上来。
isjoe 2011-06-23
  • 打赏
  • 举报
回复


/// <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;
}
}
}


isjoe 2011-06-23
  • 打赏
  • 举报
回复
运行后可以用鼠标拖动静态文本、按钮、列表框、单选、复选控件的例子,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

meic1985 2011-06-23
  • 打赏
  • 举报
回复
学习了~
没法下载,到这折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Ro

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧