PanelEx.cs 5.3 KB

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