Sei sulla pagina 1di 4

Option Explicit

Private Const SM_SCREENX = 1


Private Const SM_SCREENY = 0
Private Const msgCancel = "."
Private Const msgNoXlInstance = "."
Private Const sigProc = "Drag & Drop"
Private Const VK_SHIFT = &H10
Private Const VK_CTRL = &H11
Private Const VK_ALT = &H12

Public Type PointAPI


X As Long
Y As Long
End Type

Public Type RECT


lLeft As Long
lTop As Long
lRight As Long
lBottom As Long
End Type

Public Type SquareEnd


X As Long
Y As Long
End Type

#If VBA7 Then


Public Declare PtrSafe Function GetKeyState Lib "user32" (ByVal nVirtKey As LongPtr) As
Integer
Public Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As LongPtr,
ByVal yPoint As LongPtr) As LongPtr
Public Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr,
lpRect As RECT) As LongPtr
Public Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As
LongPtr
Public Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As LongPtr)
As LongPtr
#Else
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal
yPoint As Long) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As
RECT) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#End If

Public mPoint As PointAPI


Private ActiveShape As Shape
Private dragMode As Boolean
Private dx As Double, dy As Double
Private sqrBlack As SquareEnd
Sub DragAndDrop(selectedShape As Shape)

dragMode = Not dragMode


DoEvents
' If the shape has text and we're starting to drag, copy it with its formatting to the clipboard
If selectedShape.HasTextFrame And dragMode Then
selectedShape.TextFrame.TextRange.Copy

dx = GetSystemMetrics(SM_SCREENX)
dy = GetSystemMetrics(SM_SCREENY)

sqrBlack.X = ActivePresentation.Slides(1).Shapes("square_end").left
sqrBlack.Y = ActivePresentation.Slides(1).Shapes("square_end").top

Drag selectedShape

' Paste the original text while maintaining its formatting, back to the shape
If selectedShape.HasTextFrame Then selectedShape.TextFrame.TextRange.Paste
DoEvents
End Sub

Private Sub Drag(selectedShape As Shape)


#If VBA7 Then
Dim mWnd As LongPtr
#Else
Dim mWnd As Long
#End If
Dim sx As Long, sy As Long
Dim WR As RECT ' Slide Show Window rectangle
Dim StartTime As Single
' Change this value to change the timer to automatically drop the shape (can by integer or
decimal)
Const DropInSeconds = ADD_TIME_TO_DROP_HERE

' Get the system cursor coordinates


GetCursorPos mPoint
' Find a handle to the window that the cursor is over
mWnd = WindowFromPoint(mPoint.X, mPoint.Y)
' Get the dimensions of the window
GetWindowRect mWnd, WR
sx = WR.lLeft
sy = WR.lTop
Debug.Print sx, sy

With ActivePresentation.PageSetup
dx = (WR.lRight - WR.lLeft) / .SlideWidth
dy = (WR.lBottom - WR.lTop) / .SlideHeight
Select Case True
Case dx > dy
sx = sx + (dx - dy) * .SlideWidth / 2
dx = dy
Case dy > dx
sy = sy + (dy - dx) * .SlideHeight / 2
dy = dx
End Select
End With

StartTime = Timer

While dragMode
GetCursorPos mPoint
selectedShape.left = (mPoint.X - sx) / dx - selectedShape.Width / 2
selectedShape.top = (mPoint.Y - sy) / dy - selectedShape.Height / 2

Dim left As Integer


Dim top As Integer
left = selectedShape.left
top = selectedShape.top

ActivePresentation.Slides(1).Shapes(ADD_SHAPE_TO_DISPLAY_POSITION_HER
E).TextFrame.TextRange = "X: " + CStr(left) + " Y:" + CStr(top)

With sqrBlack

ActivePresentation.Slides(1).Shapes(ADD_SHAPE_TO_DISPLAY_END_POSITION_H
ERE).TextFrame.TextRange = "X:" + CStr(.X) + " Y:" + CStr(.Y)

End With

' Comment out the next line if you do NOT want to show the countdown text within the
shape
If selectedShape.HasTextFrame Then selectedShape.TextFrame.TextRange.Text =
CInt(DropInSeconds - (Timer - StartTime))

ActivePresentation.Slides(1).Shapes(ADD_SHAPE_NAME_FOR_COUNTDOWN_HE
RE).TextFrame.TextRange.Text = CInt(DropInSeconds - (Timer - StartTime))

DoEvents
If Timer > StartTime + DropInSeconds Then
dragMode = False

With ActivePresentation.Slides(1).Shapes(ADD_GOAL_SHAPE_NAME_HERE) '


EXAMPLE:square_end is where you want the square to land
If selectedShape.left >= .left And selectedShape.top >= .top And (selectedShape.left +
selectedShape.Width) <= (.left + .Width) And (selectedShape.top + selectedShape.Height)
<= (.top + .Height) Then
MsgBox ADD_MESSAGE_HERE
End If
End With
End If

Wend
DoEvents
End Sub

Potrebbero piacerti anche