Windows Vista Forums

Get postion of Visual relative to other Visual
  1. #1


    Robert Ludig Guest

    Get postion of Visual relative to other Visual

    I have a custom control that lives in a Panel together with other
    controls. Now that custom control needs to draw some lines to those
    other controls. How can it find the position (coordinates) of them
    relative to its own coordinate system. So that in the OnRender I could
    do:



    drawingContext.DrawLine(new Pen(),new
    Point(0,0),relativeLocationOfOtherElement);


      My System SpecsSystem Spec

  2. #2


    Bill Henning Guest

    Re: Get postion of Visual relative to other Visual

    Hi Robert,

    All you have to do is get a GeneralTransform from one Visual to the other like this:

    GeneralTransform transform = this.DialogLauncherButton.TransformToAncestor(this);
    Then once you have that GeneralTransform, you can call GeneralTransform.Transform on that instance to convert a Point from one coordinate system to the other.
    --

    Bill Henning
    Actipro Software

    WPF Ribbon and Wizard Controls - http://www.actiprosoftware.com/Products/#WPF




    "Robert Ludig" <schwertfischtrombose@gmx.de> wrote in message news:1179440453.581479.241290@y80g2000hsf.googlegroups.com...
    >I have a custom control that lives in a Panel together with other
    > controls. Now that custom control needs to draw some lines to those
    > other controls. How can it find the position (coordinates) of them
    > relative to its own coordinate system. So that in the OnRender I could
    > do:
    >
    > drawingContext.DrawLine(new Pen(),new
    > Point(0,0),relativeLocationOfOtherElement);
    >


      My System SpecsSystem Spec

  3. #3


    Robert Ludig Guest

    Re: Get postion of Visual relative to other Visual

    Yes I have tried this. Look at this little program:

    class Program
    {
    [STAThread]
    static void Main(string[] args)
    {
    Window w = new Window();
    StackPanel p = new StackPanel();
    w.Content = p;
    w.SizeToContent = SizeToContent.WidthAndHeight;
    p.Children.Add(new CustomControl());
    for (int i = 0; i< 4; i++)
    {
    Button b = new Button();
    b.Content = "Button " + i.ToString();
    b.Margin = new Thickness(10);
    p.Children.Add(b);
    }
    Application app = new Application();
    app.Run(w);
    }
    }

    public class CustomControl : FrameworkElement
    {
    public CustomControl()
    {
    this.ClipToBounds = false;
    }
    protected override Size MeasureOverride(Size availableSize)
    {
    return Size.Empty;
    }
    protected override void OnRender(DrawingContext
    drawingContext)
    {
    foreach (DependencyObject depObj in
    LogicalTreeHelper.GetChildren(this.Parent))
    {
    FrameworkElement f = depObj as FrameworkElement;
    if (f != null)
    {
    if (f == this)
    {
    continue;
    }
    GeneralTransform transform =
    f.TransformToVisual(this);
    Point point = transform.Transform(new Point(0,
    0));
    Console.WriteLine(f.ToString() + " " +
    point.ToString());
    drawingContext.DrawLine(new Pen(Brushes.Red,2),new
    Point(0,0),new Point(point.X + f.ActualWidth / 2,point.Y));
    }
    }
    drawingContext.DrawRectangle(Brushes.LightBlue,(Pen)null,
    new Rect(RenderSize));
    }
    }

    I would like to draw lines to each Button. Unfortuantely at the time
    the OnRender of CustomControl is called all the other Buttons are not
    yet rendered or something and therefore their offset always returns
    0,0. If the CustomControl is added last (after all the other butttons)
    the offset is returned correctly. But I can't rely on the user of my
    control to add it at last, so this is not an option.
    So I need to find a better time to walk the the tree, store the
    coordinates there and use them to draw the lines in the Onrender.The
    question now, is what is the preferred override to walk the tree and
    where I can be sure that the offsets are returned correctly?

    On 18 Mai, 03:50, "Bill Henning" <no_s...@hotmail.com> wrote:
    > Hi Robert,
    >
    > All you have to do is get a GeneralTransform from one Visual to the other like this:
    >
    > GeneralTransform transform = this.DialogLauncherButton.TransformToAncestor(this);
    > Then once you have that GeneralTransform, you can call GeneralTransform.Transform on that instance to convert a Point from one coordinate system to the other.
    > --
    >
    > Bill Henning
    > Actipro Software
    >
    > WPF Ribbon and Wizard Controls -http://www.actiprosoftware.com/Products/#WPF
    >
    >
    >
    > "Robert Ludig" <schwertfischtromb...@gmx.de> wrote in messagenews:1179440453.581479.241290@y80g2000hsf.googlegroups.com...
    > >I have a custom control that lives in a Panel together with other
    > > controls. Now that custom control needs to draw some lines to those
    > > other controls. How can it find the position (coordinates) of them
    > > relative to its own coordinate system. So that in the OnRender I could
    > > do:

    >
    > > drawingContext.DrawLine(new Pen(),new
    > > Point(0,0),relativeLocationOfOtherElement);- Zitierten Text ausblenden -

    >
    > - Zitierten Text anzeigen -




      My System SpecsSystem Spec

  4. #4


    Bill Henning Guest

    Re: Get postion of Visual relative to other Visual

    You might want to switch to TransformToAncestor to see if that makes a
    difference.

    Also, calling UpdateLayout on your control should ensure that all its
    children are arranged.


    --

    Bill Henning
    Actipro Software

    WPF Ribbon and Wizard Controls -
    http://www.actiprosoftware.com/Products/#WPF
    WPF Resource Guide - http://www.WPFpedia.com



    "Robert Ludig" <schwertfischtrombose@gmx.de> wrote in message
    news:1179471552.229266.37320@q75g2000hsh.googlegroups.com...
    > Yes I have tried this. Look at this little program:
    >
    > class Program
    > {
    > [STAThread]
    > static void Main(string[] args)
    > {
    > Window w = new Window();
    > StackPanel p = new StackPanel();
    > w.Content = p;
    > w.SizeToContent = SizeToContent.WidthAndHeight;
    > p.Children.Add(new CustomControl());
    > for (int i = 0; i< 4; i++)
    > {
    > Button b = new Button();
    > b.Content = "Button " + i.ToString();
    > b.Margin = new Thickness(10);
    > p.Children.Add(b);
    > }
    > Application app = new Application();
    > app.Run(w);
    > }
    > }
    >
    > public class CustomControl : FrameworkElement
    > {
    > public CustomControl()
    > {
    > this.ClipToBounds = false;
    > }
    > protected override Size MeasureOverride(Size availableSize)
    > {
    > return Size.Empty;
    > }
    > protected override void OnRender(DrawingContext
    > drawingContext)
    > {
    > foreach (DependencyObject depObj in
    > LogicalTreeHelper.GetChildren(this.Parent))
    > {
    > FrameworkElement f = depObj as FrameworkElement;
    > if (f != null)
    > {
    > if (f == this)
    > {
    > continue;
    > }
    > GeneralTransform transform =
    > f.TransformToVisual(this);
    > Point point = transform.Transform(new Point(0,
    > 0));
    > Console.WriteLine(f.ToString() + " " +
    > point.ToString());
    > drawingContext.DrawLine(new Pen(Brushes.Red,2),new
    > Point(0,0),new Point(point.X + f.ActualWidth / 2,point.Y));
    > }
    > }
    > drawingContext.DrawRectangle(Brushes.LightBlue,(Pen)null,
    > new Rect(RenderSize));
    > }
    > }
    >
    > I would like to draw lines to each Button. Unfortuantely at the time
    > the OnRender of CustomControl is called all the other Buttons are not
    > yet rendered or something and therefore their offset always returns
    > 0,0. If the CustomControl is added last (after all the other butttons)
    > the offset is returned correctly. But I can't rely on the user of my
    > control to add it at last, so this is not an option.
    > So I need to find a better time to walk the the tree, store the
    > coordinates there and use them to draw the lines in the Onrender.The
    > question now, is what is the preferred override to walk the tree and
    > where I can be sure that the offsets are returned correctly?
    >
    > On 18 Mai, 03:50, "Bill Henning" <no_s...@hotmail.com> wrote:
    >> Hi Robert,
    >>
    >> All you have to do is get a GeneralTransform from one Visual to the other
    >> like this:
    >>
    >> GeneralTransform transform =
    >> this.DialogLauncherButton.TransformToAncestor(this);
    >> Then once you have that GeneralTransform, you can call
    >> GeneralTransform.Transform on that instance to convert a Point from one
    >> coordinate system to the other.
    >> --
    >>
    >> Bill Henning
    >> Actipro Software
    >>
    >> WPF Ribbon and Wizard
    >> Controls -http://www.actiprosoftware.com/Products/#WPF
    >>
    >>
    >>
    >> "Robert Ludig" <schwertfischtromb...@gmx.de> wrote in
    >> messagenews:1179440453.581479.241290@y80g2000hsf.googlegroups.com...
    >> >I have a custom control that lives in a Panel together with other
    >> > controls. Now that custom control needs to draw some lines to those
    >> > other controls. How can it find the position (coordinates) of them
    >> > relative to its own coordinate system. So that in the OnRender I could
    >> > do:

    >>
    >> > drawingContext.DrawLine(new Pen(),new
    >> > Point(0,0),relativeLocationOfOtherElement);- Zitierten Text
    >> > ausblenden -

    >>
    >> - Zitierten Text anzeigen -

    >
    >




      My System SpecsSystem Spec

Get postion of Visual relative to other Visual problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Layout mattg08 Vista General 1 18 Jan 2008
Err.Number 486, Vista Ultimate, Visual Studio, & Visual Basic Mike Vista General 2 16 Jan 2008
Running Visual C++ and/or Visual Basic under Vista Home Premium OS DaleB Vista General 10 20 Nov 2007
visual c++ 6.0 RAM Vista installation & setup 0 21 Feb 2007
Visual Studio 6 and Visual Source Safe 6 on Vista Mikhail Gryaznov Vista General 0 14 Jun 2006