im working on code-editor (windows forms application) and I just want to ask how to get tooltip position instead of mouseposition will make it on cursor something like this 1:
Get current cursor lower left position so that tooltip could be displayed properly
specifically this code:
private void panel1_MouseClick(object sender, MouseEventArgs e) {
int x = e.X;
int y = e.Y + Cursor.Current.Size.Height - Cursor.Current.HotSpot.Y;
toolTip1.Show("test", panel1, x, y);
}
but i dont have panel i only got richtextbox[rtb](serve as code editor) and a labelbox[lb](serve as contexthint) .
anyone pls? really in need thanks!
Related
I have a PictureBox that loads me a web page, I need to be able to place the mouse or cursor at certain coordinates within the PictureBox. However, I can't do it, use several ways to get the coordinates but once the web page loads, it doesn't work.
private void m_WebView_MouseClick(object sender, MouseEventArgs e)
{
label1.Text = "X = " + e.X + " ; Y = " + e.Y;
}
private void m_WebView_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = e.Location.X.ToString();
label1.Text = e.Location.Y.ToString();
}
does not work when loading the web page and event is set
If I leave the picture box empty, if it shows me the coordinates
I need to locate the mouse in the exact coordinates that I see
Is it possible?
that its i try coords not work
var simulator = new InputSimulator();
Point position = PointToScreen(pictureBox1.Location) +
new Size(pictureBox1.Location.X / 2 , pictureBox1.Location.Y / 7 );
SetCursorPos(position.X, position.Y);
for #Olivier Jacot-Descombes
works the question is how do I locate the coordinates of a and b without having to adjust the values of
e.Width / 2, pictureBox1.Size.Height / 7));
and start the program close edit open close edit, until I manage to locate them that is my main problem
with this I get coordinates but they don't work
private void m_WebView_MouseMove (object sender, MouseEventArgs e)
{
label1.Text = e.Location.X.ToString ();
label2.Text = e.Location.Y.ToString ();
}
point A = x 106 and Y 106
point B = x 564 and y 225
How do I put those coordinates since they don't work
To place the cursor inside the picture, you must add a fraction of Size.Width and Size.Height to the location of the picture box.
This works for me
Cursor.Position = pictureBox1.PointToScreen(
new Point(pictureBox1.Size.Width / 2, pictureBox1.Size.Height / 7));
Note that this uses the PointToScreen method of the picture box instead of the form. Therefore, the location of the picture box is used as a reference automatically. This save us some calculations.
(Maybe there is a topic in this, but unfortunately I can't find it)
How can I find mouse coordinate dependent to windows form ?
Below code in c# can show mouse coordinate :
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = string.Format("X: {0} , Y: {1}", Cursor.Position.X, Cursor.Position.Y);
}
But this code show the coordinate in whole screen, when I want the coordinate (0,0) be top-left side of my form!
And I know that i can use Cursor.Position and PointToClient , but I cant solve my problem with them. can any one help me with several lines code?
(I use windows form)
In the MouseEventArgs passed to your event there is a property called Location.
According to MSDN
A Point that contains the x- and y- mouse coordinates, in pixels,
relative to the upper-left corner of the form.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = string.Format("X: {0} , Y: {1}", e.Location.X, e.Location.Y);
}
Does it solve your problem?
I have a windows form, inside it there is a PictureBox control, I populate the control with an image.
When went to the MouseDown event and was able to get the coordinates but in terms of the whole form and not just the control, so instead of getting a coordinate of (10,15) I get (110,40) that is because it got me the mouse position of the form.
Can I get a coordinates enclosed to the PictureBox control only?
Try this .........
private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int x = e.x - PictureBox1.Left ;
int y = e.y - PictureBox1.Top ;
MessageBox.Show(x.tostring + "," + y.tostring);
}
Use x - PictureBox.Left and y - PictureBox.Top.
OR
Write the code in the event handler for the PictureBox, not the form.
I am using ZedGraph to draw my plots in C#. I need to know which bar (in bar chart) was clicked by a mouse. How can I do that? Is there any way to get a bar by a point and for example change bar`s color?
Use MouseClick event and find the X and Y coordinates of the point where you clicked:
zg1.MouseClick+=new MouseEventHandler(zg1_MouseClick3);
private void zg1_MouseClick3(object sender, MouseEventArgs e)
{
PointF pt = (PointF)e.Location;
double x,y;
((ZedGraphControl)sender).MasterPane[0].ReverseTransform(pt, out x, out y);
// Do something with X and Y
}
Note, that I assumed we are operating on first pane (index 0) but if it is not your case, then you'll have to find which pane was clicked (see this example).
When you have X and Y position you should easily be able to guess which bar was clicked and do whatever you need with that information.
I'm developing an app for windows mobile (Compact Framework 2.0). It has a WinForms with a PictureBox.
I want to move the image of the PictureBox but I don't know how to do it so I choose to move the hole PictureBox.
To do it I use this event:
private void imagenMapa_MouseMove(object sender, MouseEventArgs e)
{
imagenMapa.Left = e.X;
imagenMapa.Top = e.Y;
this.Refresh();
}
But when I move the PictureBox it blinks and moves every where.
What I'm doing wrong?
Actual Code (Requires .NET Framework 3.5 and beyond, not sure if this is available in the Compact Framework)...
// Global Variables
private int _xPos;
private int _yPos;
private bool _dragging;
// Register mouse events
pictureBox.MouseUp += (sender, args) =>
{
var c = sender as PictureBox;
if (null == c) return;
_dragging = false;
};
pictureBox.MouseDown += (sender, args) =>
{
if (args.Button != MouseButtons.Left) return;
_dragging = true;
_xPos = args.X;
_yPos = args.Y;
};
pictureBox.MouseMove += (sender, args) =>
{
var c = sender as PictureBox;
if (!_dragging || null == c) return;
c.Top = args.Y + c.Top - _yPos;
c.Left = args.X + c.Left - _xPos;
};
The e.X and e.Y are relative to the picture box (e.g. if the mouse is in the upper left of the picture box, that's 0,0) .
The values for imagenMapa.Left and imagenMapa.Top are relative to the form (or whatever control contains imagenMapa)
If you try to mix values from these two systems without conversion, you're going to get jumps (like you're seeing).
You're probably better off converting the mouse position to the same coordinate system used by the thing that contains the picture box.
You could use imagenMapa.PointToScreen to get the mouse coordinates in screen coordinates (or Cursor.Position to get the position directly), and yourForm.PointToClient to get them back in the form coordinates.
Note that depending on your needs, you could accomplish "moving an image within a control" by overriding/handling the Paint event of a control and drawing the image yourself. If you did this, you could keep everything in the picturebox coordinates, since those are likely what you would use when you called graphicsObject.DrawImage.
e.X & e.Y is in the coordinate space of the pictureBox, imagenMapa.Left & imagenMapa.Top is in the coordinate space of the Form. :-)
Also don't forget to set your form to double buffered, that might help with the flickering, but for the actual positioning of it, I like Daniel L's suggestion
Embrace math!
control.Left = control.Left - (_lastMousePos.X - currentMousePos.X);
control.Top = control.Top - (_lastMousePos.Y - currentMousePos.Y);
Quick explanation:
You get the difference from the mouse positions and apply it to the object you want to move.
Example:
If the old mouse X position is 382, and the new one is 385, then the difference is -3. If the controls current X position is 10 then 10 - (-3) = 13
Why:
It works for anything, and is much cheaper than constantly converting coordinates back and forth.
Actually what you have done is correct. But you gave the MouseMove property to the picturebox. You should give that property to the Form(background).
ex:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
imagenMapa.Left = e.X;
imagenMapa.Top = e.Y;
}