Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

How to get the coordinates of a FrameworkElement?

Closed Thread
 
Thread Tools Display Modes
Old 01-31-2006   #1 (permalink)
Jason Dolinger
Guest


 

How to get the coordinates of a FrameworkElement?

In my continuing series of questions for the day, is there a way to get
the coordinates of a FrameworkElement (assuming it is somewhat
rectangular in shape?). There is a method Visual.getContentBounds(),
which returns you the bounding Rect, but it's unfortunately an internal
method.

I'd assume other people would like to do this as well? I basically have
a layered Grid AND Canvas. There are FrameworkElements layed out in the
Grid, and when they are moused over I need to display some things
somewhere in the region above them, and I'm choosing to layout those out
in the Canvas.

Thanks, any ideas?

Jason
Old 01-31-2006   #2 (permalink)
Michael Latta
Guest


 

Re: How to get the coordinates of a FrameworkElement?

You can use VisualHelper.GetContentBounds to get the same rectangle. But,
that rectangle is relative to the parent element. To get screen coordinates
you would need to walk up the element tree converting the original rectangle
as you go. You can do this until you get to an element with the same
coordinates as your canvas. I have had problems with the Visual method that
transforms a point from one coordinate space to another. Getting the
transform of each visual in the tree and transforming one at a time seems to
work more reliably in current CTPs.

Michael


"Jason Dolinger" <jdolinger@lab49.com> wrote in message
news:uxlaiQgIGHA.312@TK2MSFTNGP09.phx.gbl...
> In my continuing series of questions for the day, is there a way to get
> the coordinates of a FrameworkElement (assuming it is somewhat rectangular
> in shape?). There is a method Visual.getContentBounds(), which returns
> you the bounding Rect, but it's unfortunately an internal method.
>
> I'd assume other people would like to do this as well? I basically have a
> layered Grid AND Canvas. There are FrameworkElements layed out in the
> Grid, and when they are moused over I need to display some things
> somewhere in the region above them, and I'm choosing to layout those out
> in the Canvas.
>
> Thanks, any ideas?
>
> Jason



Old 01-31-2006   #3 (permalink)
Bob Brown[MSFT]
Guest


 

Re: How to get the coordinates of a FrameworkElement?

I did this and it makes a little caption move left <--> right just above my
FE according to the mouse's X position. Just hook it up to the FE's that
you want to add this behavior to. I'm not sure if this is what you were
looking for... I don't know what you meant by "the coordinates of a
FrameworkElement". I assumed that you meant the top left corner of its
bounding rect.

private void OnMouseMove( object sender, MouseEventArgs args )
{
// get mouse position relative to window.
Point windowOffset = args.GetPosition( this );

// get mouse position relative to "sender" in the grid
Point elementOffset = args.GetPosition(
(FrameworkElement)sender );

// calculate the difference between the two
Vector offset = windowOffset - elementOffset;

// create a new element and position it on the canvas above
"sender"
TextBlock text = new TextBlock();
text.Text = "Caption";
text.SetValue( Canvas.LeftProperty, windowOffset.X ); // put it
at mouse's x position
text.SetValue( Canvas.TopProperty, offset.Y - 30 ); // 30px
margin above "sender"

// replace the old caption with the new one
canvas.Children.Clear();
canvas.Children.Add( text );
}

--
Bob Brown [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.


"Jason Dolinger" <jdolinger@lab49.com> wrote in message
news:uxlaiQgIGHA.312@TK2MSFTNGP09.phx.gbl...
> In my continuing series of questions for the day, is there a way to get
> the coordinates of a FrameworkElement (assuming it is somewhat rectangular
> in shape?). There is a method Visual.getContentBounds(), which returns
> you the bounding Rect, but it's unfortunately an internal method.
>
> I'd assume other people would like to do this as well? I basically have a
> layered Grid AND Canvas. There are FrameworkElements layed out in the
> Grid, and when they are moused over I need to display some things
> somewhere in the region above them, and I'm choosing to layout those out
> in the Canvas.
>
> Thanks, any ideas?
>
> Jason



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding edge coordinates of elements Ofer Avalon 1 01-08-2008 10:23 AM
Texture Coordinates across multiple meshes Avery Z Avalon 1 10-19-2006 07:29 PM
API Standards question: FrameworkElement.Focusable Keith Patrick Avalon 3 02-27-2006 02:11 AM
How to draw a line without explicitly specifying its coordinates Pascal Bourque Avalon 4 02-04-2006 03:37 AM
FrameworkElement overlay notification? Jason Dolinger Avalon 1 02-02-2006 12:50 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50