ScreenPage.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Window w = (Window)VisualRoot;
  23. var screens = w.Screens.All;
  24. var scaling = ((IRenderRoot)w).RenderScaling;
  25. Pen p = new Pen(Brushes.Black);
  26. if (screens != null)
  27. foreach (Screen screen in screens)
  28. {
  29. if (screen.Bounds.X / 10f < _leftMost)
  30. {
  31. _leftMost = screen.Bounds.X / 10f;
  32. InvalidateVisual();
  33. return;
  34. }
  35. Rect boundsRect = new Rect(screen.Bounds.X / 10f + Math.Abs(_leftMost), screen.Bounds.Y / 10f, screen.Bounds.Width / 10f,
  36. screen.Bounds.Height / 10f);
  37. Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
  38. screen.WorkingArea.Height / 10f);
  39. context.DrawRectangle(p, boundsRect);
  40. context.DrawRectangle(p, workingAreaRect);
  41. FormattedText text = new FormattedText()
  42. {
  43. Typeface = Typeface.Default
  44. };
  45. text.Text = $"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}";
  46. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height), text);
  47. text.Text = $"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}";
  48. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 20), text);
  49. text.Text = $"Primary: {screen.Primary}";
  50. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 40), text);
  51. text.Text = $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}";
  52. context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 60), text);
  53. }
  54. context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10, w.Bounds.Width / 10, w.Bounds.Height / 10));
  55. }
  56. }
  57. }