ScreenPage.cs 3.2 KB

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