please look at these 2 Private Sub mnuCopyClick(sender As Object, e As EventArgs)…
- User
please look at these 2
Private Sub mnuCopy_Click(sender As Object, e As EventArgs) Handles mnuCopyAll.Click, mnuCopyBaddies.Click, mnuCopyBlocks.Click, mnuCopyBlockStoppers.Click, mnuCopyCoins.Click, mnuCopyEdge.Click, mnuCopyKeys.Click, mnuCopyWalls.Click, mnuCopyWarps.Click If Not isRestoringState Then SaveStateForUndo() If selectionRectangle.IsEmpty Then Return End If
CaptureSelectionData() copiedSelectionData = CloneSelectionData(selectionData) isCopying = True ' Extract the selection type from the sender's name Dim menuItem = CType(sender, ToolStripMenuItem) copiedSelectionType = menuItem.Name.Substring(7) ' Remove the "mnuCopy" prefix ' Get cursor position in picGame coordinates Dim cursorPosInPicGame = picGame.PointToClient(Cursor.Position) ' Adjust selection rectangle to center on the cursor selectionRectangle.Location = New Point( cursorPosInPicGame.X - selectionRectangle.Width \ 2, cursorPosInPicGame.Y - selectionRectangle.Height \ 2)
' Recalculate selectionOffset based on new rectangle position selectionOffset = New Point( Cursor.Position.X - picGame.PointToScreen(selectionRectangle.Location).X, Cursor.Position.Y - picGame.PointToScreen(selectionRectangle.Location).Y)
' Automatically start dragging the selection isSelectionReadyToDrag = True isDraggingSelection = True picGame.Capture = True Cursor = Cursors.SizeAll picGame.Invalidate()End Sub Private Sub mnuMove_Click(sender As Object, e As EventArgs) Handles mnuMoveAll.Click, mnuMoveBaddies.Click, mnuMoveBlocks.Click, mnuMoveBlockStoppers.Click, mnuMoveCoins.Click, mnuMoveEdge.Click, mnuMoveKeys.Click, mnuMoveWalls.Click, mnuMoveWarps.Click If Not isRestoringState Then SaveStateForUndo() If selectionRectangle.IsEmpty Then Return End If
CaptureSelectionData() isCopying = False ' Extract the selection type from the sender's name Dim menuItem = CType(sender, ToolStripMenuItem) copiedSelectionType = menuItem.Name.Substring(6) ' Remove the "mnuMove" prefix ' Get cursor position in picGame coordinates Dim cursorPosInPicGame = picGame.PointToClient(Cursor.Position) ' Recalculate selectionOffset based on current cursor position selectionOffset = New Point( Cursor.Position.X - picGame.PointToScreen(selectionRectangle.Location).X, Cursor.Position.Y - picGame.PointToScreen(selectionRectangle.Location).Y)
' Automatically start dragging the selection isSelectionReadyToDrag = True isDraggingSelection = True picGame.Capture = True Cursor = Cursors.SizeAll picGame.Invalidate()End Sub
I want mnuMove to snap to the cursor like mnuCopy. The code attempts it but fails. Can you see why? (If you need more code please ask)
- Assistant
Yes, the issue is that in your
mnuMove_Clickmethod, you're not adjusting theselectionRectangleto center on the cursor position as you do in themnuCopy_Clickmethod. This means that when you start moving the selection, it doesn't snap to the cursor because the selection rectangle remains at its original location.In your
mnuCopy_Clickmethod, you have the following code that adjusts theselectionRectangle:' Adjust selection rectangle to center on the cursor selectionRectangle.Location = New Point( cursorPosInPicGame.X - selectionRectangle.Width \ 2, cursorPosInPicGame.Y - selectionRectangle.Height \ 2 )This code repositions the
selectionRectangleso that it is centered around the cursor position withinpicGame. This ensures that when you start dragging, the selection is directly under the cursor.However, in your
mnuMove_Clickmethod, this adjustment is missing. To make the selection snap to the cursor when you move it, you need to include the same code inmnuMove_Click. Here's how you can modify yourmnuMove_Clickmethod:Private Sub mnuMove_Click(sender As Object, e As EventArgs) Handles mnuMoveAll.Click, mnuMoveBaddies.Click, mnuMoveBlocks.Click, mnuMoveBlockStoppers.Click, mnuMoveCoins.Click, mnuMoveEdge.Click, mnuMoveKeys.Click, mnuMoveWalls.Click, mnuMoveWarps.Click If Not isRestoringState Then SaveStateForUndo() If selectionRectangle.IsEmpty Then Return End If CaptureSelectionData() isCopying = False ' Extract the selection type from the sender's name Dim menuItem = CType(sender, ToolStripMenuItem) copiedSelectionType = menuItem.Name.Substring(6) ' Remove the "mnuMove" prefix ' Get cursor position in picGame coordinates Dim cursorPosInPicGame = picGame.PointToClient(Cursor.Position) ' Adjust selection rectangle to center on the cursor selectionRectangle.Location = New Point( cursorPosInPicGame.X - selectionRectangle.Width \ 2, cursorPosInPicGame.Y - selectionRectangle.Height \ 2 ) ' Recalculate selectionOffset based on new rectangle position selectionOffset = New Point( Cursor.Position.X - picGame.PointToScreen(selectionRectangle.Location).X, Cursor.Position.Y - picGame.PointToScreen(selectionRectangle.Location).Y ) ' Automatically start dragging the selection isSelectionReadyToDrag = True isDraggingSelection = True picGame.Capture = True Cursor = Cursors.SizeAll picGame.Invalidate() End SubBy adding the code to adjust the
selectionRectanglelocation, you ensure that the selection snaps to the cursor when you initiate the move action, just like it does in the copy action.Summary: The
mnuMove_Clickmethod doesn't adjustselectionRectangle.Locationto center on the cursor likemnuCopy_Clickdoes. Adding this adjustment tomnuMove_Clickwill make the selection snap to the cursor when moving.Because the code in
mnuMove_Clickdoesn’t adjustselectionRectangle.Locationto center on the cursor like inmnuCopy_Click, so the selection doesn’t snap to the cursor—you need to add that adjustment inmnuMove_Click.