Reach data from other event - c#

We can get X and Y points which by mouse move on the picturebox like;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
double Xcoordinate = e.X;
double Ycoordinate = e.Y;
label1.Text = Xcoordinate.ToString();
label2.Text = Ycoordinate.ToString();
}
My question How can I get Xcoordinate and Ycoordinate from other events for ex; MouseClick event or my new defined function?
Actually I want to reach XCoordinate and Ycoordinate parameter from FormLoad. How can I do that?

Use Cursor Position property..
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}

The MouseMove event happens to give you the mouse position. This is not included in other EventArgs. You can always get the mouse positon through Cursor.Position.

The static method Control.MousePosition will get you the absolute position of the mouse pointer on the screen. You can translate it using Control.PointToClient to get the coordinates local to a control of interest.
If I remember correctly, the one caveat is that MouseEventArgs gives you the mouse position as it was when the message was posted to the event loop, while Control.MousePosition gives you the position right now. For most applications, this difference is probably not a big deal.

You can Use this Solution to Get co-ordinate of picture Box at the time of other event
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
}
Or try It also
pictureBox1.MouseClick += (s, e) => MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y));

Related

C# draggable map resets to default after releasing and moving mouse

I have been trying to fix an error for the last week.
I got a panel and inside this panel there is a picturebox (a map).
Whenever you press the mouse button and you move it to somewhere it moves the map, but whenever you release it and press it again and move it, it goes back to the default position of the map (but still dragable).
I need it to be that whenever someone release the button press, and click and move it again it proceed from the position it is currently.
I am sure it has something to with my MouseMove event and I have tried a lot of things and couldn't manage to fix it.
Here are my codes for the MouseUp, MouseDown and MouseMove events.
private bool Dragging;
private Point lastLocation;
private void countryMapImage_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
lastLocation = e.Location;
}
private void countryMapImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
lastLocation = e.Location;
}
}
private void countryMapImage_MouseMove(object sender, MouseEventArgs e)
{
if (Dragging == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
countryMapImage.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);
countryMapImage.Invalidate();
}
}
Would appreciate the help!
I have tried changing some values on the mousemove padding event, mousedown and mouseup events and nothing solved it. I have already tried to look for some answers but couldn't find any that solves the problem.
Try this: When user clicks the Map, capture the location of the cursor (in "screen" coordinates) and also the current location of the Map control. Now, as you move the mouse (and only if LButton is down) calculate a delta of the current cursor position (in "screen" coordinates) relative to the cursor position captured on the MouseDown event. Then, set the new position of the Map control to be the sum of its location that you captured on the MouseDown event plus the delta.
One key to having this work properly is to take the e.Location of the handler events and convert the point to screen coordinates relative to the sender.
public MainForm()
{
InitializeComponent();
countryMapImage.MouseMove += onMapMouseMove;
countryMapImage.MouseDown += onMapMouseDown;
}
Point
// Where's the cursor in relation to screen when mouse button is pressed?
_mouseDownScreen = new Point(),
// Where's the 'map' control when mouse button is pressed?
_controlDownPoint = new Point(),
// How much has the mouse moved from it's original mouse-down location?
_mouseDelta = new Point();
private void onMapMouseDown(object sender, MouseEventArgs e)
{
if (sender is Control control)
{
_mouseDownScreen = control.PointToScreen(e.Location);
Text = $"{_mouseDownScreen}";
_controlDownPoint = countryMapImage.Location;
}
}
private void onMapMouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Equals(MouseButtons.Left))
{
if (sender is Control control)
{
var screen = control.PointToScreen(e.Location);
_mouseDelta = new Point(screen.X - _mouseDownScreen.X, screen.Y - _mouseDownScreen.Y);
Text = $"{_mouseDownScreen} {screen} {_mouseDelta}";
var newControlLocation = new Point(_controlDownPoint.X + _mouseDelta.X, _controlDownPoint.Y + _mouseDelta.Y);
if(!countryMapImage.Location.Equals(newControlLocation))
{
countryMapImage.Location = newControlLocation;
}
}
}
}
The MouseUp event shouldn't need to be handled in this simple scenario.

How we move a label when mouse click on it and when mouse key up then stop moving inside a group box

