Hi Everyone,
I have this:
[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace DrawingFigureWithMouse
{
public partial class Form1 : Form
{
private Point[] vertices = {new Point(50,100), new Point(100,50), new Point(150,100)};
private GraphicsPath p = new GraphicsPath();
private Region region;
private int oldX = 100;
private int oldY = 75;
private int x = 0;
private int y = 0;
private PictureBox circle = new PictureBox();
private Bitmap circleShape = new Bitmap(Properties.Resources.circle);
private Point _offset = Point.Empty;
private bool inside = false;
private bool drag = false;
public Form1()
{
InitializeComponent();
Size = new Size(400,400);
Text = "Moving shape with mouse";
BackColor = Color.White;
p.AddPolygon(vertices);
region = new Region(p);
circle.Size = new Size(200, 200);
circle.Visible = true;
circle.Location = new Point(40 + circle.Width, 150);
circle.Image = Properties.Resources.circle;
circle.MouseDown += new MouseEventHandler(circle_MouseDown);
circle.MouseUp += new MouseEventHandler(circle_MouseUp);
circle.MouseMove +=new MouseEventHandler(circle_MouseMove);
Controls.Add(circle);
}
private void circle_MouseDown(object sender, MouseEventArgs e)
{
drag = true;
x = e.X;
y = e.Y;
}
private void circle_MouseUp(object sender, MouseEventArgs e)
{
_offset = Point.Empty;
//drag = false;
}
private void circle_MouseMove(object sender, MouseEventArgs e)
{
/*
if (_offset != Point.Empty)
{
Point newLocation = this.Location;
circle.Left += e.X - x;
circle.Top += e.Y - y;
}
*/
if (drag)
{
circle.Left += e.X -x;
circle.Top += e.Y -y;
x = e.X;
y = e.Y;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRegion(Brushes.Blue,region);
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if(region.GetBounds(CreateGraphics()).Contains(e.X, e.Y))
{
oldX = e.X;
oldY = e.Y;
//pictureBox1.Width = e.X;
// pictureBox1.Height = e.Y;
inside = true;
}
base.OnMouseDown(e);
}//End method
protected override void OnMouseUp(MouseEventArgs e)
{
if (inside)
{
region = region.Clone();
region.Translate(e.X - oldX, e.Y - oldY);
oldX = e.X;
oldY = e.Y;
inside = false;
Invalidate();
}
base.OnMouseUp(e);
}
}
}
[/code]
I try to move the green circle with the mouse but there is a lot of flickering.
THX for helping!!
Loading
Hemant KumarPosted Oct 13, 2011, 4:17 AM
The
e.Xande.Yare relative to the picture box (e.g. if the mouse is in the upper left of the picture box, that's 0,0) .The values for
imagenMapa.LeftandimagenMapa.Topare relative to the form (or whatever control containsimagenMapa)If you try to mix values from these two systems without conversion, you're going to get jumps (like you're seeing).
You're probably better off converting the mouse position to the same coordinate system used by the thing that contains the picture box.
You could use
imagenMapa.PointToScreento get the mouse coordinates in screen coordinates (orCursor.Positionto get the position directly), andyourForm.PointToClientto get them back in the form coordinates.Note that depending on your needs, you could accomplish "moving an image within a control" by overriding/handling the
Paintevent of a control and drawing the image yourself. If you did this, you could keep everything in the picturebox coordinates, since those are likely what you would use when you calledgraphicsObject.DrawImage.