That's certainly possible. I'm doing something like that in an app I'm writing, and I'll try to formulate an answer you can actually use.
In my case, I created a custom control derived from the
Panel
control. I then overrode the Paint method, and put code in to draw an image in the specified location, and the MouseMove method to actually change the specified position. Here's most of the Mouse handler code as it exists in my code (keep in mind that you WILL have to massage it to fit your own needs, especially the painting part):
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && m_weapon != null)
{
m_moving = true;
}
base.OnMouseDown(e);
}
protected override void OnMouseEnter(EventArgs e)
{
if (m_weapon != null)
{
Cursor = Cursors.SizeAll;
}
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
Cursor = Cursors.Default;
base.OnMouseLeave(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
m_moving = false;
}
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (m_moving)
{
m_origin.X = e.Location.X - m_offset.X;
m_origin.Y = e.Location.Y - m_offset.Y;
m_bmpRect.X = m_origin.X;
m_bmpRect.Y = m_origin.Y;
Refresh();
}
else
{
m_offset.X = e.Location.X - m_bmpRect.X;
m_offset.Y = e.Location.Y - m_bmpRect.Y;
}
base.OnMouseMove(e);
}
The applicable paint hander code looks like this:
protected override void OnPaint(PaintEventArgs e)
{
Rectangle imgRect = new Rectangle(m_origin.X, m_origin.Y, m_weapon.Width, m_weapon.Height);
e.Graphics.DrawImage(m_weapon, imgRect, 0, 0, m_weapon.Width, m_weapon.Height, GraphicsUnit.Pixel, imageAttributes);
base.OnPaint(e);
}
Google is a great resource, btw.