How to get Mouse Position as pixel number in the text boxes - c#

I am working on real-time video processing project.
During the real-time video i need to get mouse position when left clicked. To do that i have one button to send mouse position to text boxes. To see position of the cursor, i have two text boxes named as txt4 and txt5 for X and Y position seperately.
I have written the given code as shown the below. When i click the button it shows the current postion of the mouse (actually it is the position of the button) but then when i do left click position of the mouse is not shown in the text boxes. Position is shown when i press "Enter" while button is clicked.
What is wrong? and How can i correct it?
private void btnCursor_Click(object sender, EventArgs e)
{
if (device.IsRunning)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
txt4.Text = string.Format("x={0:0000}", Cursor.Position.X);
txt5.Text = string.Format("y={0:0000}", Cursor.Position.Y);
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Click>0)
{
txt4.Text = MousePosition.X.ToString();
txt5.Text = MousePosition.Y.ToString();
}
}
I tried to get mouse position when i do left click.
What i get is mouse position when i pressed "Enter" instead of left clicked.
I have error in line "if(e.Click>0)".

Related

After panning the object, the objects resets to original position when mouse button is clicked next time pan operation

I am working on transformations with the cube in open tk. The pan operations are set on the right mouse button. Select the right mouse button and drag. The cube moves according to changes in screen x and y coordinates correctly. The only issue I am facing is, when button is released and again the right mouse button the clicked, the cube is shifted to original position which should not happen. The next pan operation should start from the previous pan position.
The mouse event and pan code is shared.
internal new void MouseDown(object sender, System.Windows.Forms.MouseEventArgs mouse)
{
MouseStartPosition = new Vector2(mouse.X, mouse.Y);
}
internal new void MouseMove(object sender, System.Windows.Forms.MouseEventArgs mouse)
{
if (mouse.Button == MouseButtons.Right)
{
MouseNewPosition = new Vector2(mouse.X, mouse.Y);
Pan();
}
internal void Pan()
{
CubeRender.translateX = MouseNewPosition.X - MouseStartPosition.X;
CubeRender.translateY = MouseStartPosition.Y - MouseNewPosition.Y;
this.Invalidate();
this.Refresh();
}

Re-position WPF window after resizing to mouse location

I am creating a small game that resizes the main window to fit a growing board. I want to re-position the window to keep the button that was clicked under the mouse after resizing. Currently, when clicked the board gets wider and moves the button away from the mouse.
How do I define the button as the anchor point when I move the window?
I don't know if there is a way to use the button as an anchor point. But after a little thinking, i came up with a code that can work for you.
Basically, i'm using the mouse relative to the button position before and after the resize to move the window accordingly. I hope it helps.
private void Btn1Click(object sender, RoutedEventArgs e)
{
int widthGrowth = 50;
int heightGrowth = 80;
Button btn = sender as Button;
Point oldMousePosition = Mouse.GetPosition(btn);
Width += widthGrowth;
Height += heightGrowth;
Point newMousePosition = Mouse.GetPosition(btn);
Left += newMousePosition.X - oldMousePosition.X;
Top += newMousePosition.Y - oldMousePosition.Y;
}

Visual Studio C# how to move a button's X position by mouse without flicker

So I'm trying to make a slider. I'm using my cursor to move a button's x position.
I have 3 functions, the mouseDown, mouseUp and the mouseMove function. In the mouseUp and mouseDown functions I set a variable to true and false to tell the program that the mouse is clicked or not. In the mouseMove function I tell the program to set the button's x position to the x position of the mouse when the mouse is clicked. This works but has 2 problems.
The first problem is that when I press the button and move it, the button moves along with the mouse's x but it has a space between the mouse and the button. It looks a bit like this:
CURSOR.......BUTTON
The space between the cursor and button change when I change the resolution of the form.
The second problem is that when I move the button it flickers a bit. It only does this at higher speeds but it is a problem in my case.
My code looks like this:
bool mouseDown = false;
private void volumeGrabBT_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
}
}
private void volumeGrabBT_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = false;
}
}
private void volumeGrabBT_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
Point volumeBTPoint = new Point();
volumeBTPoint.X = Cursor.Position.X;
volumeBTPoint.Y = volumeGrabBT.Location.Y;
volumeGrabBT.Location = volumeBTPoint;
}
}
The volumeGrabBT is the button I'm trying to move along with the mouse.
The volumeBTPoint is the point of the button I'm trying to set the button's position to.
I hope someone can help me fix these problems. Thanks in advance!
I believe that flickering can be fixed by setting some additional form styles: SetStyle(ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
in form's constructor. It will use double buffering and generally just draws faster.
For the Cursor class, it's relative to the screen, not your form. You can use this.PointToClient() function to get client's space position of cursor, like this:
Point clientCursor = this.PointToClient(Cursor.Position);
and then use clientCursor to get exact X in your client space.
You have to translate screen coordinates to client coordinates.
Point volumeBTPoint = new Point();
Point point = this.PointToClient(Cursor.Position);
volumeBTPoint.X = point.X;
volumeBTPoint.Y = volumeGrabBT.Location.Y;
volumeGrabBT.Location = volumeBTPoint;
Instead of this you should use button's parent control (Panel, GroupBox, etc).

How to get the correct position of an Image in WPF?

I am a beginner in Windows Presentation Foundation.
I have a Canvas in which there is a small square canvas. Inside this small square canvas i have an image.
I have attached an event handler to this image , When user click on this image user can get the current position of the image.
Whenever user click on that image , he/she will get a prompt message box in which user get the current image position to print.
Everything is working fine when user press Left click on the image .
But, the problem arises when user first pressing the right click on the image and suddenly pressing Left click on the same image , It is not showing the same value.
I have not attached any event handler on the right click.
I am using the following code :
Image objImage = new Image();
objImage .Height =12;
objImage .Width = 12;
objImage .Stretch = Stretch.UniformToFill;
imageAck.MouseLeftButtonDown += new MouseButtonEventHandler(objImageMouseLeftButtonDown);
private void objImageMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point mouseCurrent = e.GetPosition(null);
}
I am notgetting the same value in mouseCurrent
Using e.GetPosition, this gets the position of the mouse pointer relative to the element that was clicked, not the canvas that the element belongs to.
To get the actual position of the image on the Canvas, you need to use the Canvas.GetLeft, and Canvas.GetTop methods. Here is an example:
private void objImageMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
double left = Canvas.GetLeft((UIElement)sender);
double top = Canvas.GetTop((UIElement)sender);
}
You can then use the left and top variables for whatever purpose you need.
Finally, I got an answer by Googling some hour .....
Point currentMousePosition = Mouse.GetPosition(Application.Current.MainWindow);

