ScreenPage.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media;
  7. using Avalonia.Platform;
  8. using Avalonia.Rendering;
  9. namespace ControlCatalog.Pages
  10. {
  11. public class ScreenPage : UserControl
  12. {
  13. private double _leftMost;
  14. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  15. {
  16. base.OnAttachedToVisualTree(e);
  17. Window w = (Window)VisualRoot;
  18. w.PositionChanged += (sender, args) => InvalidateVisual();
  19. }
  20. public override void Render(DrawingContext context)
  21. {
  22. base.Render(context);
  23. if (!(VisualRoot is Window w))
  24. {
  25. return;
  26. }
  27. var screens = w.Screens.All;
  28. var scaling = ((IRenderRoot)w).RenderScaling;
  29. var drawBrush = Brushes.Black;
  30. Pen p = new Pen(drawBrush);
  31. if (screens != null)
  32. foreach (Screen screen in screens)
  33. {
  34. if (screen.Bounds.X / 10f < _leftMost)
  35. {
  36. _leftMost = screen.Bounds.X / 10f;
  37. InvalidateVisual();
  38. return;
  39. }
  40. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f, screen.Bounds.Width / 10f,
  41. screen.Bounds.Height / 10f);
  42. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
  43. screen.WorkingArea.Height / 10f);
  44. context.DrawRectangle(p, boundsRect);
  45. context.DrawRectangle(p, workingAreaRect);
  46. var formattedText = CreateFormattedText($"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}");
  47. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height));
  48. formattedText =
  49. CreateFormattedText($"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}");
  50. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 20));
  51. formattedText = CreateFormattedText($"Scaling: {screen.PixelDensity * 100}%");
  52. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 40));
  53. formattedText = CreateFormattedText($"Primary: {screen.Primary}");
  54. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 60));
  55. formattedText =
  56. CreateFormattedText(
  57. $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}");
  58. context.DrawText(formattedText, boundsRect.Position.WithY(boundsRect.Size.Height + 80));
  59. }
  60. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10f, w.Bounds.Width / 10, w.Bounds.Height / 10));
  61. }
  62. private FormattedText CreateFormattedText(string textToFormat)
  63. {
  64. return new FormattedText(textToFormat, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
  65. Typeface.Default, 12, Brushes.Green);
  66. }
  67. }
  68. }