日期:2014-05-18 浏览次数:21150 次
int x, y; bool mousedown=false; private void button1_Click(object sender, EventArgs e) { PictureBox pic = new PictureBox(); pic.Image = Image.FromFile("c:\\1.bmp"); pic.Parent = this; pic.MouseDown += new MouseEventHandler ( delegate(object obj, MouseEventArgs ex) { if (ex.Button == MouseButtons.Left) { x = ex.X; y = ex.Y; mousedown = true; } } ); pic.MouseUp += new MouseEventHandler ( delegate(object obj, MouseEventArgs ex) { mousedown = false; } ); pic.MouseMove += new MouseEventHandler ( delegate(object obj, MouseEventArgs ex) { if (mousedown) { ((PictureBox)obj).Left += ex.X - x; ((PictureBox)obj).Top += ex.Y - y; } } ); }
------解决方案--------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
System.Collections.Generic.List<mouselocation> mouse = new List<mouselocation>();
protected override void OnMouseMove(MouseEventArgs e)
{
if (flag)
{
mouselocation ms = new mouselocation();
ms.x = e.X;
ms.y = e.Y;
mouse.Add(ms);
}
Grap();
base.OnMouseMove(e);
}
private void Grap()
{
// Refresh();
Graphics gp = this.CreateGraphics();
Brush bsh = new SolidBrush(Color.Red);
Pen p = new Pen(bsh);
for (int i = 0; i < mouse.Count-1; i++)
{
gp.DrawLine(p,mouse[i].x,mouse[i].y,mouse[i+1].x,mouse[i+1].y);
}
}
public bool flag = false;
private void button1_Click(object sender, Event