Get the position of a view on the screen - c#

I am using Xamarin native (not forms) and I have an ImageView inside a RelativeLayout.
Is there any way to get the absolute position of the view relative to the screen and not relative to the parent?
I saw some solutions for this problem on Xamarin.Forms that loop through the parent view and add its coordinates relative to its parent, but it doesn't seem to work here.
I tried using iv.GetX() but this is for getting the position in relative to the parent.
What is the way to get the absolute position?
Thanks in advance.

You could get the absolute coordinates across the entire screen with the GetLocationOnScreen method.
like:
int[] location = new int[2];
iv.GetLocationOnScreen(location);
int x = location[0];
int y = location[1];
Note:
This value is calculated from the top of the screen, which includes the height of the notification bar.If those parameters are printed in the Activity's OnCreate() method, they are all 0, and you won't get them until the UI controls are loaded.
It's better to get it in OnWindowFocusChanged method.

Related

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);

Camera.pixel(Height/Width) doesnt match the Canvas size Unity

I am making an app that has a custom viewport Rect like so:
When i check the sizes of this viewport rect in pixels with the following function in c#: Camera.pixelHeight; Camera.pixelWidth i get the following values: 743px, 770px.
This looks correct.
Now when i check my canvas in the Editor i see the following values for the width and hight.. See img below
No i have a script that gets the width and hight of the Canvas.. This is correct i get again the 743px and 770px. But when i position my UI element it is outside because the positioning refers back the the incorrect values displayed in the Canvas Rect Transform properties.
Edit:
Maybe it has something to do with how i position the element. I do it in the following way atm:
sl.position = new Vector3(200, -200, 0);
this position should be relative to the anchor point of this element in the Canvas.
What is happening here and how do it make the canvas use the correct values?

Label disappearing after attributting it a parent [duplicate]

In my GUI, i need to change Parents of PictureBoxes to the background PictureBox so that the transparency works correctly.
However, changing the parent also changes the location of the pictureboxes. I have tried grabbing the absolute location of the picture box via the PointToClient, but it doesn't work. I put the coordinates in the comments, and they don't change after assigning the new parent even though the image visibly changes location. Furthermore, I don't expect that it could possibly work as it's being passed a point, not an object with more information about parents and whatnot that's needed to deduce the absolute position.
What is the correct way to deduce the absolute position of an element so that I can move the image to the correct location after its parent changes? Or is there a better way to do this?
Point oldRel = pictureBox4.Location; //258, 109
Point oldAbs = PointToClient(oldRel); //75, -96
//Commenting out this line fixes the image shift but ruins the transparency
pictureBox4.Parent = pictureBox2;
Point newRel = pictureBox4.Location; //258, 109
Point newAbs = PointToClient(pictureBox4.Location); //75, -96
This will move a Control child from one Parent to a new one, keeping the absolute screen position intact:
void MoveTo(Control child, Control newParent )
{
child.Location = newParent.PointToClient(child.PointToScreen(Point.Empty));
child.Parent = newParent;
}
The trick with PointToClient and PointToScreen is to use them from the right parent control; not setting the control will default to the Form, which will miss out on the actual position of the parent..

Determine position on ImageView

I'm developing a mobile application (xamarin.android & c#). Situation is showing an image having a certain width, e.g. 2380 pixel. I'm using the "ShowPress"-gesture so that the user is able to mark s a point on the picture. Now I need the location on the picture.
The event gets a parameter that provides the values RawX as well as RawY relating to the screen. How can I get the size of the ImageView? I need it to determine the position on original bitmap.
public void OnShowPress(MotionEvent e)
{
var x = e.RawX;
var y = e.RawY;
How can I achieve this?
You could read this document : MotionEvent,
Position e.RawX/e.RawY is related to the screen :
Returns the original raw X coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.
Position e.GetX/e.GetY is related to the view :
getX(int) for the first pointer index (may be an arbitrary pointer identifier).
Better understanding :
RawX/RawY that is guaranteed to return absolute coordinates, relative to the device screen.
While GetX()/GetY(), should return you coordinates, relative to the View, that dispatched them.
So you could use e.GetX()/e.GetY() to determine the position on your original bitmap.

Getting points along a XAML Shapes.Path

I'm trying to get points along a Shapes.Path object from a Silverlight MainWindow.xaml file. I use Expression Blend to create the path.
The following code works fine unless I try to move the path in expression blend, which only moves the margins. Apparently that doesn't get calculated in, and I get the points from where the path was originally created.
var pathGeometry = path.Data.GetFlattenedPathGeometry();
var currentPoint = new System.Windows.Point();
var tangentPoint = new System.Windows.Point();
var rList = new List<Point>();
var pointsCount = 10;
for (int i = 0; i < pointsCount; i++)
{
double fraction = (double)i / (double)(pointsCount - 1);
pathGeometry.GetPointAtFractionLength(fraction, out currentPoint, out tangentPoint);
rList.Add(currentPoint);
}
return rList;
Any suggestions on how to get the margin change to also calculate in?
Ah... I think I see the problem. PathGeometry does not inherit from UIElement, meaning that it does not have any sense of Margins. The Geometry is hosted within a control that does, and it is that control which actually draws the Geometry onto the screen. If you want to translate a point on the Geometry into screen coordinates, then you'll need to find the location of the host control and calculate the offset.
My guess is that your PathGeometry is hosted inside a Path control. (Confusing, no?) Getting the location in screen coordinates is relatively easy. You can call the PointToScreen method on Path and feed it the calculated point (perhaps adding any padding you may be applying). Getting a point relative to the window is more complicated. You could call PointToScreen (for point 0,0) on the window and then subtract the Path's point from the window's. Or, you could put the Path inside a Canvas (where the Canvas is directly inside the window) and simply get the Canvas.Top and Canvas.Left.
You don't say what your ultimate goal is, so it is difficult to recommend a particular solution for you.

Categories

Resources