您的位置:首页 > 其它

winform移动窗体或窗体中的控件

2013-05-10 13:37 253 查看
新建类:winMove
using System;
using System.Collections.Generic;
using System.Text;

namespace WinExcelIEview
{
using System.Windows.Forms;
internal class winMove
{
private bool isMouseDown = false;
private System.Drawing.Point FormLocation;     //form的location
private System.Drawing.Point conObjLocation;     //Control的location
private System.Drawing.Point mouseOffset;      //鼠标的按下位置
private Form movedForm = null;
private Control conObj = null;
private int rectLeft = 0;
private int rectRight = 0;

private winMove()
{ }

/// <summary>
///
/// </summary>
/// <param name="movedForm">要移动的窗体</param>
/// <param name="conObj">控制窗体移动的控件,为null,则是窗体本身</param>
public winMove(Form movedForm, Control conObj)
{
this.movedForm = movedForm;
this.conObj = (conObj==null)?movedForm:conObj;
this.conObj.MouseDown += new MouseEventHandler(ConObj_MouseDown);
this.conObj.MouseMove += new MouseEventHandler(ConObj_MouseMove);
this.conObj.MouseUp += new MouseEventHandler(ConObj_MouseUp);
}

public winMove(Control conObj,int _rectLeft,int _right)
{
this.rectLeft = _rectLeft;
this.rectRight = _right;
this.conObj = conObj;
this.conObj.MouseDown += new MouseEventHandler(ConObj_MouseDown2);
this.conObj.MouseMove += new MouseEventHandler(ConObj_MouseMove2);
this.conObj.MouseUp += new MouseEventHandler(ConObj_MouseUp);
}
private void ConObj_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
FormLocation = movedForm.Location;
mouseOffset = Control.MousePosition;
this.conObj.Cursor = System.Windows.Forms.Cursors.SizeAll;
}
}
private void ConObj_MouseDown2(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
conObjLocation = conObj.Location;
mouseOffset = Control.MousePosition;
this.conObj.Cursor = System.Windows.Forms.Cursors.SizeAll;
}
}
private void ConObj_MouseUp(object sender, MouseEventArgs e)
{
this.conObj.Cursor = System.Windows.Forms.Cursors.Default;
isMouseDown = false;
}

private void ConObj_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
int _x = 0;
int _y = 0;
System.Drawing.Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y;
movedForm.Location = new System.Drawing.Point(FormLocation.X - _x, FormLocation.Y - _y);

}
}
private void ConObj_MouseMove2(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
int _x = 0;
int _y = 0;
System.Drawing.Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y;
if (conObjLocation.X - _x < 0 || conObjLocation.Y - _y < 0
|| conObjLocation.X - _x > rectLeft || conObjLocation.Y - _y > rectRight)
{
return;
}
conObj.Location = new System.Drawing.Point(conObjLocation.X - _x, conObjLocation.Y - _y);

}
}
}
}
在form窗体调用:
new winMove(this.Form1, this.panel2); //在panel2区域拖动form窗口//拖动paneShowAbout层,并限制拖动边界new winMove(this.paneShowAbout, (this.Width - this.paneShowAbout.Width), (this.Height - this.paneShowAbout.Height));


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息