我在编写一个地图编辑器,就类似于小时候玩的坦克大战的地图编辑器,我想编一个可以拖拽不同地形块的功能,就是像可视化编程的控件工具栏中拖拽一个按钮,文本框之类的东西可以直接放在窗口上一样,
我用C#编写的时候碰到一个问题,按住左键拖动一个地形图标,在鼠标松开之前不能显示我拖拽的东西,只有当鼠标放下的时候才显示,请教这是为什么,如何解决,谢谢!
我用C#编写的时候碰到一个问题,按住左键拖动一个地形图标,在鼠标松开之前不能显示我拖拽的东西,只有当鼠标放下的时候才显示,请教这是为什么,如何解决,谢谢!
{
if (e.Button != MouseButtons.Left) return;
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
int x = p.X / 30; //图片大小是30*30
int y = p.Y / 30;
addPictureBox(x * 30, y * 30); //在计算好的坐标点绘制这个图片
} private void Map_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
int x = p.X / 30; //图片大小是30*30
int y = p.Y / 30;
addPictureBox(x * 30, y * 30); //在计算好的坐标点绘制这个图片
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MoveControl
{
/// <summary>
///使窗口的中的指定控件支持运行时移动
/// TODO:运行时缩放
/// </summary>
public class ControlMoveResize
{
#region 私有成员
bool IsMoving = false;
Point pCtrlLastCoordinate = new Point(0, 0);
Point pCursorOffset = new Point(0, 0);
Point pCursorLastCoordinate = new Point(0, 0);
private Control ctrl = null;
private ScrollableControl Containe = null;
#endregion #region 私有方法
/// <summary>
///在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MouseDown(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
}
if (e.Button == MouseButtons.Left)
{
IsMoving = true;
pCtrlLastCoordinate.X = ctrl.Left;
pCtrlLastCoordinate.Y = ctrl.Top;
pCursorLastCoordinate.X = Cursor.Position.X;
pCursorLastCoordinate.Y = Cursor.Position.Y;
}
}
private void MouseMove(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
} if (e.Button == MouseButtons.Left)
{
if (this.IsMoving)
{
Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y); pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X; pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
}
}
} private void MouseUp(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
}
if (this.IsMoving)
{
if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
{
return;
}
if ((pCtrlLastCoordinate.X + pCursorOffset.X + ctrl.Width) > 0)
{
ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
}
else
{
ctrl.Left = 0;
}
if ((pCtrlLastCoordinate.Y + pCursorOffset.Y + ctrl.Height) > 0)
{
ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
}
else
{
ctrl.Top = 0;
}
pCursorOffset.X = 0;
pCursorOffset.Y = 0;
}
}
#endregion #region 构造函数
/// <summary>
///获取被移动控件对象和容器对象
/// </summary>
/// <param name="c">被设置为可运行时移动的控件</param>
/// <param name="parentContain">可移动控件的容器</param>
public ControlMoveResize(Control c, ScrollableControl parentContain)
{
ctrl = c;
this.Containe = parentContain;
ctrl.MouseDown += new MouseEventHandler(MouseDown);
ctrl.MouseMove += new MouseEventHandler(MouseMove);
ctrl.MouseUp += new MouseEventHandler(MouseUp);
}
#endregion
}
}
-----------------
调用:
private ControlMoveResize clmr = null;
public Form1()
{
InitializeComponent();
clmr = new ControlMoveResize(this.button1, this);
}