Re: how to manipulate the darg and drop of the webbrowser editor!!
Posted by: George Soules (---.greenmountainaccess.net)
Date: Sunday, 30-Oct-2005, 15:15:47
Attached is my version of drag and drop support. It is a simplified version of the code originally posted by Tomcat on 11-30-04 and 12-03-04. I can't guarantee that it's correct, but it seems to work for my application.
I am using Tim's original files with some minor modifications. The changes I made for drag and drop are detailed below. You need to edit HtmlEditor.cs, comsupport.cs, and HtmlSite.cs. Most of the code is new (marked "New"), but some existing code was modified (marked "Modified).
Note that my app subclasses HtmlEditor. The subclass overrides DropAllowed and OnDrop. My application-specified logic examines dataObject and takes appropriate action in these methods. This is similar to Tomcat's DataObjectConverter logic. I found, however, that the COM code could be much simpler if I let the subclass decide if draging/dropping was acceptable.
I hope this helps those of you that are looking for a simple example. I think this is about as simple as it can be and still work. Thank you to everyone who contributed to this thread and made it possible for me to add drag and drop to my app.
George
---- HtmlEditor.cs -----------------
// New
public virtual bool DropAllowed(DataObject dataObject, Point screenPoint)
{
return false;
}
public virtual void OnDrop(DataObject dataObject, Point screenPoint)
{
}
---- comsupport.cs --------------
// Modified
int GetDropTarget([In, MarshalAs(UnmanagedType.Interface)] IOleDropTarget pDropTarget,
[Out, MarshalAs(UnmanagedType.Interface)] out IOleDropTarget ppDropTarget);
// New
[System.Runtime.InteropServices.ComImport()]
[System.Runtime.InteropServices.InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[System.Runtime.InteropServices.GuidAttribute("00000122-0000-0000-C000-000000000046")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public interface IOleDropTarget
{
[System.Runtime.InteropServices.PreserveSig()] int OleDragEnter (IntPtr pDataObj, [System.Runtime.InteropServices.MarshalAs(UnmanagedType.U4)] int grfKeyState, [System.Runtime.InteropServices.MarshalAs(UnmanagedType.Struct)] tagPOINT pt, ref int pdwEffect);
[System.Runtime.InteropServices.PreserveSig()] int OleDragOver (int grfKeyState, [System.Runtime.InteropServices.MarshalAs(UnmanagedType.Struct)] tagPOINT pt, ref int pdwEffect);
[System.Runtime.InteropServices.PreserveSig()] int OleDragLeave ();
[System.Runtime.InteropServices.PreserveSig()] int OleDrop (IntPtr pDataObj, [System.Runtime.InteropServices.MarshalAs(UnmanagedType.U4)] int grfKeyState, [System.Runtime.InteropServices.MarshalAs(UnmanagedType.Struct)] tagPOINT pt, ref int pdwEffect);
}
---- HtmlSite.cs ---------------
// Modified
public int GetDropTarget(IOleDropTarget pDropTarget, out IOleDropTarget ppDropTarget)
{
ppDropTarget = new DropTarget((HtmlEditor)container, pDropTarget);
return HRESULT.S_OK;
}
// New
private sealed class DropTarget : IOleDropTarget
{
const int DROPEFFECT_NONE = 0;
const int DROPEFFECT_COPY = 1;
const string guidForIDataObject = "0000010E-0000-0000-C000-000000000046";
private DataObject dataObject;
private IntPtr dataObjectPtr;
private HtmlEditor htmlEditor;
public DropTarget (HtmlEditor htmlEditor, IOleDropTarget originalDropTarget)
{
this.htmlEditor = htmlEditor;
}
public int OleDragEnter(IntPtr pDataObj, int grfKeyState, tagPOINT pt, ref int pdwEffect)
{
dataObject = new DataObject(Marshal.GetObjectForIUnknown(pDataObj));
IntPtr intPtr = Marshal.GetIUnknownForObject(dataObject);
Guid guid = new Guid(guidForIDataObject);
Marshal.QueryInterface (intPtr, ref guid, out dataObjectPtr);
Marshal.Release(intPtr);
pdwEffect = DROPEFFECT_NONE;
return HRESULT.S_OK;
}
public int OleDragOver(int grfKeyState, tagPOINT pt, ref int pdwEffect)
{
bool dropAllowed = htmlEditor.DropAllowed(dataObject, new Point(pt.x, pt.y));
pdwEffect = dropAllowed ? DROPEFFECT_COPY : DROPEFFECT_NONE;
return HRESULT.S_OK;
}
public int OleDrop(IntPtr pDataObj, int grfKeyState, tagPOINT pt, ref int pdwEffect)
{
DataObject draggedDataObject = new DataObject(Marshal.GetObjectForIUnknown(pDataObj));
htmlEditor.OnDrop(draggedDataObject, new Point(pt.x, pt.y));
IntPtr pUnk = Marshal.GetIUnknownForObject(dataObject);
Guid guid = new Guid(guidForIDataObject);
Marshal.QueryInterface(pUnk, ref guid, out dataObjectPtr);
Marshal.Release(dataObjectPtr);
dataObject = null;
dataObjectPtr = IntPtr.Zero;
return HRESULT.S_OK;
}
public int OleDragLeave()
{
if (dataObject != null)
{
dataObject = null;
Marshal.Release(dataObjectPtr);
dataObjectPtr = IntPtr.Zero;
}
return HRESULT.S_OK;
}