PanelEx.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using SmartCoalApplication.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace SmartCoalApplication.Core
  10. {
  11. public class PanelEx : ScrollPanel
  12. {
  13. private ScaleFactor scaleFactor;
  14. private bool hideHScroll = false;
  15. private bool hideVScroll = false;
  16. private bool response = false;
  17. public Rectangle[] realUpdateRects = null;
  18. public PanelEx()
  19. {
  20. SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
  21. }
  22. public bool Response
  23. {
  24. set
  25. {
  26. this.response = value;
  27. }
  28. }
  29. public bool HideVScroll
  30. {
  31. get
  32. {
  33. return this.hideVScroll;
  34. }
  35. set
  36. {
  37. this.hideVScroll = value;
  38. }
  39. }
  40. public bool HideHScroll
  41. {
  42. get
  43. {
  44. return this.hideHScroll;
  45. }
  46. set
  47. {
  48. this.hideHScroll = value;
  49. }
  50. }
  51. protected override void OnSizeChanged(EventArgs e)
  52. {
  53. if (this.hideHScroll || this.HideVScroll)
  54. {
  55. SystemLayer.UI.SuspendControlPainting(this);
  56. }
  57. base.OnSizeChanged(e);
  58. if (this.hideHScroll || this.HideVScroll)
  59. {
  60. if (this.hideHScroll && this.HideVScroll)
  61. {
  62. SystemLayer.UI.HideAllScrollBar(this);
  63. }
  64. else
  65. {
  66. if (this.hideHScroll)
  67. SystemLayer.UI.HideHorizontalScrollBar(this);
  68. if (this.HideVScroll)
  69. SystemLayer.UI.HideVerticalScrollBar(this);
  70. }
  71. SystemLayer.UI.ResumeControlPainting(this);
  72. Invalidate(true);
  73. }
  74. }
  75. protected override void WndProc(ref Message m)
  76. {
  77. if (m.Msg == 7)
  78. {
  79. return;
  80. }
  81. else if (m.Msg == 0x000f)
  82. {
  83. this.realUpdateRects = UI.GetUpdateRegion(this);
  84. if (this.realUpdateRects != null &&
  85. this.realUpdateRects.Length >= 5)
  86. {
  87. this.realUpdateRects = null;
  88. }
  89. base.WndProc(ref m);
  90. }
  91. else
  92. {
  93. base.WndProc(ref m);
  94. }
  95. }
  96. protected override void OnMouseWheel(MouseEventArgs e)
  97. {
  98. if (response)
  99. {
  100. response = false;
  101. base.OnMouseWheel(e);
  102. }
  103. }
  104. public ScaleFactor ScaleFactor
  105. {
  106. get
  107. {
  108. return this.scaleFactor;
  109. }
  110. set
  111. {
  112. this.scaleFactor = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Converts from control client coordinates to surface coordinates
  117. /// This is useful when this.Bounds != surface.Bounds (i.e. some sort of zooming is in effect)
  118. /// </summary>
  119. /// <param name="clientPt"></param>
  120. /// <returns></returns>
  121. public PointF ClientToSurface(PointF clientPt)
  122. {
  123. return ScaleFactor.UnscalePoint(clientPt);
  124. }
  125. public Point ClientToSurface(Point clientPt)
  126. {
  127. return ScaleFactor.UnscalePoint(clientPt);
  128. }
  129. public SizeF ClientToSurface(SizeF clientSize)
  130. {
  131. return ScaleFactor.UnscaleSize(clientSize);
  132. }
  133. public Size ClientToSurface(Size clientSize)
  134. {
  135. return Size.Round(ClientToSurface((SizeF)clientSize));
  136. }
  137. public RectangleF ClientToSurface(RectangleF clientRect)
  138. {
  139. return new RectangleF(ClientToSurface(clientRect.Location), ClientToSurface(clientRect.Size));
  140. }
  141. public Rectangle ClientToSurface(Rectangle clientRect)
  142. {
  143. return new Rectangle(ClientToSurface(clientRect.Location), ClientToSurface(clientRect.Size));
  144. }
  145. public PointF SurfaceToClient(PointF surfacePt)
  146. {
  147. return ScaleFactor.ScalePoint(surfacePt);
  148. }
  149. public Point SurfaceToClient(Point surfacePt)
  150. {
  151. return ScaleFactor.ScalePoint(surfacePt);
  152. }
  153. public SizeF SurfaceToClient(SizeF surfaceSize)
  154. {
  155. return ScaleFactor.ScaleSize(surfaceSize);
  156. }
  157. public Size SurfaceToClient(Size surfaceSize)
  158. {
  159. return Size.Round(SurfaceToClient((SizeF)surfaceSize));
  160. }
  161. public RectangleF SurfaceToClient(RectangleF surfaceRect)
  162. {
  163. return new RectangleF(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size));
  164. }
  165. public Rectangle SurfaceToClient(Rectangle surfaceRect)
  166. {
  167. return new Rectangle(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size));
  168. }
  169. }
  170. }