When is try with code, there appear two label and when move, screen become white from where they move. I want single label move with mouse move.
bool mDown = false;
private void label13_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
label13.Location = e.Location;
}
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
The e.Location gives you a mouse position relative to the control that is being clicked. So to fix that, instead of
label13.Location = e.Location;
use
var pos = this.PointToClient(Cursor.Position);
label13.Location = new Point(pos.X - offset.X, pos.Y - offset.Y);`
Create the offset variable as a property of the form (type Point) and initialize it on the mouse down event:
offset = e.Location;

Get number of pixels between two points in an Image

I am working on a project which requires a number of pixels between two points which are horizontal in an image. I am doing this in a windows application form.
Basically when a user clicks on one point in image and then on other point then he should get horizontal distance in form on number of pixels.
I am not getting any idea to do it.
please help.
For an image container, I am just using a pictureBox, though this would work with a label or whatever control you want.
Outside of any function:
private Boolean clicked_once = false;
private Point point1;
private Point point2;
And then I added a MouseClick event (not a Click event) :
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (clicked_once == false)
{
clicked_once = true;
point1 = e.Location;
}
else if (clicked_once == true)
{
clicked_once = false;
point2 = e.Location;
int distance = Math.Abs(point1.X - point2.X);
MessageBox.Show("Distance of pixels horizontally: " + distance.ToString());
}
}
Should work.
Assuming your image is displayed using any control descended from Control then you will have access to the Control.MouseClick event (link).
This event uses MouseEventArgs (link), which has a property X.
Should be pretty clear from that point.
EDIT: Added this very simple example:
private int? x1;
private void MyImageControl_MouseClick(object sender, MouseEventArgs e)
{
if (x1.HasValue)
{
MessageBox.Show("Difference of " + Math.Abs(e.X - x1.Value).ToString());
x1 = null;
}
else
{
x1 = e.X;
}
}

how to Make the Mouse Freeze c#

i want the mouse to freez (cant move) when mouse down
thanks
I used a tableLayoutPanel for your reference (Just remember to implement the code to the Control that is in the front):
OPTION1: Reset the mouse position:
Define two global variables:
bool mousemove = true;
Point currentp = new Point(0, 0);
Handel MouseDown Event to update mousemove :
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
int offsetX = (sender as Control).Location.X + this.Location.X;
int offsetY = (sender as Control).Location.Y + this.Location.Y;
mousemove = false;
currentp = new Point(e.X+offsetX, e.Y+offsetY); //or just use Cursor.Position
}
Handel MouseMove to disable/enable move:
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mousemove)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = currentp;
}
}
Reset mousemove while Mouseup
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
mousemove = true;
}
OPTION2: Limit mouse clipping rectangle:
Limit it while MouseDown:
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = new Rectangle(Cursor.Position, new Size(0, 0));
}
Release it after MouseUp:
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = Screen.PrimaryScreen.Bounds;
}
You can't.
Mouse acts in the OS Layer, not your app... even if you freeze your app, mouse will be able to run.
You can try to disconnect the mouse driver/port but you do need to ask the user what port the mouse is using as for the OS it's a Input device, just like a pen in a design board and you will not know the one to disconnect.
It's possible, Windows has a dedicated API for it, BlockInput(). Be sure to save all your work when you experiment with it, it is rather effective. You may need to reboot your machine, the thing your user will do when you use it in a program. Here's a sample Windows Forms form that uses it, it needs a button and a timer:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 3000;
timer1.Tick += new EventHandler(timer1_Tick);
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e) {
timer1.Enabled = true;
BlockInput(true);
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
BlockInput(false);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool BlockInput(bool block);
}
You can fake that behavior for your window in the following way:
Remember current cursor and its position.
Set
this.Cursor = Cursors.None;
Draw the remembered cursor at specified position and introduce canExecute flag for all your mouse handlers to disable them during "fake mouse freezing".
Can't you move the mouse pointer somewhere? You could reset its position when moving (which may look ugly).
Setup a low level mouse hook with SetWindowsHookEx and ignore all messages to the HOOKPROC delegate you specified (means not to call CallNextHookEx).

How can I move windows when Mouse Down

we able to move windows forms when we mouse down on title bar .
but how can I move windows when mouse down in form ?
You'll need to record when the mouse is down and up using the MouseDown and MouseUp events:
private bool mouseIsDown = false;
private Point firstPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstPoint = e.Location;
mouseIsDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
As you can see, the first point is being recorded, so you can then use the MouseMove event as follows:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
// Get the difference between the two points
int xDiff = firstPoint.X - e.Location.X;
int yDiff = firstPoint.Y - e.Location.Y;
// Set the new point
int x = this.Location.X - xDiff;
int y = this.Location.Y - yDiff;
this.Location = new Point(x, y);
}
}
You can do it manually by handling the MouseDown event, as explained in other answers. Another option is to use this small utility class I wrote some time ago. It allows you to make the window "movable" automatically, without a line of code.
Listen for the event when the mouse button goes down in the form and then listen for mouse moves until it goes up again.
Here's a codeproject article that shows how to do this: Move window/form without Titlebar in C#
You can't use location provided in MouseUp or Down, you should use system location like this
private Point diffPoint;
bool mouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//saves position difference
diffPoint.X = System.Windows.Forms.Cursor.Position.X - this.Left;
diffPoint.Y = System.Windows.Forms.Cursor.Position.Y - this.Top;
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Left = System.Windows.Forms.Cursor.Position.X - diffPoint.X;
this.Top = System.Windows.Forms.Cursor.Position.Y - diffPoint.Y;
}
}
This works, tested.

Categories