Skip to content

Commit 107a574

Browse files
authored
Do not use the obsolete API in VisualTreeHelper (#8734)
1 parent fddd7a8 commit 107a574

File tree

1 file changed

+13
-10
lines changed
  • snippets/csharp/System.Windows.Media/VisualTreeHelper/Overview

1 file changed

+13
-10
lines changed

snippets/csharp/System.Windows.Media/VisualTreeHelper/Overview/Window1.xaml.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public MyRectangle(Color myColor, Rect myRect, string caption)
3232
// Draw a rectangle into the drawing context.
3333
dc.DrawRectangle(new SolidColorBrush(myColor), null, myRect);
3434

35+
DpiScale dpiScale = VisualTreeHelper.GetDpi(Application.Current.MainWindow);
36+
3537
// Create a text string and draw it in the drawing context.
3638
FormattedText formattedText = new FormattedText(caption, CultureInfo.CurrentCulture,
37-
FlowDirection.LeftToRight,
38-
new Typeface("Verdana"), 36, Brushes.Black);
39+
FlowDirection.LeftToRight,
40+
new Typeface("Verdana"), 36, Brushes.Black, dpiScale.PixelsPerDip);
3941
dc.DrawText(formattedText, new Point(myRect.Left + 10, myRect.Top + 5));
4042

4143
// Close the drawing context to persist the changes.
@@ -80,7 +82,7 @@ void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseBut
8082

8183
//<Snippet101>
8284
// Enumerate all the descendants of the visual object.
83-
static public void EnumVisual(Visual myVisual)
85+
public static void EnumVisual(Visual myVisual)
8486
{
8587
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
8688
{
@@ -96,7 +98,7 @@ static public void EnumVisual(Visual myVisual)
9698
//</Snippet101>
9799

98100
// Get the combined bounding rectangle of the parent visual and its descendants.
99-
static public Rect GetBoundingRectangle(Visual parentVisual)
101+
public static Rect GetBoundingRectangle(Visual parentVisual)
100102
{
101103
//<Snippet102>
102104
// Return the bounding rectangle of the parent visual object and all of its descendants.
@@ -108,15 +110,15 @@ static public Rect GetBoundingRectangle(Visual parentVisual)
108110

109111
//<SnippetVisualsOverviewSnippet4>
110112
// Determine if a geometry within the visual was hit.
111-
static public void HitTestGeometryInVisual(Visual visual, Point pt)
113+
public static void HitTestGeometryInVisual(Visual visual, Point pt)
112114
{
113115
// Retrieve the group of drawings for the visual.
114116
DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(visual);
115117
EnumDrawingGroup(drawingGroup, pt);
116118
}
117119

118120
// Enumerate the drawings in the DrawingGroup.
119-
static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
121+
public static void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
120122
{
121123
DrawingCollection drawingCollection = drawingGroup.Children;
122124

@@ -141,7 +143,7 @@ static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
141143
//</SnippetVisualsOverviewSnippet4>
142144

143145
// Find a DrawingVisual in the hit object.
144-
static public void FindDrawingVisual(Visual myVisual, Point pt)
146+
public static void FindDrawingVisual(Visual myVisual, Point pt)
145147
{
146148
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
147149
{
@@ -171,10 +173,11 @@ private DrawingVisual CreateDrawingVisualEllipses()
171173
dc.DrawEllipse(Brushes.Gray, null, new Point(430, 136), 20, 20);
172174
dc.DrawEllipse(Brushes.SteelBlue, null, new Point(480, 136), 20, 20);
173175
dc.DrawEllipse(Brushes.Maroon, null, new Point(530, 136), 20, 20);
176+
DpiScale dpiScale = VisualTreeHelper.GetDpi(Application.Current.MainWindow);
174177
// Create a text string and draw it in the drawing context.
175178
FormattedText formattedText = new FormattedText("Hi", CultureInfo.CurrentCulture,
176-
FlowDirection.LeftToRight,
177-
new Typeface("Verdana"), 24, Brushes.Black);
179+
FlowDirection.LeftToRight,
180+
new Typeface("Verdana"), 24, Brushes.Black, dpiScale.PixelsPerDip);
178181
dc.DrawText(formattedText, new Point(430-12, 136-12));
179182
dc.Close();
180183

@@ -212,4 +215,4 @@ protected override Size ArrangeOverride(Size finalSize)
212215
return base.ArrangeOverride(finalSize);
213216
}
214217
}
215-
}
218+
}

0 commit comments

Comments
 (0)