Calling InitializeFromBitmap fails with exception "The method or operation is not implemented"
-
Hello, I have the following code in one of my projects: DragDropHelper Helper = new DragDropHelper(); // See below! IDragSourceHelper Drag = (IDragSourceHelper)Helper; SHDRAGIMAGE tt = new SHDRAGIMAGE(); Bitmap img = new Bitmap(@"C:\Temp\DragImage.bmp"); tt.ptOffset.x = 0; tt.ptOffset.y = 0; tt.sizeDragImage.x = img.Width; tt.sizeDragImage.y = img.Height; tt.hbmpDragImage = img.GetHbitmap(); tt.crColorKey = 0; Drag.InitializeFromBitmap(ref tt, dragObject); Calling InitializeFromBitmap fails with the above mentioned message. Can any one tell me why? In addition, this code is in my project as well: [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] public struct SHDRAGIMAGE { public POINT sizeDragImage; public POINT ptOffset; public IntPtr hbmpDragImage; public uint crColorKey; } [ComImport, Guid("DE5BF786-477A-11d2-839D-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDragSourceHelper { void InitializeFromBitmap( ref SHDRAGIMAGE pshdi, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject); void InitializeFromWindow( IntPtr hwnd, ref POINT point, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject); } [ComImport, Guid("4657278B-411B-11d2-839A-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDropTargetHelper { void DragEnter(IntPtr hwnd, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref POINT point, DragDropEffects dwEffect); void DragLeave(); void DragOver(ref POINT point, DragDropEffects dwEffect); void Drop([MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref POINT point, DragDropEffects dwEffect); void Show([MarshalAs(UnmanagedType.Bool)] bool fShow); } [ComImport, Guid("4657278A-411B-11d2-839A-00C04FD918D0")] class DragDropHelper { }
-
Hello, I have the following code in one of my projects: DragDropHelper Helper = new DragDropHelper(); // See below! IDragSourceHelper Drag = (IDragSourceHelper)Helper; SHDRAGIMAGE tt = new SHDRAGIMAGE(); Bitmap img = new Bitmap(@"C:\Temp\DragImage.bmp"); tt.ptOffset.x = 0; tt.ptOffset.y = 0; tt.sizeDragImage.x = img.Width; tt.sizeDragImage.y = img.Height; tt.hbmpDragImage = img.GetHbitmap(); tt.crColorKey = 0; Drag.InitializeFromBitmap(ref tt, dragObject); Calling InitializeFromBitmap fails with the above mentioned message. Can any one tell me why? In addition, this code is in my project as well: [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] public struct SHDRAGIMAGE { public POINT sizeDragImage; public POINT ptOffset; public IntPtr hbmpDragImage; public uint crColorKey; } [ComImport, Guid("DE5BF786-477A-11d2-839D-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDragSourceHelper { void InitializeFromBitmap( ref SHDRAGIMAGE pshdi, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject); void InitializeFromWindow( IntPtr hwnd, ref POINT point, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject); } [ComImport, Guid("4657278B-411B-11d2-839A-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IDropTargetHelper { void DragEnter(IntPtr hwnd, [MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref POINT point, DragDropEffects dwEffect); void DragLeave(); void DragOver(ref POINT point, DragDropEffects dwEffect); void Drop([MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref POINT point, DragDropEffects dwEffect); void Show([MarshalAs(UnmanagedType.Bool)] bool fShow); } [ComImport, Guid("4657278A-411B-11d2-839A-00C04FD918D0")] class DragDropHelper { }
If I've read your code and problem right, the problem became clear. The method or operation is not implemented. Now to be serious, MSDN reports that the minimal dll version is shell32.dll v5.0. Do you meet this requirement? Does marshaling IDragSourceHelper require you to define the GetData() method? Not sure myself.
Just because we can; does not mean we should.
-
If I've read your code and problem right, the problem became clear. The method or operation is not implemented. Now to be serious, MSDN reports that the minimal dll version is shell32.dll v5.0. Do you meet this requirement? Does marshaling IDragSourceHelper require you to define the GetData() method? Not sure myself.
Just because we can; does not mean we should.
Thanks for your reply. Meanwhile I've come a lot further. The problem was that methods SetData and GetData where not implemented. Therefore I created a new DataObject class which re-implements these methods. This is the code I have so far which is working but needs some adjustments here and there. Also I'm not sure whether all allocated objects are released correctly. Regards, Kees public partial class CustomControl1 : Control { DragDropHelper Helper; public CustomControl1() { InitializeComponent(); this.AllowDrop = true; } protected override void OnPaint(PaintEventArgs pe) { Brush B = new SolidBrush(Color.White); pe.Graphics.FillRectangle(B, pe.ClipRectangle); } protected override void OnMouseDown(MouseEventArgs e) { DataFormats.Format ganttbarFormat = DataFormats.GetFormat("Ganttbar"); object ganttbar = new object(); DataObject dragObject = new DataObjectEx(ganttbarFormat.Name, ganttbar); DoDragDrop(dragObject, DragDropEffects.All); } private void UpdateDragImage(System.Runtime.InteropServices.ComTypes.IDataObject dragObject) { if (Helper == null) { Helper = new DragDropHelper(); } IDragSourceHelper Drag = (IDragSourceHelper)Helper; SHDRAGIMAGE tt = new SHDRAGIMAGE(); Bitmap img = new Bitmap(@"C:\Temp\DragImage.bmp"); tt.ptOffset.x = 0; tt.ptOffset.y = 0; tt.sizeDragImage.x = img.Width; tt.sizeDragImage.y = img.Height; tt.hbmpDragImage = img.GetHbitmap(); tt.crColorKey = 0; Drag.InitializeFromBitmap(ref tt, dragObject); } protected override void OnDragOver(DragEventArgs drgevent) { IDropTargetHelper H = (IDropTargetHelper)Helper; POINT p = new POINT(); p.x = drgevent.X; p.y = drgevent.Y; H.DragOver(ref p, DragDropEffects.Copy); drgevent.Effect = DragDropEffects.Copy; } protected override void OnDragDrop(DragEventArgs drgevent) { System.Runtime.InteropServices.ComTypes.IDataObject dragObject = (System.Runtime.InteropServices.ComTypes.IDataObject)drgevent.Data; IDropTargetHelper H = (IDropTargetHelper)Helper; POINT p = new POINT(); p.x = drgevent.X; p.y = drgevent.Y; H.Drop(dragObject, ref p, DragDropEffects.Copy); base.OnDragDrop(drgevent); } protected override void OnDragEnter(DragEventArgs drgevent)