![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Visual Basic | Vista General | |||
| Err.Number 486, Vista Ultimate, Visual Studio, & Visual Basic | Vista General | |||
| Running Visual C++ and/or Visual Basic under Vista Home Premium OS | Vista General | |||
| visual c++ 6.0 | Vista installation & setup | |||
| Visual Studio 6 and Visual Source Safe 6 on Vista | Vista General | |||