get the position of picturebox that has been clicked - c#

I want to get the position of the picturebox that has been cliked by the mouse,but i don't know how??
I mean the position of picturebox not the form that the picturebox on it.
thanks.

MUGAN's close. The Point you'll get from MouseEventArgs is the "screen" point of the mouse, where 0,0 is the top left of the entire monitor or desktop (however you want to think of it). To convert that to a "client" point within the PictureBox control, where 0,0 is the top left of that PictureBox, you'll need to use the Control.PointToClient() method:
private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = (Control)sender.PointToClient(new Point(e.X, e.Y));
//here goes your if condition ...
}

private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
//here goes your if condition ...
}

Related

WinForm not dragging smoothly when moving by client area

Whenever I move a Windows Form by some component (i.e. a Label) in the client area, I end up with a strange mouse offset in which the form does not stay visually underneath the cursor. It will still move according to my mouse location on the screen, but it dramatically shifts southeast of the cursor's position.
I've had to specify a negative offset of my own to counteract this offset; my code is as follows:
private void component_MouseDown(object sender, MouseEventArgs e)
{
if (sender is Label)
{
if (e.Button == MouseButtons.Left)
{
mouseLoc = new Point(-(e.X + OFFSET_X), -(e.Y + OFFSET_Y));
isMouseDown = true;
}
}
}
private void component_MouseMove(object sender, MouseEventArgs e)
{
if (isTitleLabelMouseDown)
{
Point p = Control.MousePosition;
p.Offset(mouseLoc);
Location = p;
}
}
private void component_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
This offset does fix the problem, but what throws me for a loop is why the form's location offsets when I move it by its client area in the first place?
Thanks!
You seem to be translating client coordinates to screen coordinates. There is a better way...
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen%28v=vs.110%29.aspx
Edit: And of course there's a better way to do this whole thing. Basically, you want to intercept the click higher up the chain and tell Windows that the click is actually in the window title, which will cause Windows to do the dragging for you...
Winforms - WM_NCHITEST message for click on control

Unable to set the panel location to the current mouse position inside splitContainer?

I have panel1 in my form, i set the visible property to panel1.Visible=false; I want to show this panel wherever i click on the screen.
I need to grab the current mouse location and then display the panel1 where the top-left corner must be in the same point as the mouse cursor !
Sorry for being so beginner but i really stuck on how to do it.
Code that i tried :
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
panel1.Location = e.Location;
panel1.Show();
}
It might this will be your guide to your task, just use the .PointToScreen and .GetCellDisplayRectangle Method
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == -1) return;
var cellRectangle = dataGridView1.PointToScreen(
dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);
panel1.Location = new Point(cellRectangle.X + 50, cellRectangle.Y - 175);
panel1.Show();
}
As far I can recognize your problem you should use PointToScreen function - more on this here

Find the mouse location over a bitmap control and display a rectangle in c#

I want to find the exact mouse location of the panel's display rectangle. I have a panel and assigned a bitmap image on it. I searched long time and found control over mouse location option but it won't fit. Anyone help me in this.
You can use the MouseMove event and exploit the MouseEventArgs:
//panel.MouseMove += new MouseEventHandler(panel_MouseMove);
void panel_MouseMove(object sender, MouseEventArgs e)
{
Point myLocation = e.Location;
//Or e.X and e.Y
}
You can use MouseMove event:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = String.Format("({0};{1})", e.X, e.Y);
}
You can use e.Location or splitted values e.X and e.Y.

Drawing and deleting old drawings

I'm new in this forum, and my english is not perfect, so i want to excuse if my question isn't writen good.
I'm making a painting program with C# and all is perfect expect one problem.
When i'm drawing a line, or rectangle or elipse, when the mouseMove event is called the old shapes is drawed too.
How to draw a shape with mouseMove event and the old shapes to not been drawed.
Here is part of my code for more clarification.
//---Variables declared by the Prgrammer---//
//'parent' is variable that take the MdiParent
//'mouseIsDown' is boolean variable
//'startPoint' and 'endPoint' are Point Varables
//'pen' is Pen variable that is configured eralier
//'graphic' is pictureBox in the same form.
private void pbx_MouseDown(object sender, MouseEventArgs e)
{
if (parent.btnLine.Checked)
{
mouseIsDown = true;
startPoint = new Point(e.X, e.Y);
}
}
private void pbx_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown == true && parent.btnLine.Checked)
{
pen = new Pen(parent.btnPreview.BackColor, 12);
endPoint = new Point(e.X, e.Y);
graphic.DrawLine(pen, startPoint, endPoint);
}
}
private void pbx_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
Please help me. This is a big problem for me.
Thanks.
Please help me, i'm waiting 2 days.
When i'm making a new drawing to the graphic object, the old graphic is deleted.
I try the graphic.Save() method, but has not give me right result.
How to make the drawings to saty when i'm making another graphic?
As you're using picturebox you can use Invalidate() method.

how to move a label on a winform at Runtime

using this event the label just disappears, how shod i do this?
private void label4_MouseMove(object sender, MouseEventArgs e)
{
label4.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
handle these three event ...
Control actcontrol;
Point preloc;
void label1_Mousedown(object sender, MouseEventArgs e)
{
actcontrol = sender as Control;
preloc = e.Location;
Cursor = Cursors.Default;
}
void label1_MouseMove(object sender, MouseEventArgs e)
{
if (actcontrol == null || actcontrol != sender)
return;
var location = actcontrol.Location;
location.Offset(e.Location.X - preloc.X, e.Location.Y - preloc.Y);
actcontrol.Location = location;
}
void label1_MouseUp(object sender, MouseEventArgs e)
{
actcontrol = null;
Cursor = Cursors.Default;
}
The location of label4 is relative to the container (Form or parent control), Cursor position may be relative to the screen.
You need to adjust the location. For example, if the container is the Form you can find its location in the screen and calculate by it the location of the cursor relative to screen.
This is only one possibility for the cause, but this one is happens a lot :)
Use the form's PointToClient() function to translate the mouse X/Y coordinates into points that are relative to your form, that should do it.
Edit: Use the mouse event args object properties instead:
Label1.Location = New Point(e.X, e.Y)
PS pardon the VB, no C# on this PC
The location of an element is relative to its parent. In this case though you are using the absolute mouse position as its location.
You'll need to translate the mouse position into the coordinate system of the parent element.
Use the PointToClient method on the label's parent element.

Categories

Resources