Set location of mouse - c#

I cannot fix my mistake : /
if (Cursor.Position = new Point(x, y)
I have an error on x,y, and I want to change to 50,99 for example

First of all you should trig mouseClick event. It's quite simple:
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
Then you should handle the event. Suppose your desired position is (x,y)=(100,100):
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
int xDesired = 100;
int yDesired = 100;
if (e.X == xDesired && e.Y == yDesired)
MessageBox.Show("Certain point clicked.");
}

Related

Dragging of the form moves the form down

I have a Windows Form, My task is to add drag function to the form. "VendorMasterList" is a label. I have added mouse move, mouse down and mouse up events to that label.
If i tried to drag, the form moves down and then only able to drag the way i want. My question is why its going down?This is my code
private Point startPoint = new Point(0, 0);
private bool isDragging = false;
private void lblHeader_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true; // _dragging is your variable flag
startPoint = new Point(e.X, e.Y);
}
private void lblHeader_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void lblHeader_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point p = PointToScreen(e.Location);
this.Location = new Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);
}
}
Is there any way to fix this?
Newly added image
I haven' tried your code, but what I see is only partially correct :-)
You save the initial coordinate, which is relative to the upper left corner of the control that's clicked, but you need to save screen coordinates.
Then the mouse is moved and again you get a point relative to that corner. You're missing a few relevant things:
You need to calculate a delta between the two points based on the screen coordinates, otherwise moving the window underneath the cursor gives you wrong values.
You need to add this delta to the form's current location
You need to save the new mouse position to calculate the next delta correctly
The current mouse position based on screen coordinates can be obtained through Cursor.Position.
So your code should read:
private void lblHeader_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true; // _dragging is your variable flag
startPoint = Cursor.Position;
}
private void lblHeader_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void lblHeader_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point p = Cursor.Position;
int deltaX = p.X - startPoint.X;
int deltaY = p.Y - startPoint.Y;
startPoint = p;
this.Left += deltaX;
this.Top += deltaY;
}
}
I just placed a panel on a form and implented the above, which worked.
#IInspector is right when he says that this approach is actually not very good, as it doesn't take into account that Windows can actually handle all this for you.
An alternative approach (and the better one) I'd take is:
Override the WndProc method as shown below
Done. No more mouse handling
The following is an example of how you could override WndProc to do what you need:
protected override void WndProc(ref Message m)
{
const UInt32 WM_NCHITTEST = 0x0084;
const UInt32 HTCAPTION = 0x2;
bool handled = false;
if (m.Msg == WM_NCHITTEST)
{
if (<cursor is within the caption area>)
{
m.Result = (IntPtr)HTCAPTION;
handled = true;
}
}
if (!handled)
base.WndProc(ref m);
}
To restrict the area in which the window can be moved, you could use the following. Of course, #IInspectable will disagree, but you went this road :-)
private void lblHeader_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point p = Cursor.Position;
int deltaX = p.X - startPoint.X;
int deltaY = p.Y - startPoint.Y;
startPoint = p;
int fX = this.Left + deltaX;
int fY = this.Top + deltaY;
if (fX >= 0 && fX + this.Width < Area.Width) this.Left = fX;
if (fY >= 0 && f> + this.Height < Area.Height) this.Top = fY;
}
}

Drag and Drop something

I made a class Schalter (eng. switch) and now I want to drag and drop this to an other position. The Schalter is a just an object with 0 or 1 as output and it has some drawing in it. I tried something but it just worked half. When I move it it moves much too fast.
Here the code I tried:
namespace Schaltungszeichner {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
s = new Schalter(this);
this.DoubleBuffered = true;
}
private bool myMouseDown = false;
private int myMouseX, myMouseY;
Schalter s;
private void Form1_Paint(object sender, PaintEventArgs e) {
s.zeichnen(e.Graphics);
}
private void Form1_MouseMove(object sender, MouseEventArgs e) {
if (myMouseDown) {
s.X += e.X - myMouseX;
s.Y += e.Y - myMouseY;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
myMouseDown = false;
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
if (s.isClicked(e.X, e.Y)) {
s.Out = !s.Out;
myMouseDown = true;
myMouseX = e.X;
myMouseY = e.Y;
}
}
}
}
The problem is that MouseEventArgs holds the mouse position relative to the object which owns the event and not the object being moved. You need to account for this by subtracting the starting position of the moving object.
In Form1_MouseDown change:
myMouseX = e.X;
myMouseY = e.Y;
to:
myMouseX = e.X - s.X;
myMouseY = e.Y - s.Y;
And in Form1_MouseMove change:
s.X += e.X - myMouseX;
s.Y += e.Y - myMouseY;
to:
s.X = e.X - myMouseX;
s.Y = e.Y - myMouseY;
I would also consider renaming myMouseX and myMouseY to something that reflects the values that they now hold, like differenceX and differenceY.

