Text under mouse cursor on PictureBox - c#

I have a problem with displaying the text under the mouse cursor on Picture Box.
I want to dynamically display the mouse coordinates under the cursor.
I'm trying to display text by label and change the label's position when a "MouseMove" event happens.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Location = new Point(e.X + 10, e.Y + 10);
}
But, I think, it is a bad solution...
And although it works it hides other output when we move the cursor
Thanks a lot!

Thanks for Thomas's advice, here code to display mouse coordinate under cursor:
ToolTip tt = new ToolTip();
tt.SetToolTip(pic, " ");
tt.ShowAlways = true;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
tt.ToolTipTitle = e.X + " " + e.Y;
}

Related

Picturebox change location on mouseenter without white borders appearing

Hello I'm trying to do the effect that the button is pressed, so when mousenter moving 2px the picturebox, I'm using picturebox as button because It allows me to set transparent backgrounds.
I tried forcing the background to transparent in various events(paint,pevious change location,after change location),but without success.
I think that is caused for the re-drawing when changing an element, because the white part of the background that whas "hidden" by the picturebox appears.
Any idea how to solve that?
Thanks in advance
private void buttonX2_MouseLeave(object sender, EventArgs e)
{
((PictureBox) sender).Location = new Point(
((PictureBox) sender).Location.X, ((PictureBox) sender).Location.Y - 2);
}
private void buttonX2_MouseEnter(object sender, EventArgs e)
{
((PictureBox)sender).Location = new Point(
((PictureBox)sender).Location.X, ((PictureBox)sender).Location.Y + 2);
}
The problem seems to come from moving in or out too slowly. If you do that you will say enter from below, but the PB is moving up so you're out of it again, so it moves down and therefore you're in it again etc..The correct repainting of the Background can't keep up with these 'Jittering ButtBoxes'..
First, as Hans noted, make things less obtrusive: make the Form's BackColor dark, maybe even black!
Second to avoid the problem of jittering, move the Mouse Cursor itself a few pixels with the Pictureboxes, like this:
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
PictureBox PB = (PictureBox)sender;
Point MP = Cursor.Position;
this.SuspendLayout();
PB.Location = new Point( PB.Location.X, PB.Location.Y - 2);
Cursor.Position = new Point(MP.X, MP.Y - 2);
this.ResumeLayout();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
PictureBox PB = (PictureBox)sender;
Point MP = Cursor.Position;
this.SuspendLayout();
PB.Location = new Point( PB.Location.X, PB.Location.Y + 2);
Cursor.Position = new Point(MP.X, MP.Y + 2);
this.ResumeLayout();
}

Grab, move and drop control in Form by mouse

Hello I have found this code that might help me with following issue, I'm trying to make drag, drop and move label in my Form by mouse.
private Point MouseDownLocation;
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Left = e.X + this.Left - MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
}
But when I assing mousemove and mousedown as events to label and i try to grab the label and move with mouse it moves with the whole Form.
May I ask where is should the code be improved?
Thank you for your time.
Instead of using this.Left (which is the form), you need to move your control:
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MyControl.Left = e.X + MyControl.Left - MouseDownLocation.X;
MyControl.Top = e.Y + MyControl.Top - MouseDownLocation.Y;
}
}
In addition, you may want to capture the mouse on button down, and release it on button up. That will prevent very fast movements from "breaking" your logic. For details, see Mouse Capture in Windows Forms.

Show my current cursor position as a current line and current column?

I am working on Windows Forms application that uses a RichtextBox, Menustrip and many more controls.
I have done some work but can't get it to work. When my mouse cursor moves in RichTextBox I want to change change the position automatically, like that on simple notepad.
My bit coding is....
I want it so when my mouse cursor moves it changes the dynamic position on my status bar
private void sizeToolStripMenuItem_Click(object sender, EventArgs e)
{
int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);
toolStripStatusLabel5.Text ="Line"+" "+ line.ToString();
toolStripStatusLabel6.Text = " Column" + " " + line.ToString();
toolStripStatusLabel3.Text= Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
}
show your line every time when press the enter key.
code is mention below:::----
private void Key_Down(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);
toolStripStatusLabel5.Text = "Line" + " " + line.ToString();
toolStripStatusLabel6.Text = " Column" + " " + column.ToString();
toolStripStatusLabel3.Text = Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
Update();
}
}
If you want to update position automatically, you should use MouseMove event from richtextbox instead. While you are moving your mouse it is always updating. Also, "MouseEventArgs e" from MouseMove call can give you the cursor position inside the richtextbox.
You could subscribe to the RichTextBox MouseMove event to update the ToolStrip label with the current mouse position
Example:
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
toolStripStatusLabel3.Text = string.Format("X={0}, Y={1}", e.X, e.Y);
}
Or if you want it to show the position reletive to the RichTextBox you can use the Location from the MouseEventArgs, this will return the position inside the RichTextBox (topleft of textbox = 0,0)
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
toolStripStatusLabel3.Text = string.Format("X={0}, Y={1}", e.Location.X, e.Location.Y);
}
I have done with the help of StackoverFlow and sweetly user(programmer).
Thanks for reply. My code is
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
int column = richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexFromLine(line);
toolStripStatusLabel5.Text = "Line" + " " + line.ToString();
toolStripStatusLabel6.Text = " Column" + " " + line.ToString();
toolStripStatusLabel3.Text = Cursor.Position.ToString(); // where is my mouse cursor at this Time like that x and y cordinate 330,334
Update();
}
}

