Ask a questionAsk a question
 

AnswerHow to move a Shape?

  • Tuesday, October 27, 2009 3:49 PMSergiu Dudnic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    As my precedent question was the most probably a PowerPacks bug, my following question is more direct. How to move a OvalShape?


    I have a simple code, that works only if I move the shape little by little, without quit the shape area. I would like to move it in a "usual" (even if the cursor temporary leaves the shape, the shape should follow the cursor) way.

    Imports Microsoft.VisualBasic.PowerPacks
    
    Public Class Form1
      Private mbox As New MoveBox
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mbox.Location = New Point(30, 30)
        ShapeContainer1.Shapes.Add(mbox)
      End Sub
    
    End Class
    
    
    
    Public Class MoveBox
      Inherits OvalShape
    
      Private _isActive As Boolean
      Public Sub New()
        Me.Size = New Size(10, 10)
        Me.BackStyle = PowerPacks.BackStyle.Opaque
        Me.BackColor = Color.Blue
        Me.BorderColor = Color.Red
      End Sub
    
      Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        _isActive = True
      End Sub
    
      Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        _isActive = False
      End Sub
    
      Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        If Me._isActive Then
          Me.Location = Me.Location + e.Location - New Point(Me.Width \ 2, Me.Height \ 2)
        End If
      End Sub
    End Class
    
    Thanks.
    Best regards, Sergiu

Answers

All Replies

  • Friday, October 30, 2009 8:41 AMXingwei HuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi Sergiu Dudnic,

    The following example shows how to use the MouseDown, MouseMove, and MouseUp events to draw lines on a RectangleShape control. This example requires that you have a RectangleShape control named RectangleShape1 on a form.

    Private mousePath = New System.Drawing.Drawing2D.GraphicsPath()
    
    Private Sub RectangleShape2_MouseDown(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
      RectangleShape2.MouseDown
        Dim mouseDownLocation As New Point(e.X + RectangleShape2.Left, _
          e.Y + RectangleShape2.Top)
        ' Clear the previous line.
        mousePath.Dispose()
        mousePath = New System.Drawing.Drawing2D.GraphicsPath()
        RectangleShape2.Invalidate()
        ' Add a line to the graphics path.
        mousePath.AddLine(mouseDownLocation, mouseDownLocation)
    End Sub
    
    Private Sub RectangleShape2_MouseMove(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) _
      Handles RectangleShape2.MouseMove
        Dim mouseX As Integer = e.X + RectangleShape2.Left
        Dim mouseY As Integer = e.Y + RectangleShape2.Top
        ' Add a line to the graphics path.
        mousePath.AddLine(mouseX, mouseY, mouseX, mouseY)
    End Sub
    
    Private Sub RectangleShape2_MouseUp(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) _
      Handles RectangleShape2.MouseUp
        Dim mouseUpLocation = New System.Drawing.Point(e.X + _
          RectangleShape2.Left, e.Y + RectangleShape2.Top)
        ' Add a line to the graphics path.
        mousePath.Addline(mouseUpLocation, mouseUpLocation)
        ' Force the shape to redraw.
        RectangleShape2.Invalidate()
    End Sub
    
    Private Sub RectangleShape2_Paint(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) _
      Handles RectangleShape2.Paint
        ' Draw the line.
        e.Graphics.DrawPath(System.Drawing.Pens.DarkRed, mousePath)
    End Sub
    
    



    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.shape.mousemove.aspx


    Best Wishes

    XIngwei Hu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, October 30, 2009 11:10 AMSergiu Dudnic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you, XIngwei Hu

    the example you copied from the MSDN demonstrates how to draw a line with the mouse, drawing the paths.

    In my case I need to do a completely different thing.

    I have a little red circle (Oval Shape Control). I need to move it(the control) on the panel with the mouse cursor.

    If you test ly code on a simple application, you will see that it works (moves the shape) but in a little steps (until the cursor not leaves the control, then the movement, unfortunately, stops)

    Best regards, Sergiu
  • Thursday, November 05, 2009 3:41 AMJohn Chen MSModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sergiu, I undestand your question, the problem is you are only handle/listen the mouse move enent on the OvalShape, that explain why you can't move out of it's original region. You need to add mouse move event handling on the parent control (the panel in your case).

    Hope this help.

    John 

    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
  • Thursday, November 05, 2009 3:54 PMSergiu Dudnic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sergiu, I undestand your question, the problem is you are only handle/listen the mouse move enent on the OvalShape, that explain why you can't move out of it's original region. You need to add mouse move event handling on the parent control (the panel in your case).

    Hope this help.

    John 

    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.

    When you have, by ex, a button control, the MouseMove event continues even when you pressed the cutton and leave the button area.

    even e.Location in this case can be negative or over the button's width(height)...

    "You need to add mouse move event handling on the parent control "

    This is the biggest problem. The parent control DOES NOT receive ANY MouseMove event , when the leftMouseButton is pressed in the child control and leaves its area .

    Best regards, Sergiu
  • Friday, November 06, 2009 11:56 AMSergiu Dudnic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    related bug on MS Connect
    Best regards, Sergiu