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
Related
So im trying to get mouse coordinates from a click on an image, and it gives the wrong coordinates. When i move the mouse to draw, the line appears away from the cursor.
This is the code i use to get the mouse coordinates:
private void ponaredek_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseDown = true;
//x1 = System.Windows.Forms.Control.MousePosition;
x1 = new System.Drawing.Point((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
}
x1 is of type System.Drawing.Point (i need the point from drawing, to use in emgucv). What do i have to do to correct the cursor location (i drew where the cursor was)
You want to get the mouse position relative to the Image element, not the Window. So replace
e.GetPosition(this)
by
e.GetPosition((IInputElement)sender)
or
e.GetPosition(ponaredek)
if that is the Image element.
It should look like this:
var pos = e.GetPosition((IInputElement)sender);
x1 = new System.Drawing.Point(pos.X, pos.Y);
Also make sure the Image element's Stretch property is set to None.
I want to show usercontrol as popup at mouse location.
However below code doesn't work for this;
System.Drawing.Point mousePos = System.Windows.Forms.Cursor.Position;
System.Drawing.Point formPos = this.Bounds.Location;
popupChart1.ultraLabel1.Text = e.DataRow.ToString() ;
popupChart1.Location = new System.Drawing.Point(mousePos.X - formPos.X, mousePos.Y - formPos.Y);
popupChart1.Show();
It doesn't throws any error, it just show it self at another place. What could be the reason ?
You should set the location after the form is rendered.
And if you want it to show at your mouse cursor position then simply use Cursor.Position instead of the math you did
popupChart1.Show();
popupChart1.Location = Cursor.Position;
OR
if you want to set the location before you show the window you can do
popupChart1.Location = Cursor.Position;
popupChart1.StartPosition = FormStartPosition.Manual;
popupChart1.Show();
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
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);