XAML-Map-Control/MapControl/WPF/MapGraticule.WPF.cs

95 lines
3.3 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace MapControl
{
public partial class MapGraticule : FrameworkElement, IMapElement
{
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, Brush>(TextElement.ForegroundProperty);
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty, 12d);
2024-05-26 20:32:29 +02:00
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
2025-12-27 21:24:01 +01:00
get;
2024-05-26 20:32:29 +02:00
set
{
2025-12-27 21:24:01 +01:00
if (field != null)
2024-05-26 20:32:29 +02:00
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged -= OnViewportChanged;
2024-05-26 20:32:29 +02:00
}
2025-12-27 21:24:01 +01:00
field = value;
2024-05-26 20:32:29 +02:00
2025-12-27 21:24:01 +01:00
if (field != null)
2024-05-26 20:32:29 +02:00
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged += OnViewportChanged;
2024-05-26 20:32:29 +02:00
}
}
}
2024-05-26 20:32:29 +02:00
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
InvalidateVisual();
}
protected override void OnRender(DrawingContext drawingContext)
{
2025-12-27 21:24:01 +01:00
if (ParentMap != null)
{
2022-04-27 18:00:40 +02:00
var pathGeometry = new PathGeometry();
2022-04-28 18:34:30 +02:00
var labels = DrawGraticule(pathGeometry.Figures);
2022-03-04 22:28:18 +01:00
2024-05-26 20:32:29 +02:00
var pen = new Pen
{
Brush = Foreground,
Thickness = StrokeThickness,
};
drawingContext.DrawGeometry(null, pen, pathGeometry);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
if (labels.Count > 0)
2022-03-04 22:28:18 +01:00
{
var typeface = new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
2022-04-28 18:34:30 +02:00
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
foreach (var label in labels)
{
var latText = new FormattedText(label.LatitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var lonText = new FormattedText(label.LongitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var x = label.X + StrokeThickness / 2d + 2d;
var y1 = label.Y - StrokeThickness / 2d - latText.Height;
var y2 = label.Y + StrokeThickness / 2d;
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(latText, new Point(x, y1));
drawingContext.DrawText(lonText, new Point(x, y2));
drawingContext.Pop();
}
2022-03-05 18:40:57 +01:00
}
2022-03-04 22:28:18 +01:00
}
}
2026-02-01 01:42:24 +01:00
private static PolyLineSegment CreatePolyLineSegment(IEnumerable<Point> points)
2022-03-04 22:28:18 +01:00
{
2026-02-01 01:42:24 +01:00
return new PolyLineSegment(points, true);
}
}
}