using PaintDotNet.SystemLayer; using System; using System.Drawing; using System.Windows.Forms; namespace PaintDotNet { public class PanelEx : SystemLayer.ScrollPanel { private ScaleFactor scaleFactor; private bool hideHScroll = false; private bool hideVScroll = false; private bool response = false; public Rectangle[] realUpdateRects = null; public PanelEx() { SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true); } public bool Response { set { this.response = value; } } public bool HideVScroll { get { return this.hideVScroll; } set { this.hideVScroll = value; } } public bool HideHScroll { get { return this.hideHScroll; } set { this.hideHScroll = value; } } protected override void OnSizeChanged(EventArgs e) { if (this.hideHScroll || this.HideVScroll) { SystemLayer.UI.SuspendControlPainting(this); } base.OnSizeChanged(e); if (this.hideHScroll || this.HideVScroll) { if (this.hideHScroll && this.HideVScroll) { SystemLayer.UI.HideAllScrollBar(this); } else { if (this.hideHScroll) SystemLayer.UI.HideHorizontalScrollBar(this); if (this.HideVScroll) SystemLayer.UI.HideVerticalScrollBar(this); } SystemLayer.UI.ResumeControlPainting(this); Invalidate(true); } } protected override void WndProc(ref Message m) { IntPtr preR = m.Result; // Ignore focus if (m.Msg == 7 /* WM_SETFOCUS */) { return; } else if (m.Msg == 0x000f /* WM_PAINT */) { this.realUpdateRects = UI.GetUpdateRegion(this); if (this.realUpdateRects != null && this.realUpdateRects.Length >= 5) // '5' chosen arbitrarily { this.realUpdateRects = null; } base.WndProc(ref m); } else { base.WndProc(ref m); } } protected override void OnMouseWheel(MouseEventArgs e) { if (response) { response = false; base.OnMouseWheel(e); } } public ScaleFactor ScaleFactor { get { return this.scaleFactor; } set { this.scaleFactor = value; } } /// /// Converts from control client coordinates to surface coordinates /// This is useful when this.Bounds != surface.Bounds (i.e. some sort of zooming is in effect) /// /// /// public PointF ClientToSurface(PointF clientPt) { return ScaleFactor.UnscalePoint(clientPt); } public Point ClientToSurface(Point clientPt) { return ScaleFactor.UnscalePoint(clientPt); } public SizeF ClientToSurface(SizeF clientSize) { return ScaleFactor.UnscaleSize(clientSize); } public Size ClientToSurface(Size clientSize) { return Size.Round(ClientToSurface((SizeF)clientSize)); } public RectangleF ClientToSurface(RectangleF clientRect) { return new RectangleF(ClientToSurface(clientRect.Location), ClientToSurface(clientRect.Size)); } public Rectangle ClientToSurface(Rectangle clientRect) { return new Rectangle(ClientToSurface(clientRect.Location), ClientToSurface(clientRect.Size)); } public PointF SurfaceToClient(PointF surfacePt) { return ScaleFactor.ScalePoint(surfacePt); } public Point SurfaceToClient(Point surfacePt) { return ScaleFactor.ScalePoint(surfacePt); } public SizeF SurfaceToClient(SizeF surfaceSize) { return ScaleFactor.ScaleSize(surfaceSize); } public Size SurfaceToClient(Size surfaceSize) { return Size.Round(SurfaceToClient((SizeF)surfaceSize)); } public RectangleF SurfaceToClient(RectangleF surfaceRect) { return new RectangleF(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size)); } public Rectangle SurfaceToClient(Rectangle surfaceRect) { return new Rectangle(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size)); } } }