protected override void OnMouseDown(MouseEventArgs e) { if (GuiUtil.PtInRect(e.X, e.Y, this.ClientRectangle) == false) { // 防止在卷滚条上单击后拖动造成副作用 goto END1; } this.Capture = true; this.Focus(); // Graphics g = Graphics.FromHwnd(this.Handle); // g.TransformPoints(System.Drawing.Drawing2D.CoordinateSpace.Page, System.Drawing.Drawing2D.CoordinateSpace.World, pts); PointF p1 = ScreenToPhysic(new Point(e.X, e.Y)); // 屏幕坐标 _startHit = this.HitTest( p1.X, p1.Y); END1: base.OnMouseDown(e); }
// parameters: // x 内部坐标 HitTestResult HitTest(float x, float y) { HitTestResult result = new HitTestResult(); result.X = x; result.Y = y; result.CornerType = CornerType.None; if (this.Image == null) return result; Rectangle display_rect = GetPictureBoxZoomSize(); float x_ratio = (float)display_rect.Width / (float)this.Image.Width; float scale_ratio = (float)1 / x_ratio; // 左上 RectangleF rect = GetBoxRect(_points[0], scale_ratio); if (GuiUtil.PtInRect(x, y, rect) == true) { result.CornerType = CornerType.LeftTop; return result; } // 右上 rect = GetBoxRect(_points[1], scale_ratio); if (GuiUtil.PtInRect(x, y, rect) == true) { result.CornerType = CornerType.RightTop; return result; } // 右下 rect = GetBoxRect(_points[2], scale_ratio); if (GuiUtil.PtInRect(x, y, rect) == true) { result.CornerType = CornerType.RightBottom; return result; } // 左下 rect = GetBoxRect(_points[3], scale_ratio); if (GuiUtil.PtInRect(x, y, rect) == true) { result.CornerType = CornerType.LeftBottom; return result; } return result; }
protected override void OnMouseUp(MouseEventArgs e) { this.Capture = false; base.OnMouseUp(e); if (_startHit != null && _startHit.CornerType != CornerType.None) { PointF p1 = ScreenToPhysic(new Point(e.X, e.Y)); ChangeCornerPosition(_startHit.CornerType, Convert.ToInt32(p1.X), Convert.ToInt32(p1.Y)); } _startHit = null; }