Button image not aligned with text when the button is clicked?

I'm having a rather irritating problem with images on buttons in .NET. They don't behave as you would expect an image on a button to behave.
In the properties of a button you can set Image. So I select an image and the image shows up on the button! So far so good.
When a button is clicked, or in a pressed state, the text of the button will move down and right one pixel to create a depth. But not the image! It will stay in the same position, and it will look weird.
There is also the BackgroundImage property, but that's even worse! Because if I set BackgroundImageLayout to None instead of Center, the image will move up and left when pressed, the complete opposite direction of the text! What's up with that?
Anyway, what I want to achieve is a button image that moves just like the text would move when the button is in a pressed state. Is there a way to do this?
Just make a new image and paste the original one at an offset. Then set that as the Button's Image.
Example:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
// replace "button_image.png" with the filename of the image you are using
Image normalImage = Image.FromFile("button_image.png");
Image mouseDownImage = new Bitmap(normalImage.Width + 1, normalImage.Height + 1);
Graphics g = Graphics.FromImage(mouseDownImage);
// this will draw the normal image at an offset on mouseDownImage
g.DrawImage(normalImage, 1, 1); // offset is one pixel each for x and y
// clean up
g.Dispose();
button1.Image = mouseDownImage;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
// reset image to the normal one
button1.Image = Image.FromFile("button_image.png");
}
EDIT: The following function fixes a problem where the image would not 'pop' back up when the cursor leaves the button area while the mouse button is still pressed (see Labor's comment below):
private void button1_MouseMove(object sender, MouseEventArgs e)
{
Point relMousePos = e.Location;
bool mouseOverButton = true;
mouseOverButton &= relMousePos.X > 0;
mouseOverButton &= relMousePos.X < button1.Width;
mouseOverButton &= relMousePos.Y > 0;
mouseOverButton &= relMousePos.Y < button1.Height;
if (mouseOverButton != MouseButtons.None)
{
button1_MouseDown(sender, e);
}
else
{
button1_MouseUp(sender, e);
}
}

Categories

Resources