ResolutionAdapter.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Shapes;
  9. using System.Windows;
  10. namespace ivf_tl_Manage
  11. {
  12. public class ResolutionAdapter
  13. {
  14. public double wB { get; set; }
  15. public double hB { get; set; }
  16. public void EnumVisualTree(int Ident, Visual visualObj)
  17. {
  18. wB = GetScreenWidth() / 1920d;
  19. hB = GetScreenHeight() / 1080d;
  20. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualObj); i++)
  21. {
  22. Visual childVisual = (Visual)VisualTreeHelper.GetChild(visualObj, i);
  23. if (childVisual is Border)
  24. Console.WriteLine("");
  25. var uie = childVisual as FrameworkElement;
  26. if (uie != null)
  27. {
  28. SetElement(wB, hB, uie);
  29. if (uie is Grid)
  30. {
  31. Grid grid = uie as Grid;
  32. grid.Width = grid.Width * wB;
  33. grid.Height = grid.Height * hB;
  34. var cols = grid.ColumnDefinitions.ToArray();
  35. foreach (var col in cols)
  36. {
  37. col.Width =new GridLength(col.ActualWidth*wB);
  38. }
  39. }
  40. if (uie is TextBlock)
  41. {
  42. var tbElement = uie as TextBlock;
  43. tbElement.FontSize = tbElement.FontSize * wB;
  44. }
  45. if (uie is Label)
  46. {
  47. var lblElement = uie as Label;
  48. lblElement.FontSize = lblElement.FontSize * wB;
  49. }
  50. if (uie is Border)
  51. {
  52. var borderElement = uie as Border;
  53. CornerRadius radius = borderElement.CornerRadius;
  54. borderElement.CornerRadius = new CornerRadius(radius.TopLeft * hB, radius.TopRight * hB, radius.BottomRight * hB, radius.BottomLeft * hB);
  55. }
  56. if (uie is Ellipse)
  57. {
  58. Ellipse ellipse = uie as Ellipse;
  59. double temp = ellipse.Height;
  60. ellipse.Width = temp * hB;
  61. ellipse.Height = temp * hB;
  62. }
  63. }
  64. EnumVisualTree(Ident + 1, childVisual);
  65. }
  66. }
  67. private void SetElement(double wB, double hB, FrameworkElement uie)
  68. {
  69. uie.Width = uie.Width * wB;
  70. uie.Height = uie.Height * hB;
  71. uie.SetValue(Canvas.LeftProperty, (double)uie.GetValue(Canvas.LeftProperty) * wB);
  72. uie.SetValue(Canvas.TopProperty, (double)uie.GetValue(Canvas.TopProperty) * hB);
  73. uie.Margin = new Thickness(uie.Margin.Left * wB, uie.Margin.Top * hB, uie.Margin.Right * wB, uie.Margin.Bottom * hB);
  74. }
  75. public double GetScreenWidth()
  76. {
  77. return SystemParameters.PrimaryScreenWidth;
  78. }
  79. public double GetScreenHeight()
  80. {
  81. return SystemParameters.PrimaryScreenHeight;
  82. }
  83. }
  84. }