Stop dragging picturebox c #

I have a picturebox and movement just right and to the left, but when moving it to the left or right edge of the picturebox appears in the middle of the form , as the picture below
This thus
what I want is when moving left or right when the board of picturebox were in the same position of the board form the image can not be more moves , just to the other lado.Conforme the image below
Follows the script:
private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
x = e.X;
//y = e.Y;
}
}
private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
imagemPictureBox.Left += (e.X - x);
//imagemPictureBox.Top += (e.Y - y);
}
}
Try something like this:
// the point in the PB where we grab it:
Point mDown;
void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
// grab the pb
mDown = e.Location;
}
void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
// new position:
int x = imagemPictureBox.Left + e.X - mDown.X;
int y = imagemPictureBox.Top + e.Y - mDown.Y;
// limit to form size:
x = Math.Min(Math.Max(x, 0), this.ClientSize.Width - imagemPictureBox.Width);
y = Math.Min(Math.Max(y, 0), this.ClientSize.Height - imagemPictureBox.Height);
// move the pb:
imagemPictureBox.Location = new Point(x, y);
}
void imagemPictureBox_MouseUp(object sender, MouseEventArgs e)
{
//release it (optional)
mDown = Point.Empty;
}

Modifying all items in a list of Points

List<Point> pointList;
public int pickedIndexRight = -1;
public int diffX = 0;
public int diffY = 0;
public Form1()
{
InitializeComponent();
pointList = new List<Point>();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Point myPoint = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
if (pickedIndex == -1)
{
if (pointList.Contains(myPoint) == false)
{
pointList.Add(myPoint);
}
}
}
else if (e.Button == MouseButtons.Right)
{
//if right click near a point then pickedIndexRight is index of that point in list
pickedIndexRight = pointList.FindIndex(delegate(Point point) { return Distance(point, myPoint) < 10; });
}
Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && pickedIndexRight != -1)
{
for (int i = 0; i < pointList.Count - 1; i++)//(int i = pointList.Count - 1; i > 0; i--)
{
diffX = pointList[i].X + (e.X - pointList[i].X);
diffY = pointList[i].Y + (e.Y - pointList[i].Y);
pointList[i] = new Point(diffX, diffY);
Invalidate();
}
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pickedIndexRight = -1;
Invalidate();
}
private double Distance(Point p1, Point p2) //calculate distance between two points
{
double d = Math.Sqrt((p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y));
return d;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (Point myPoint in pointList)
{
e.Graphics.DrawRectangle(Pens.Black, myPoint.X, myPoint.Y, 1, 1);
}
}
Ok, so I need my app to draw points on a form with every left click - that works just fine. All the points are stored in a list pointList and then the Paint form draws them each one by one.
The thing is, i need the program to have another functionality - moving all points in parallel by dragging one of the points with the right mouse button - I already wrote the function for that but I can't figure out why my code doesn't work properly - it seems to screw up the entire list when i right click.
I'm all out of ideas, I'd be thankful for any hints.
In this line:
diffX = pointList[i].X + (e.X - pointList[i].X);
The pointList[i].X terms cancel out. So it's just:
diffX = e.X;
You're assigning the current mouse position to every point. If you want to move all the points by the distance that the mouse has moved, but keep their positions relative to each other, you need to remember the previous position of the mouse, so you can compare it with the new position. The difference between new and old mouse positions is the correct amount to add to each point.
So add a field such as:
Point oldMousePosition;
And initialise it when the button-down occurs. In each move event:
pointList[i] = new Point(pointList[i].X + (e.X - oldMousePosition.X),
pointList[i].Y + (e.Y - oldMousePosition.Y))

How to call the function?

I need to get the position of the mouse on click in a form, and save the x and y coordinates. I made this simple function:
public void kokot (MouseEventArgs e)
{
x = e.X;
y = e.Y;
this.Invalidate();
}
How can I call it? When I try kokot() it doesn't work of course, because there are no arguments. So what arguments should I use in this case? Thanks in advance for any help.
public Form1()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(Form1_MouseClick);
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
this.Invalidate();
}
Add overload to the function that accept two integers:
public void kokot (int X, int Y)
{
x = X;
y = Y;
this.Invalidate();
}
Then call it like this from anywhere in your code:
Point position = System.Windows.Forms.Cursor.Position;
kokot(position.X, position.Y);
You need to subcsribe to the forms MouseClick Event.
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);

Categories