(转)C#下拖动没有边框的窗体
原文:http://topic.csdn.net/t/20020425/15/675744.html
方法一:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace NetFishApplication
{
class MyForm : Form
{
// 鼠标位移记录器
private MouseOffsetRecorder mr = new MouseOffsetRecorder();
// 可拖动标志
private bool boolFormDrag = false;
public MyForm()
{
// 设置窗体样式
this.AutoScale = false;
this.BackColor = System.Drawing.Color.Red;
this.ClientSize = new System.Drawing.Size(140, 50);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
// 事件关联
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseUp);
this.DoubleClick += new System.EventHandler(this.MyForm_DBClick);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseDown);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyForm_OnPaint);
}
static void Main()
{
Application.Run(new MyForm());
}
private void MyForm_OnMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.boolFormDrag = true;
mr.Record();
}
}
private void MyForm_OnMouseMove(object sender, MouseEventArgs e)
{
if ( this.boolFormDrag == true)
{
this.Location = new Point(this.Location.X + mr.offsetX, this.Location.Y + mr.offsetY);
mr.Record();
}
}
private void MyForm_OnMouseUp(object sender, MouseEventArgs e)
{
this.boolFormDrag = false;
}
private void MyForm_DBClick(object sender, System.EventArgs e)
{
this.Close();
}
protected void MyForm_OnPaint(object sender,System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Double Click To Exit",this.Font,new SolidBrush(Color.Black),1,20);
}
}
/// <summary>
/// 记录鼠标在两个动作间的位移
/// </summary>
public class MouseOffsetRecorder
{
private Point StartPosition = new Point();
// 水平位置上的位移
public int offsetX
{
get
{
return this.GetCurrentPosition().X - this.StartPosition.X;
}
}
// 垂直位置上的位移
public int offsetY
{
get
{
return this.GetCurrentPosition().Y - this.StartPosition.Y;
}
}
public void Record()
{
this.StartPosition = this.GetCurrentPosition();
}
protected Point GetCurrentPosition()
{
return new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
}
}
}
方法二:
可以通过在Form窗体的MouseMove和MouseDown事件响应方法中来记录鼠标的位置,并计算出Form窗体的新位置。下面,提供一段示例代码,供您参考:
// 记录鼠标在Form窗体上的位置
private Point mouse_offset;
……
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Gets the x-coordinate and y-coordinate of a mouse click based on the the client area of the form
mouse_offset = new Point(-e.X, -e.Y);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
// Gets the position of the mouse cursor in screen coordinates
Point mousePos = Control.MousePosition;
// Translates this Point by the specified amount.
mousePos.Offset(mouse_offset.X, mouse_offset.Y);
Location = mousePos;
}
}
没测试,暂时放着,留作备用,嘿嘿
浙公网安备 33010602011771号