|
返信ありがとうございます。
ご指定いただいたサイトを見て色々やってみましたがうまくいきませんでした。
ソースを載せさせて頂きますので、よろしくお願いいたします。
class ControlMoveHelper
{
private Control control;
private bool isDragged = false;
private bool doAction = true;
public bool DoAction
{
set { this.doAction = value; }
get { return this.doAction; }
}
private Point point = new Point();
private int margin;
public ControlMoveHelper(Control control, int margin)
: this(control, null, margin){}
public ControlMoveHelper(Control control, Control[] eventControls, int margin)
{
this.control = control;
this.margin = margin;
this.control.MouseDown += new MouseEventHandler(control_MouseDown);
this.control.MouseUp += new MouseEventHandler(control_MouseUp);
this.control.MouseMove += new MouseEventHandler(control_MouseMove);
if (eventControls != null)
{
foreach (Control eventControl in eventControls)
{
eventControl.MouseDown += new MouseEventHandler(control_MouseDown);
eventControl.MouseUp += new MouseEventHandler(control_MouseUp);
eventControl.MouseMove += new MouseEventHandler(control_MouseMove);
}
}
}
/// <summary>
/// マウスが動かされた場合の処理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void control_MouseMove(object sender, MouseEventArgs e)
{
if (!this.doAction) { return; }
if (this.isDragged)
{
//Point point = this.control.PointToScreen(new Point(e.X, e.Y));
Point point = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
this.control.Top += point.Y - this.point.Y;
this.control.Left += point.X - this.point.X;
//this.control.Top += e.Y - this.point.Y;
//this.control.Left += e.X - this.point.X;
}
else
{
if (this.IsRange(e))
{
this.control.Cursor = Cursors.SizeAll;
}
}
}
/// <summary>
/// マウスがアップした場合の処理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void control_MouseUp(object sender, MouseEventArgs e)
{
if (!this.doAction) { return; }
this.isDragged = false;
}
/// <summary>
/// マウスがダウンされた場合の処理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void control_MouseDown(object sender, MouseEventArgs e)
{
if (!this.doAction) { return; }
if (this.IsRange(e))
{
//this.point.X = e.X;
//this.point.Y = e.Y;
//this.point = this.control.PointToScreen(new Point(e.X, e.Y));
this.point = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
this.isDragged = true;
}
}
private bool IsRange(MouseEventArgs e)
{
Rectangle range = new Rectangle(this.margin, this.margin, this.control.Width - this.margin * 2, this.control.Height - this.margin * 2);
return range.Contains(e.Location);
}
}
|