How to move PictureBox in C#?

i have used this code to move picture box on the pictureBox_MouseMove event
pictureBox.Location = new System.Drawing.Point(e.Location);
but when i try to execute the picture box flickers and the exact position cannot be identified. can you guys help me with it. I want the picture box to be steady...
You want to move the control by the amount that the mouse moved:
Point mousePos;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
mousePos = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
int dx = e.X - mousePos.X;
int dy = e.Y - mousePos.Y;
pictureBox1.Location = new Point(pictureBox1.Left + dx, pictureBox1.Top + dy);
}
}
Note that this code does not update the mousePos variable in MouseMove. Necessary since moving the control changes the relative position of the mouse cursor.
You have to do several things
Register the start of the moving operation in MouseDown and remember the start location of the mouse.
In MouseMove see if you are actually moving the picture. Move by keeping the same offset to the upper left corner of the picture box, i.e. while moving, the mouse pointer should always point to the same point inside the picture box. This makes the picture box move together with the mouse pointer.
Register the end of the moving operation in MouseUp.
private bool _moving;
private Point _startLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_moving = true;
_startLocation = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_moving = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_moving) {
pictureBox1.Left += e.Location.X - _startLocation.X;
pictureBox1.Top += e.Location.Y - _startLocation.Y;
}
}
Try to change SizeMode property from AutoSize to Normal

Get cursor position with respect to the control - C#

I want to get the mouse position with respect to the control in which mouse pointer is present. That means when I place the cursor to the starting point (Top-Left corner) of control it should give (0,0). I am using the following code:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = Convert.ToString(Cursor.Position.X + ":" + Cursor.Position.Y);
}
But this gives the position with respect to the screen not to the control.
Code sample will be appreciated.
Use Control.PointToClient to convert a point from screen-relative coords to control-relative coords. If you need to go the other way, use PointToScreen.
You can directly use the Location property of the MouseEventArgs argument passed to your event-handler.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = e.Location.X + ":" + e.Location.Y;
}
The following will give you mouse coordinates relative to your control. For example, this results in (0,0) if mouse is over top left corner of the control:
var coordinates = yourControl.PointToClient(Cursor.Position);
One can use following methods for getting the relative from absolute and absolute from relative coordinates:
Point Control.PointToClient(Point point);
Point Control.PointToScreen(Point point);
Cursor.Position return Point on Screen, but Control.PointToClient(Cursor.Position) returns point on control (e.g. control -> panel). In your case, you have e.Locate which return point on control.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = panel1.PointToClient(Cursor.Position).ToString();
}
Simply subtract from the cursor position the Left and Top coordinates of the control:
this.Text = Convert.ToString(
Cursor.Position.X - this.Left + ":" +
Cursor.Position.Y - this.Top);
I use MouseLocation and PointToClient to check. And then use it in a timer!
bool IsMouseHover(Control c, Control container)
{
Point p = Control.MousePosition;
Point p1 = c.PointToClient(p);
Point p2 = container.PointToClient(p);
if (c.DisplayRectangle.Contains(p1) && container.DisplayRectangle.Contains(p2))
{
return true;
}
return false;
}
private void lienzo_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
Point coordenadas = new Point();
coordenadas = Mouse.GetPosition(lienzo);
string msg = "Coordenadas mouse :" + coordenadas.X + "," + coordenadas.Y;
MessageBoxResult resultado;
string titulo = "Informacion";
MessageBoxButton botones = MessageBoxButton.OK;
MessageBoxImage icono = MessageBoxImage.Information;
resultado = MessageBox.Show(msg, titulo, botones, icono);
}
Where "lienzo" is my canvas panel
Snippet code as following:
private void Control_MouseMove(object sender, MouseEventArgs e)
{
var btn = sender as Button;
var point = e.Location;
point.X += btn.Location.X;
point.Y += btn.Location.Y;
Control findTarget = btn.Parent.GetChildAtPoint(point, GetChildAtPointSkip.Invisible) as Button;
if (findTarget != null)
{
// TO DO
}
}
Where the button is one of many buttons in a hosting panel.
Create af standard Project C# WinForms
Place 2 Textboxes named X and Y, and a Timer object from the toolbox to the Design page
Press [F7] and replace all code with below.
using System;
using System.Windows.Forms;
namespace MousePos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void Form1_MouseCaptureChanged(object sender, EventArgs e)
{
X.Text = MousePosition.X.ToString();
Y.Text = MousePosition.Y.ToString();
}
}
}
Set Timer.Tick action to "Form1_MouseCaptureChanged"
[F5] Run - and now you have an MosusePos app.

Categories

Resources