Windows drag & drop with win32com and IDropTarget

G

Greg K

I'm trying to create a program that will process files dragged into
its window, however I can't seem to get the cursor to change correctly
when something is dragged over the window. I've created an object that
implements the IDropTarget interface, but it seems the value returned
by its DragEnter method is ignored by Windows. No matter what value it
returns, I always get a "create shortcut" cursor when something is
dragged over the window. I'm thinking maybe the value returned by the
Python object isn't getting mapped to the "ByRef" parameter correctly,
but I can't figure out how to fix it. Can anyone see what I'm doing
wrong? This is the code:

---
import winerror
import win32gui
import win32con
import pythoncom
from win32com.shell import shellcon
from win32com.server.policy import DesignatedWrapPolicy

def main():
pythoncom.OleInitialize()
hWnd = CreateWindow()
pythoncom.RegisterDragDrop(
hWnd,
pythoncom.WrapObject(
DropTarget( hWnd ),
pythoncom.IID_IDropTarget,
pythoncom.IID_IDropTarget
)
)
win32gui.PumpMessages()

class DropTarget( DesignatedWrapPolicy ):
_reg_clsid_ = "{8BFD2A1E-5F4B-4B0A-9246-52C5FDBB3B50}"
_reg_progid_ = "Blah.Blah"
_reg_desc_ = "Test"
_public_methods_ = [ 'DragEnter', 'DragOver', 'DragLeave',
'Drop' ]
_com_interfaces_ = [ pythoncom.IID_IDropTarget ]

def __init__( self, hWnd ):
self._wrap_( self )
self.hWnd = hWnd

def DragEnter( self, dataObject, keyState, point, effect ):
return winerror.S_OK, shellcon.DROPEFFECT_COPY
# return shellcon.DROPEFFECT_COPY # also doesn't work
def DragOver( self, keyState, point, effect ):
pass
def DragLeave( self ):
pass
def Drop( self, dataObject, keyState, point, effect ):
pass

def CreateWindow():

def OnDestroy( hwnd, msg, wparam, lparam ):
win32gui.PostQuitMessage( 0 )

wc = win32gui.WNDCLASS()
hinst = win32gui.GetModuleHandle( None )
wc.lpszClassName = "BlahBlah"
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = {
win32con.WM_DESTROY : OnDestroy
}
classAtom = win32gui.RegisterClass( wc )

return win32gui.CreateWindow(
classAtom,
"Title",
win32con.WS_VISIBLE | win32con.WS_OVERLAPPED |
win32con.WS_SYSMENU,
200, 200, 200, 200,
0, 0, hinst, None
)

if __name__ == '__main__':
main()
---

The DragEnter method is documented on MSDN as:

HRESULT DragEnter(
[in] IDataObject *pDataObj,
[in] DWORD grfKeyState,
[in] POINTL pt,
[in, out] DWORD *pdwEffect
);

The out value of pdwEffect should determine the cursor appearance.

Thanks,
Greg.
 
T

Tim Golden

I'm trying to create a program that will process files dragged into
its window, however I can't seem to get the cursor to change correctly
when something is dragged over the window. I've created an object that
implements the IDropTarget interface, but it seems the value returned
by its DragEnter method is ignored by Windows. No matter what value it
returns, I always get a "create shortcut" cursor when something is
dragged over the window.

This is a segment of my code which works ok:

<... code snippet ...>
def DragEnter (self, data_object, key_state, point, effect):
ur"""Query the data block for a drag action which is over the dialog.
If we can handle it, indicate that we're ready to accept a drop from
this data.
"""
try:
data_object.QueryGetData (self._data_format)
except pywintypes.error:
return shellcon.DROPEFFECT_NONE
else:
return shellcon.DROPEFFECT_COPY
<... code snippet ...>

The whole code's here if you want:

https://winsys.googlecode.com/svn/trunk/winsys/dialogs.py

It looks as though you're returning an OK *and* the effect
which isn't how the thing is implemented via the pywin32
wrappers.

TJG
 
G

Greg K

It looks as though you're returning an OK *and* the effect
which isn't how the thing is implemented via the pywin32
wrappers.

Thanks for the reply, however even after I change the return value to
just the effect code, I still have the same problem. There must still
be something else I'm doing wrong... But I can probably figure it out
from looking at your working code (thanks for that).

Greg.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top