/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (!m_bVisible) return; int nX = e.X - this.AutoScrollPosition.X; int nY = e.Y - this.AutoScrollPosition.Y; // m_MouseDownPosition = new Point(nX, nY); if (IsMouseInImageBoxBounds(nX, nY, 2)) { if (e.Button == m_nRectMoveMouseButton) { if (m_bAllowMouseMove) { m_nImageBoxMode = ImageBoxMode.Move; m_nRectMousePosX = nX - m_ImageBoxRect.X; m_nRectMousePosY = nY - m_ImageBoxRect.Y; Cursor.Current = Cursors.SizeAll; } } // Fire rectangle mouse down event. OnImageBoxMouseDown(this, new ImageBoxMouseEventArgs(e, m_ImageBoxRect)); } else { if (e.Button == m_nRectResizeMouseButton) { if (m_bAllowMouseResize) { ImageBoxBounds nOnBounds; if (IsMouseOnImageBoxBounds(nX, nY, ImageBoxBounds.Any, out nOnBounds)) { m_nImageBoxMode = ImageBoxMode.Resize; m_nBoundsSelected = nOnBounds; switch (nOnBounds) { case ImageBoxBounds.None: Cursor.Current = Cursors.Default; break; case ImageBoxBounds.Top: Cursor.Current = Cursors.SizeNS; break; case ImageBoxBounds.Bottom: Cursor.Current = Cursors.SizeNS; break; case ImageBoxBounds.Left: Cursor.Current = Cursors.SizeWE; break; case ImageBoxBounds.Right: Cursor.Current = Cursors.SizeWE; break; case ImageBoxBounds.TopLeft: Cursor.Current = Cursors.SizeNWSE; break; case ImageBoxBounds.TopRight: Cursor.Current = Cursors.SizeNESW; break; case ImageBoxBounds.BottomLeft: Cursor.Current = Cursors.SizeNESW; break; case ImageBoxBounds.BottomRight: Cursor.Current = Cursors.SizeNWSE; break; } } } } } if (m_bFocusControl) { if (!this.Focused) this.Focus(); } }
/// <summary> /// Returns true if the specified coordinates are on the specified bounds /// of the image box. /// </summary> /// <param name="nX">Specifies the X position.</param> /// <param name="nY">Specifies the Y position.</param> /// <param name="nBounds">Specifies the bounds to check for.</param> /// <returns></returns> private bool IsMouseOnImageBoxBounds(int nX, int nY, ImageBoxBounds nBounds) { int nWidthOffset = 1; int nLengthOffset = 2; int nRectBottom = m_ImageBoxRect.Bottom; int nRectRight = m_ImageBoxRect.Right; int nRectPosX = m_ImageBoxRect.X; int nRectPosY = m_ImageBoxRect.Y; int nRectWidth = m_ImageBoxRect.Width; int nRectHeight = m_ImageBoxRect.Height; if ((nRectWidth > 0) & (nRectHeight > 0)) { if (nBounds == ImageBoxBounds.Top) { // Top bounds width check. if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { // Top bounds length check. if ((nX >= nRectPosX + nLengthOffset) && (nX <= nRectRight - nLengthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.Bottom) { // Bottom bounds width check. if ((nY <= nRectBottom + nWidthOffset) && (nY >= nRectBottom - nWidthOffset)) { // Bottom bounds length check. if ((nX >= nRectPosX + nLengthOffset) && (nX <= nRectRight - nLengthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.Left) { // Left bounds width check. if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // Left bounds length check. if ((nY >= nRectPosY + nLengthOffset) && (nY <= nRectBottom - nLengthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.Right) { // Right bounds width check. if ((nX <= nRectRight + nWidthOffset) && (nX >= nRectRight - nWidthOffset)) { // Right bounds length check. if ((nY >= nRectPosY + nLengthOffset) && (nY <= nRectBottom - nLengthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.TopLeft) { // Top left bounds width check if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // Top left bounds height check if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.TopRight) { // Top right bounds width check if ((nX >= nRectRight - nWidthOffset) && (nX <= nRectRight + nWidthOffset)) { // Top right bounds height check. if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.BottomLeft) { // Bottom left bounds width check if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // bottom left bounds height check. if ((nY >= nRectBottom - nWidthOffset) && (nY <= nRectBottom + nWidthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.BottomRight) { // Bottom right bounds width check. if ((nX >= nRectRight - nWidthOffset) && (nX <= nRectRight + nWidthOffset)) { // bottom right bounds height check. if ((nY >= nRectBottom - nWidthOffset) && (nY <= nRectBottom + nWidthOffset)) { return true; } } } else if (nBounds == ImageBoxBounds.Any) { // Top bounds width check. if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { // Top bounds length check. if ((nX >= nRectPosX + nLengthOffset) && (nX <= nRectRight - nLengthOffset)) { return true; } } // Bottom bounds width check. if ((nY <= nRectBottom + nWidthOffset) && (nY >= nRectBottom - nWidthOffset)) { // Bottom bounds length check. if ((nX >= nRectPosX + nLengthOffset) && (nX <= nRectRight - nLengthOffset)) { return true; } } // Left bounds width check. if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // Left bounds length check. if ((nY >= nRectPosY + nLengthOffset) && (nY <= nRectBottom - nLengthOffset)) { return true; } } // Right bounds width check. if ((nX <= nRectRight + nWidthOffset) && (nX >= nRectRight - nWidthOffset)) { // Right bounds length check. if ((nY >= nRectPosY + nLengthOffset) && (nY <= nRectBottom - nLengthOffset)) { return true; } } // Top left bounds width check if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // Top left bounds height check if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { return true; } } // Top right bounds width check if ((nX >= nRectRight - nWidthOffset) && (nX <= nRectRight + nWidthOffset)) { // Top right bounds height check. if ((nY >= nRectPosY - nWidthOffset) && (nY <= nRectPosY + nWidthOffset)) { return true; } } // Bottom left bounds width check if ((nX >= nRectPosX - nWidthOffset) && (nX <= nRectPosX + nWidthOffset)) { // bottom left bounds height check. if ((nY >= nRectBottom - nWidthOffset) && (nY <= nRectBottom + nWidthOffset)) { return true; } } // Bottom right bounds width check. if ((nX >= nRectRight - nWidthOffset) && (nX <= nRectRight + nWidthOffset)) { // bottom right bounds height check. if ((nY >= nRectBottom - nWidthOffset) && (nY <= nRectBottom + nWidthOffset)) { return true; } } } } return false; }
/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (!m_bVisible) return; // Set flags. m_nImageBoxMode = ImageBoxMode.None; m_nBoundsSelected = ImageBoxBounds.None; m_MouseDownPosition = Point.Empty; if (IsMouseInImageBoxBounds(e.X - this.AutoScrollPosition.X, e.Y - this.AutoScrollPosition.Y, 2)) { OnImageBoxMouseUp(this, new ImageBoxMouseEventArgs(e, m_ImageBoxRect)); } }