@@ -32,10 +32,12 @@ public MyRectangle(Color myColor, Rect myRect, string caption)
32
32
// Draw a rectangle into the drawing context.
33
33
dc . DrawRectangle ( new SolidColorBrush ( myColor ) , null , myRect ) ;
34
34
35
+ DpiScale dpiScale = VisualTreeHelper . GetDpi ( Application . Current . MainWindow ) ;
36
+
35
37
// Create a text string and draw it in the drawing context.
36
38
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 ) ;
39
41
dc . DrawText ( formattedText , new Point ( myRect . Left + 10 , myRect . Top + 5 ) ) ;
40
42
41
43
// Close the drawing context to persist the changes.
@@ -80,7 +82,7 @@ void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseBut
80
82
81
83
//<Snippet101>
82
84
// Enumerate all the descendants of the visual object.
83
- static public void EnumVisual ( Visual myVisual )
85
+ public static void EnumVisual ( Visual myVisual )
84
86
{
85
87
for ( int i = 0 ; i < VisualTreeHelper . GetChildrenCount ( myVisual ) ; i ++ )
86
88
{
@@ -96,7 +98,7 @@ static public void EnumVisual(Visual myVisual)
96
98
//</Snippet101>
97
99
98
100
// 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 )
100
102
{
101
103
//<Snippet102>
102
104
// Return the bounding rectangle of the parent visual object and all of its descendants.
@@ -108,15 +110,15 @@ static public Rect GetBoundingRectangle(Visual parentVisual)
108
110
109
111
//<SnippetVisualsOverviewSnippet4>
110
112
// 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 )
112
114
{
113
115
// Retrieve the group of drawings for the visual.
114
116
DrawingGroup drawingGroup = VisualTreeHelper . GetDrawing ( visual ) ;
115
117
EnumDrawingGroup ( drawingGroup , pt ) ;
116
118
}
117
119
118
120
// Enumerate the drawings in the DrawingGroup.
119
- static public void EnumDrawingGroup ( DrawingGroup drawingGroup , Point pt )
121
+ public static void EnumDrawingGroup ( DrawingGroup drawingGroup , Point pt )
120
122
{
121
123
DrawingCollection drawingCollection = drawingGroup . Children ;
122
124
@@ -141,7 +143,7 @@ static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
141
143
//</SnippetVisualsOverviewSnippet4>
142
144
143
145
// 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 )
145
147
{
146
148
for ( int i = 0 ; i < VisualTreeHelper . GetChildrenCount ( myVisual ) ; i ++ )
147
149
{
@@ -171,10 +173,11 @@ private DrawingVisual CreateDrawingVisualEllipses()
171
173
dc . DrawEllipse ( Brushes . Gray , null , new Point ( 430 , 136 ) , 20 , 20 ) ;
172
174
dc . DrawEllipse ( Brushes . SteelBlue , null , new Point ( 480 , 136 ) , 20 , 20 ) ;
173
175
dc . DrawEllipse ( Brushes . Maroon , null , new Point ( 530 , 136 ) , 20 , 20 ) ;
176
+ DpiScale dpiScale = VisualTreeHelper . GetDpi ( Application . Current . MainWindow ) ;
174
177
// Create a text string and draw it in the drawing context.
175
178
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 ) ;
178
181
dc . DrawText ( formattedText , new Point ( 430 - 12 , 136 - 12 ) ) ;
179
182
dc . Close ( ) ;
180
183
@@ -212,4 +215,4 @@ protected override Size ArrangeOverride(Size finalSize)
212
215
return base . ArrangeOverride ( finalSize ) ;
213
216
}
214
217
}
215
- }
218
+ }
0 commit comments