AspectRatio.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. /// <summary>
  4. /// Resizes controls on a form or user control automatically when the form resizes.
  5. /// </summary>
  6. namespace OmxPlayerGui
  7. {
  8. public class AspectRatio
  9. {
  10. /// <summary>
  11. /// Used to store information about the controls to resize.
  12. /// </summary>
  13. private struct ControlInfo
  14. {
  15. public double _DockLeft;
  16. public double _DockWidth;
  17. public double _DockTop;
  18. public double _DockHeight;
  19. public System.Windows.Forms.Control _Control;
  20. public double _OriginalLeft;
  21. public double _OriginalWidth;
  22. public double _OriginalTop;
  23. public double _OriginalHeight;
  24. public ControlInfo(double left, double width, double top, double height, System.Windows.Forms.Control control)
  25. {
  26. _DockLeft = left;
  27. _DockWidth = width;
  28. _DockTop = top;
  29. _DockHeight = height;
  30. _Control = control;
  31. _OriginalLeft = control.Left;
  32. _OriginalWidth = control.Width;
  33. _OriginalTop = control.Top;
  34. _OriginalHeight = control.Height;
  35. }
  36. public void Resize(double widthChange,double heightChange)
  37. {
  38. if (_DockLeft != 0)
  39. {
  40. // DOCK LEFT.
  41. _Control.Left = (int)(_OriginalLeft + widthChange * _DockLeft);
  42. }
  43. if (_DockWidth != 0)
  44. {
  45. // DOCK RIGHT.
  46. _Control.Width = (int)(_OriginalWidth + widthChange * _DockWidth);
  47. }
  48. if (_DockTop != 0)
  49. {
  50. // DOCK TOP.
  51. _Control.Top = (int)(_OriginalTop + heightChange * _DockTop);
  52. }
  53. if (_DockHeight != 0)
  54. {
  55. // DOCK BOTTOM.
  56. _Control.Height = (int)(_OriginalHeight + heightChange * _DockHeight);
  57. }
  58. return;
  59. }
  60. }
  61. private System.Windows.Forms.Form _ParentForm;
  62. private System.Windows.Forms.Control _ParentControl;
  63. private List<ControlInfo> _ControlInfo;
  64. private double _OriginalWidth;
  65. private double _OriginalHeight;
  66. /// <summary>
  67. /// Constructor
  68. /// </summary>
  69. /// <param name="parentForm">Form that has the controls to be resized</param>
  70. public AspectRatio(System.Windows.Forms.Form parentForm)
  71. {
  72. _ParentForm = parentForm;
  73. _ControlInfo = new List<ControlInfo>();
  74. _OriginalWidth = parentForm.Width;
  75. _OriginalHeight = parentForm.Height;
  76. parentForm.Resize += new System.EventHandler(this.ResizeEvent_Form);
  77. }
  78. /// <summary>
  79. /// Constructor
  80. /// </summary>
  81. /// <param name="parentControl">Control that has the controls to be resized.</param>
  82. public AspectRatio(System.Windows.Forms.Control parentControl)
  83. {
  84. _ParentControl = parentControl;
  85. _ControlInfo = new List<ControlInfo>();
  86. _OriginalWidth = parentControl.Width;
  87. _OriginalHeight = parentControl.Height;
  88. parentControl.Resize += new System.EventHandler(this.ResizeEvent_Control);
  89. }
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. /// <param name="controlToAdd">Control to adjust when the form resizes</param>
  94. /// <param name="dockLeft">0 to leave Control.Left alone.1 - dock. 0.5 - center.</param>
  95. /// <param name="dockWidth">0 to leave Control.Width alone. 1 - dock. 0.5 - center</param>
  96. /// <param name="dockTop">0 to leave Control.Top alone. 1 - dock. 0.5 - center</param>
  97. /// <param name="dockHeight">0 to leave Control.Height alone. 1 - dock. 0.5 - center</param>
  98. public void AddControl(System.Windows.Forms.Control controlToAdd, double dockLeft, double dockWidth, double dockTop, double dockHeight)
  99. {
  100. // oControl IS THE CONTROL TO BE RESIZED WHEN THE FORM IS RESIZED.
  101. // bDockLeft: The Left property is changed.
  102. // bDockWidth: The Width property is changed.
  103. // bDockTop: The Top property is changed.
  104. // bDockHeight: The Height property is changed.
  105. if ((dockLeft==0) && (dockWidth==0) && (dockTop==0) && (dockHeight==0))
  106. {
  107. // DO NOTHING.
  108. return;
  109. }
  110. _ControlInfo.Add(new ControlInfo(dockLeft, dockWidth, dockTop, dockHeight, controlToAdd));
  111. return;
  112. }
  113. /// <summary>
  114. /// This event is fired after the ResizeEvent_Form is finished resizing.
  115. /// </summary>
  116. public EventHandler AfterResize = null;
  117. private void ResizeEvent_Form(object sender, System.EventArgs e)
  118. {
  119. // CALLED FROM THE RESIZE EVENT OF A FORM.
  120. // THE CONTRUCTOR OF THIS CLASS AUTOMATICALLY
  121. // ADDS AN EVENT TO JUMP TO THIS FUNCTION FOR Form_Resize.
  122. ResizeControls(_ParentForm.Width - _OriginalWidth, _ParentForm.Height - _OriginalHeight,sender,e);
  123. return;
  124. }
  125. private void ResizeEvent_Control(object sender, System.EventArgs e)
  126. {
  127. // CALLED FROM THE RESIZE EVENT OF A CONTROL.
  128. // THE CONTRUCTOR OF THIS CLASS AUTOMATICALLY
  129. // ADDS AN EVENT TO JUMP TO THIS FUNCTION FOR Form_Resize.
  130. ResizeControls(_ParentControl.Width - _OriginalWidth, _ParentControl.Height - _OriginalHeight, sender, e);
  131. return;
  132. }
  133. private void ResizeControls(double widthChange, double heightChange, object sender, System.EventArgs e)
  134. {
  135. // Called from ResizeEvent_Control and ResizeEvent_Form.
  136. foreach (ControlInfo loop in _ControlInfo)
  137. {
  138. loop.Resize(widthChange, heightChange);
  139. }
  140. // Pass the resize event on.
  141. if (AfterResize != null)
  142. {
  143. AfterResize(sender, e);
  144. }
  145. return;
  146. }
  147. }
  148. }