The point I need help is that when I try to do a click event, a different point of a picture box object should be positioned, not the top left corner.
Is there a way to change the reference moving point of an object?
Example:
The point shown as "Yellow Circle" is the reference point where the "pictureBoxRed" object is located in any mouse click event.
The point shown as "Black Star" is a point indicating the position to be clicked for example.
If the sample point shown as "Black Star" is clicked with the left cursor, it must be moved from the "Yellow Circle" point of the "pictureBoxRed" object, not the upper left corner point of the "pictureBoxRed" object.
Thanks for your aid.
Here Example Gif Animation About This Problem
private void Exmp(object sender, MouseEventArgs e)
{
GeneralClick(e.Button, e.X, e.Y);
}
PictureBox picRed = new PictureBox();
PictureBox picBlue = new PictureBox();
private void GeneralClick(MouseButtons sender, int X, int Y)
{
if (sender == MouseButtons.Left)
{
picRed.Location = new Point(X, Y);
var arrayer = this.PointToScreen(picRed.Location);
arrayer = picBlue.PointToClient(arrayer);
picRed.Parent = picBlue;
}
}
public Form1()
{
InitializeComponent();
picBlue.MouseClick+= new MouseEventHandler(Exmp);
}
Related
I'm working on a simple practice: Double click to create a textBox on mouse location (X,Y)
It is creating the object, but far from the mouse exact position.
private void DynamicObjects_MouseDoubleClick(object sender, MouseEventArgs e)
{
System.Windows.Forms.TextBox txtBox = new System.Windows.Forms.TextBox();
this.Controls.Add(txtBox);
txtBox.Top = MousePosition.Y;
txtBox.Left = MousePosition.X;
//txtBox.Location = MousePosition; --Still off away from Mouse real location
//txtBox.Location = MousePosition.Y; -- Fives erro Cannot implicitly convert Int to 'System.Drawing.Point'
}
Only way I've found to short of work is using .Top and .Left.
Why is creating far from mouse?
MousePosition gets the position of mouse cursor in screen coordinates.
We have to translate it to the client coordinates.
var textBox = new TextBox();
textBox.Location = PointToClient(MousePosition);
Controls.Add(textBox);
We need to call this method from the same control that we will place the TextBox on.
For example:
this.PointToClient
this.Controls.Add
or
panel.PointToClient
panel.Controls.Add
I am using a pictureBox to move 2 linear stages; when the mouseDown event triggers, the pictureBox coordinates are remapped to match the maximum travel length of the axis, and then sent to them to perform the movement.
To improve this feature i have added a tiny dot on this image to track the current position of the mouse during the mouseDown event.
the dot must update its positition everytime the mouse moves; in order to do so i have used gfx.Clear(Color.White); to delete the previous and draw the new one.
Problem is that to understand the correct positioning of the axis the pictureBox should show a photo of the axis; but calling the gfx.Clear(Color) clears the image and leaves me with a white background.
is there a way to update the dot position without calling the gfx.Clear (in order to keep the image?)
if (e.Button.Equals(MouseButtons.Left))
{
{
this.gridImage.Refresh();
convertedX = (e.X * 100) / gridImage.Size.Width;
convertedY = (e.Y * 100) / gridImage.Size.Height;
using (Graphics gfx = Graphics.FromImage(this.gridImage.Image))
{
circle_bounds.X = e.X;
circle_bounds.Y = e.Y;
gfx.Clear(Color.White);
gfx.DrawEllipse(Pens.Red, this.circle_bounds);
}
Console.WriteLine("(X,Y): " + convertedX.ToString() + " " + convertedY.ToString());
Thread.Sleep(20);
//moveAbs(port1, "1", convertedX.ToString());
//moveAbs(port2, "1", convertedY.ToString());
initialXText.Text = convertedX.ToString();
initialYText.Text = convertedY.ToString();
}
}
What i would do is using the PictureBox.Paint event to draw the point that must follow the mouse move. First I declare a Pointto store the mouse position any time it moves:
Point mousePosition;
Then, in the PictureBox.MouseMove event handler, I would store this location and invalidate the PictureBox:
private void gridImage_MouseMove(object sender, MouseEventArgs e)
{
mousePosition = e.Location;
pictureBox1.Invalidate();
}
Finally, in the PictureBox.Paint i just draw a circle using the mouse position:
private void gridImage_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Red, new Rectangle(mousePosition, new Size(5,5)));
}
Hope this leads you in the right direction
I'm trying to implement the Brushing function to my Paint project.
My idea is everywhen I move and press mouse left button on canvas, I'll add a ellipse to Free-brush (like MSPaint)
Everything was good until I move mouse faster. >> the brush print is separated.
Can anyone explain to me and give me some hints to solve this?
Here is my code:
Point _startPoint, _endPoint;
private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(MyCanvas);
Ellipse MyBrush_Ellipse = new Ellipse();
MyBrushing.CreateBrush(_cl1, _cl2, ref MyBrush_Ellipse, Mybrush_type);
Canvas.SetTop(MyBrush_Ellipse, _startPoint.Y);
Canvas.SetLeft(MyBrush_Ellipse, _startPoint.X);
}
private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
{
_endPoint = e.GetPosition(MyCanvas);
if (e.LeftButton == MouseButtonState.Pressed)
{
Ellipse MyBrush_Ellipse = new Ellipse();
MyBrushing.CreateBrush(_cl1, _cl2, ref MyBrush_Ellipse, Mybrush_type);
Canvas.SetTop(MyBrush_Ellipse, _endPoint.Y);
Canvas.SetLeft(MyBrush_Ellipse, _endPoint.X);
MyCanvas.Children.Add(MyBrush_Ellipse);
MyCanvas.CaptureMouse();
_myUndoRedo.PushToStackForBrush(MyBrush_Ellipse);
}
}
private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MyCanvas.ReleaseMouseCapture();
}
The mouse is not moving constantly over the screen. When moving faster from A to B the mouse actually does not neccessarily move over every part of the screen in between.
To solve your problem draw a line instead of drawing an ellipse at the mouse's position. Therefore you have to store the point the mouse has been before. After a movement draw a line from the stored, last known point to the actual point.
In my program I added this code so when I move my mouse all over the screen I will get the mouse cursor coordinates in real time:
Form1 Load:
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
t1.Interval = 50;
t1.Tick += new EventHandler(timer1_Tick);
t1.Enabled = true;;
}
Then the method that get the mouse position:
public static Point GetMousePosition()
{
var position = System.Windows.Forms.Cursor.Position;
return new Point(position.X, position.Y);
}
Then the timer1 tick event:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = string.Format("X={0}, Y={1}", GetMousePosition().X, GetMousePosition().Y);
}
Then I ran some application and moved the mouse over a specific location on the screen where the application window is and I found this coordinates:
358, 913
Now I have in my program a listBox with items each item present application screenshot. And if I click on the pictureBox for example in this case on the BATTLEFIELD 3 area I get the mouse cursor coordinates according to the pictureBox area.
So I did:
Point screenCoordinates;
Point pictureBoxSnapCoordinates;
private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
screenCoordinates = pictureBoxSnap.PointToScreen(e.Location);
pictureBoxSnapCoordinates = e.Location;
}
Now when I click in the pictureBox at the same location as I found the coordinates 358, 913 but on the pictureBox so the results are:
screenCoordinates 435, 724
pictureBoxSnapCoordinates 23,423
The screenCoordinates isn't the same coordinates as I found with the mouse move 358, 913 it's not even close. There is a big difference between 358,913 and 437,724
e.Location is relative to the Control's top left corner. If you want to use e.Location to get the screen coordinates, then you have to first do pictureBoxSnap.PointToScreen(Point.Empty); and then offset by the e.Location.
Also, Cursor.Position returns a Point object, so making a new Point(...) is pointless.
I must add, if you are dealing with images, and you need to interact with mouse, and do any task related with offset, scroll, etc, I recommend you that library, it is open source and have a lot of examples and methods that will help you
https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox
I have a function that sets the location of a certain textbox to the location of mouse cursor whenever the dragover event is called.
private void DGVLogicSimView_DragOver(object sender, DragEventArgs e)
{
txtBoxDragPoint.Visible = true;
txtBoxDragPoint.BackColor = Color.LightSkyBlue;
txtBoxDragPoint.Location = new Point(e.X, e.Y);
e.Effect = DragDropEffects.Copy;
}
The above event works perfectly when the form is maximized. However, when the form is not maximized and located somewhere random in the desktop, the txtbox location gets all messed up.
I believe it is returning the mouse location relative to the form, not the screen. What is the best solution for this?
Yes, that's because the D+D events deliver the mouse position in screen coordinates, not in client coordinates. You'll need to map the position relative to the textbox' parent, like this:
txtBoxDragPoint.Location = txtBoxDragPoint.Parent.PointToClient(new Point(e.X, e.Y));
Coordinates are indeed not screen-relative.
You can try changing this:
txtBoxDragPoint.Location = new Point(e.X, e.Y);
to this (assuming DGVLogicSimView is the name of the control you're hovering)
txtBoxDragPoint.Location = new Point(DGVLogicSimView.Left + e.X, DGVLogicSimView.Top + e.Y);