Get Mouse Position in SharpDX relative to window - c#

How can I get the Position of the Cursor in C# relative to the window im in?
I am using SharpDX and i tried
System.Drawing.Point loc = Cursor.Position;
and
System.Drawing.Point loc = Control.MousePosition;
which gave me the absolute X and Y Coordinates of the Cursor (X:0 and Y:0 being in the top left corner of my screen and not my window).
Is there something implemented in SharpDX to get this done?
I found nothing that worked for me on the internet

Assume you are using a winform? If so, look for mouse events for controls.
If you are not using Visual studio then you will have to add the event. The controls sample can be found here -> https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx
Sharpdx does not itself deal with mouse at all from my recollection. You need to understand the field of movement for the mouse within the window and convert to what ever input stream you need.

Related

Visual Studio Windows forms C# is there a way to make a picture box look at the cursor?

I am doing a project for coursework which is why I am using VS2019 windows forms. I've been trying to get a picture box to look at the cursor and I've not found a solution, This image shows what I want to achieve, the circle with an arrow is the character, the arrow is the direction the character is looking at, and the triangle on the stick is the cursor and the character will always look in the direction of the mouse cursor, so when the mouse cursor moves so does the picture box to look in the cursor's direction. The picture box's only rotates when the mouse cursor moves, it doesn't change it's location but just rotates to look in the direction of the mouse pointer.
To reproduce your problem: C# sets the mouse cursor to a custom image.
Implementation code:
/// <summary>
/// Set the cursor to the image
/// </summary>
private void setBmpCursor()
{
//The image needs to be converted to ico format
//Here can also use the resource file written in the previous article to store pictures
string path = "C:\\Users\\Admin\\source\\repos\\TestRe\\TestRe\\test.ico";
//original image
Bitmap cursorBmp = new Bitmap(path);
//Custom size (double down);
Bitmap bmp = new Bitmap(cursorBmp, cursorBmp.Width / 2, cursorBmp.Height / 2);
this.Cursor = new Cursor(bmp.GetHicon());
}
achieve effect:

C# WinForms Move Cursor To Other Screen [duplicate]

I could not find anything on this particular problem, so here it goes!
I am interested in changing the position of the mouse cursor programmatically.
Cursor.Position = new Point(x, y);
works as expected using a single monitor. However, when I plug in a second one and choose to extend the desktop, changing the cursor's coordinates only works relative to the main screen.
So, does anyone know how to tell the cursor that I want to move to a different screen?
Get the width and height of your required screen and move the cursor relative to it
int width=Screen.AllScreens[1].Bounds.Width;
int height=Screen.AllScreens[1].Bounds.Height;
Cursor.Position=new Point(width-x,height-y);

C# How to Position a Parent Form using Client Coordinates?

I have a main form (application's parent form) which is set to the back using p/Invoke SetWindowPos. SetWindowPos asks for the X,Y coordinates of the window which are to be in Client Coordinates.
When I start the program the window is displayed center of screen. I then move the window and save the new location to Properties.Settings.Default.frmMainLocation which is bound to the frmMain.Location property.
I stop the program and then restart it - and the form is back to the original center position.
How would I set SetWindowPos to locate the window at X, Y Coordinates which are derived from the form's Location.X, Location.Y coordinates?
If you take your properties and save them to a point, you can set the window location like this:
Point pos = new Point
(
Properties.Settings.Default.posX,
Properties.Settings.Default.posY
);
this.Location = pos;
I set this in my form's Form_Load() method for the Form.Load() event with a few different sets of simple coords and it changed the position that the form was displayed on load each time. It's just a hop, skip, and a jump to have it load the coords from Properties.Settings.Default as I've shown in the example.
Actually it was a bit of a mess getting it to work.
When the App starts you need to SetWindowPos without the SWP_NOMOVE flag
Then SetWindowPos with the SWP_MOVE Flag set
Then when you want to change the position repeat the App start process.

Simulate mouse events

I'm trying to simulate a mouse move using the following code: mouse_event(MOUSEEVENTF_MOVE,150 ,150, 0, 0);
It works fine, but the X and Y coordinates start from my current cursor position. Is there a way I can make them start at the upper left of my screen?
I'm working in a Windows form app using c#
If you have no clue of what I'm asking, I tried to draw it for you;
The MSDN documents say that you can simply set the position of a form's cursor. Just do that, then send your mouseevent.
System.Windows.Forms.Cursor.Position = new Point(0, 0);
followed by your mouse_event, should do the trick.

How to get (x, y) coordinates of displayed image?

Currently, I'm using a WPF Image control to display images. I want to do the following: On a MouseUp, MouseDown or MouseMove event, I want to get the coordinates of the point under the cursor, not with reference to the top-left corner of the control, but with respect to the actual coordinates of the displayed bitmap, which may have been re-scaled, or may have a different aspect ratio than the control itself. If an alternate control provides this functionality, I'd be equally happy to hear about it. Thanks in advance.
EDIT: I'd also like so may to know it if the coordinates of the control were out of range of the image, due to a different aspect ratio.
you can get the coordinates of the mouse by this way :
private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(image1); //position relative to the image
Point point2 = e.GetPosition(this); //position relative to the window
}
Hope that answered a part of your question.
You can use the various TransformTo* methods of the Visual class to get, coordnates relative to a sepcific control, it will take all transformation applied to the visual tree into account.
Also, if you attach MouseUp, MouseDown and MouseMove to the Image control itself, you should already get the correct coordinates from the MouseButtonEventArgs and if you use the mouse outside of the visual bounds of the control, you will not get those events, so you don't need extra code check for coordinates being out of bounds.
If your actual goal is to find out which acutal pixel of your bitmap image has been touched by the mouse (as you would need for a bitmap/pixel editing software) things get much harder because WPF uses virtual device indendent pixels that do not directly relate to pixels on the screen or pixels in a bitmap that has been rendered in an Image control. The Image control internally scales a bitmap image based on the DPI settings of the bitmap file itself and based on the DPI settings of operating system.

Categories

Resources