ScreenPage.cs 2.8 KB

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