<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'><div style="text-align: left;">Thats excellent Tim, I was just about to start digging around for information on how to get the data once it's dropped.<br><br>Thanks for your help<br>Alex<br><br></div><hr id="stopSpelling">> Date: Fri, 25 Apr 2008 17:16:34 +0100<br>> From: mail@timgolden.me.uk<br>> CC: python-win32@python.org<br>> Subject: Re: [python-win32] Creating python com objects<br>> <br>> Well, for those of you still watching this show (!) and<br>> just in case anyone comes along in the future with the<br>> same question, I attach below a small working example<br>> which will accept one or more files dropped onto its<br>> Window. It doesn't do anything with the files, bar pull<br>> their names from the relevant data structure, but that's<br>> up to the specific application.<br>> <br>> Many thanks to Roger for the critical info to get this<br>> working and to Alex for asking the question which started<br>> it off.<br>> <br>> TJG<br>> <br>> <code><br>> import os, sys<br>> import win32gui<br>> import win32con<br>> import pythoncom<br>> import win32com.server.policy<br>> from win32com.shell import shell, shellcon<br>> <br>> # clsid generated by pythoncom.CreateGuid ()<br>> CLSID = '{89DD545A-2C83-4103-AFE3-6CEB7FF5ECA4}'<br>> PROGID = "Tim.DropTarget"<br>> DESC = "Drop target handler for Tim"<br>> <br>> class DropTarget (win32com.server.policy.DesignatedWrapPolicy):<br>> _reg_clsid_ = CLSID<br>> _reg_progid_ = PROGID<br>> _reg_desc_ = DESC<br>> _public_methods_ = ['DragEnter', 'DragOver', 'DragLeave', 'Drop']<br>> _com_interfaces_ = [pythoncom.IID_IDropTarget]<br>> <br>> def __init__ (self, hWnd):<br>> self._wrap_ (self)<br>> self.hWnd = hWnd<br>> <br>> def DragEnter (self, data_object, key_state, point, effect):<br>> if data_object.QueryGetData ((15, None, 1, -1, 1)):<br>> return shellcon.DROPEFFECT_COPY<br>> else:<br>> return shellcon.DROPEFFECT_NONE<br>> def DragOver (self, key_state, point, effect):<br>> pass<br>> def DragLeave (self):<br>> pass<br>> def Drop (self, data_object, key_state, point, effect):<br>> data = data_object.GetData ((15, None, 1, -1, 1))<br>> n_files = shell.DragQueryFileW (data.data_handle, -1)<br>> filenames = [<br>> shell.DragQueryFileW (data.data_handle, n_file) \<br>> for n_file in range (n_files)<br>> ]<br>> <br>> def create_window ():<br>> def OnDestroy (hwnd, msg, wparam, lparam):<br>> win32gui.PostQuitMessage (0)<br>> <br>> wc = win32gui.WNDCLASS ()<br>> hinst = win32gui.GetModuleHandle(None)<br>> wc.lpszClassName = "DragDrop"<br>> wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;<br>> wc.hCursor = win32gui.LoadCursor (0, win32con.IDC_ARROW)<br>> wc.hbrBackground = win32con.COLOR_WINDOW<br>> wc.lpfnWndProc = {win32con.WM_DESTROY : OnDestroy}<br>> classAtom = win32gui.RegisterClass (wc)<br>> style = win32con.WS_VISIBLE | win32con.WS_OVERLAPPED \<br>> | win32con.WS_SYSMENU<br>> return win32gui.CreateWindow (<br>> classAtom,<br>> "Drag & Drop demo",<br>> style,<br>> 0, 0, 100, 100,<br>> 0, 0, hinst, None<br>> )<br>> <br>> if __name__ == '__main__':<br>> pythoncom.OleInitialize ()<br>> hWnd = create_window ()<br>> drop_target = DropTarget (hWnd)<br>> pythoncom.RegisterDragDrop (<br>> hWnd,<br>> pythoncom.WrapObject (<br>> drop_target,<br>> pythoncom.IID_IDropTarget,<br>> pythoncom.IID_IDropTarget<br>> )<br>> )<br>> win32gui.PumpMessages ()<br>> <br>> </code><br>> _______________________________________________<br>> python-win32 mailing list<br>> python-win32@python.org<br>> http://mail.python.org/mailman/listinfo/python-win32<br><br /><hr />Get fish-slapping on Messenger <a href='http://mobile.uk.msn.com/pc/messenger.aspx ' target='_new'>Play Now</a></body>
</html>