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

Get postion of Visual relative to other Visual

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-17-2007   #1 (permalink)
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
Old 05-17-2007   #2 (permalink)
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
Old 05-18-2007   #3 (permalink)
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
Old 05-18-2007   #4 (permalink)
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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Err.Number 486, Vista Ultimate, Visual Studio, & Visual Basic Mike Vista General 2 01-16-2008 01:06 PM
Visual Studio 6 and Visual Source Safe 6 on Vista Mikhail Gryaznov Vista General 1 01-15-2008 10:07 PM
Running Visual C++ and/or Visual Basic under Vista Home Premium OS DaleB Vista General 10 11-20-2007 12:45 PM
Visual Studio 6.0 Lorne Merner Vista General 1 05-11-2007 11:02 PM
visual c++ 6.0 RAM Vista installation & setup 0 02-21-2007 10:49 AM


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 51