ScreenPage.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. {
  41. Typeface = Typeface.Default
  42. };
  43. text.Text = $"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}";
  44. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height), text);
  45. text.Text = $"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}";
  46. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 20), text);
  47. text.Text = $"Primary: {screen.Primary}";
  48. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 40), text);
  49. text.Text = $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new Rect(w.Position, w.Bounds.Size)))}";
  50. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 60), text);
  51. }
  52. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10, w.Bounds.Width / 10, w.Bounds.Height / 10));
  53. }
  54. }
  55. }