Pickle Matrix Technician

Hi,

I have had trouble positioning DrawingVisual objects where I want them when they are added as children to another DrawingVisual. For example, say I have a DrawingVisual (DV) that is 1000 x 1000, and I have another DV that is 20 x 20 that I want to be drawn at (500, 500) on the large DV. If I draw the contents of the small DV with an offset of (500, 500), and then add it to the large DV's children, it will be in the right place. The problem is if I want to move it to (400, 400) without clearing its contents and redrawing it. I have a lot of fairly complex DVs to add to a larger one and they need to be moved regularly after they are drawn (usually all at once, and in a real-time application for which performance is quite important). They are drawn based on data that is generated over time. I have read elsewhere in this forum that using geometry.transform for layout is very costly relative to positioning visuals "with the cpu", but I'm not sure how to go about that.

Any advice is greatly appreciated. Thanks.


Re: Windows Presentation Foundation (WPF) Positioning DrawingVisual in parent visual?

Adam Smith - MSFT

You can set both an offset and a full Transform on DrawingVisuals, and you're correct that this should be much more efficient than changing the geometry transform or recreating the render data.




Re: Windows Presentation Foundation (WPF) Positioning DrawingVisual in parent visual?

Pickle Matrix Technician

Thanks for the suggestion. I tried using both the VisualOffset property and Transform property of a DrawingVisual before, during and after drawing it without any affect on its position, and I'm having difficulty finding useful info on the web. Can you please point me to some examples/tutorials or give a quick overview of how to use them I have used Transforms successfully by using DrawingContext.PushTransform, but that isn't appropriate for changing the position of an existing DrawingVisual in its parent.

Thanks.




Re: Windows Presentation Foundation (WPF) Positioning DrawingVisual in parent visual?

Adam Smith - MSFT

Correct, PushTransform won't do what you want. I'm a bit confused, as changing the Offset and/or Transform really should change the location of the DrawingVisual - can you show some example code






Re: Windows Presentation Foundation (WPF) Positioning DrawingVisual in parent visual?

Pickle Matrix Technician

I don't have code with me at the moment (on a break at work, hoping to find an answer by this evening though), but it is something along these lines:

Code Snippet

DrawingVisual myDV; //for example, as object instance var

...

myDV = new DrawingVisual();

DrawingContext dc = myDV.RenderOpen();

//drawing code in here, mostly with gradient brushes etc in this case

dc.Close();

someOtherParentVisual.AddVisualChild(myDV);

//everything above works just fine

...

//at some later point when I need to move my visual without redrawing its contents

myDV.Offset = new Vector(100, 100); //has no affect

myDV.Transform = new TranslateTransform(100, 100); //also has no affect

I did also try setting the offset and transform before adding the visual to its parent just to see if it would work, but the offset etc still had no affect.





Re: Windows Presentation Foundation (WPF) Positioning DrawingVisual in parent visual?

Pickle Matrix Technician

I tried using Offset again, doing exactly what I thought I did before, and am now having success. I wish I knew what I'm doing differently... thankyou for your help.