Visual Basic Development Bookmark and Share   
 Home > Visual Basic Power Packs > How to move a Shape?
 

How to move a Shape?

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
Sergiu Dudnic  Tuesday, October 27, 2009 3:49 PM
related bug on MS Connect
Best regards, Sergiu
Sergiu Dudnic  Friday, November 06, 2009 11:56 AM

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.
Xingwei Hu  Friday, October 30, 2009 8:41 AM
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
Sergiu Dudnic  Friday, October 30, 2009 11:10 AM
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.
John Chen MS  Thursday, November 05, 2009 3:41 AM
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
Sergiu Dudnic  Thursday, November 05, 2009 3:54 PM
related bug on MS Connect
Best regards, Sergiu
Sergiu Dudnic  Friday, November 06, 2009 11:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Printform PowerPack problem
• How to prevent encountering GAC error?
• Add new item in a datarepeater
• PrintForm Clips Form
• Adding a new item at the first row when a scrollbar is visible
• NESTED DATAREPEATER CONTROL
• Selecting multiple shapes at once (run-time user action)
• VB2008 Label Control Transparency
• Graphic line will not print
• query about the component to be added for media player