Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2004

Tip: Looking for answers? Try searching our database.

Problem using RegisterDragDrop to register a IDropTarget

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SunStar - 19 Jul 2004 18:23 GMT
Now I want to use RegisterDragDrop method to register a IDropTarget to a
panel on my form.Below is my code, when I drag some url link to the panel ,
I found that the IDropTarget's dragenter method was invoked by drag, but the
DragDropEffect always None so the drop method was never invoked. What 's the
problem?

 private MyDropTarget target;

 private void button1_Click(object sender, System.EventArgs e)
 {
  if (this.panel1.IsHandleCreated)
  {
   if (Application.OleRequired() != ApartmentState.STA)
   {
    throw new ThreadStateException("ThreadMustBeSTA");
   }
   new UIPermission(UIPermissionClipboard.AllClipboard).Demand();

   target=new MyDropTarget();
   int result=ComUtils.RegisterDragDrop(new HandleRef(this.panel1,
this.panel1.Handle), (ComUtils.IDropTarget)target);
   if ((result == 0) || (result == -2147221247))
   {
    MessageBox.Show("OK");
   }

  }
 }
}

public class MyDropTarget:ComUtils.IDropTarget
{
 public MyDropTarget()
 {
 }
 #region IDropTarget
 public int DragEnter(object pDataObj, int grfKeyState, long pt, out int
pdwEffect)
 {
  pdwEffect=1;
  return 0;
 }

 public int DragLeave()
 {
  return 0;
 }

 public int DragOver(int grfKeyState, long pt, out int pdwEffect)
 {
  pdwEffect = 1;
  return 0;
 }

 public int Drop(object pDataObj, int grfKeyState, long pt, out int
pdwEffect)
 {
  pdwEffect = 1;
  return 0;
 }

 #endregion

}

Any help will be appreciated

Regards
Mattias Sj?gren - 19 Jul 2004 20:48 GMT
How did you declare the IDropTarget interface?

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

SunStar - 20 Jul 2004 01:11 GMT
hi, Mattias,Here is my interface declaration.

public enum CLIPFORMAT : uint
 {
  CF_TEXT =  1,
  CF_BITMAP =  2,
  CF_METAFILEPICT= 3,
  CF_SYLK =  4,
  CF_DIF =  5,
  CF_TIFF =  6,
  CF_OEMTEXT = 7,
  CF_DIB =  8,
  CF_PALETTE = 9,
  CF_PENDATA = 10,
  CF_RIFF =  11,
  CF_WAVE =  12,
  CF_UNICODETEXT= 13,
  CF_ENHMETAFILE= 14,
  CF_HDROP =  15,
  CF_LOCALE =  16,
  CF_MAX =  17,

  CF_OWNERDISPLAY=0x0080,
  CF_DSPTEXT = 0x0081,
  CF_DSPBITMAP = 0x0082,
  CF_DSPMETAFILEPICT= 0x0083,
  CF_DSPENHMETAFILE = 0x008E,

  CF_PRIVATEFIRST=0x0200,
  CF_PRIVATELAST= 0x02FF,

  CF_GDIOBJFIRST =0x0300,
  CF_GDIOBJLAST = 0x03FF
 }

 public enum DVASPECT: uint
 {
  DVASPECT_CONTENT = 1,
  DVASPECT_THUMBNAIL = 2,
  DVASPECT_ICON = 4,
  DVASPECT_DOCPRINT = 8
 }

 public enum TYMED: uint
 {
  TYMED_HGLOBAL = 1,
  TYMED_FILE = 2,
  TYMED_ISTREAM = 4,
  TYMED_ISTORAGE= 8,
  TYMED_GDI =  16,
  TYMED_MFPICT = 32,
  TYMED_ENHMF = 64,
  TYMED_NULL=  0
 }

 [StructLayout(LayoutKind.Sequential)]
  public struct FORMATETC
 {
  public uint cfFormat;
  public uint   ptd;
  public DVASPECT  dwAspect;
  public int   lindex;
  public TYMED  tymed;
 }

 [StructLayout(LayoutKind.Sequential)]
  public struct STGMEDIUM
 {
  public uint tymed;
  public uint hGlobal;
  public uint pUnkForRelease;
 }

 [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
GuidAttribute("0000010e-0000-0000-C000-000000000046")]
  public interface IDataObject
 {
  [PreserveSig()]
  int GetData(ref FORMATETC a, ref STGMEDIUM b);
  [PreserveSig()]
  void GetDataHere(int a, ref STGMEDIUM b);
  [PreserveSig()]
  int QueryGetData(ref FORMATETC f);
  [PreserveSig()]
  int GetCanonicalFormatEtc(int a, ref int b);
  [PreserveSig()]
  int SetData(int a, int b, int c);
  [PreserveSig()]
  int EnumFormatEtc(uint a, ref Object b);
  [PreserveSig()]
  int DAdvise(int a, uint b, Object c, ref uint d);
  [PreserveSig()]
  int DUnadvise(uint a);
  [PreserveSig()]
  int EnumDAdvise(ref Object a);
 }

 [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
GuidAttribute("00000122-0000-0000-C000-000000000046")]
  public interface IDropTarget
 {
  [PreserveSig()]
  int DragEnter(object pDataObj, int grfKeyState, long pt, out int
pdwEffect);
  [PreserveSig()]
  int DragLeave();
  [PreserveSig()]
  int DragOver(int grfKeyState, long pt, out int pdwEffect);
  [PreserveSig()]
  int Drop(object pDataObj, int grfKeyState, long pt, out int pdwEffect);
 }

 [DllImport("ole32.dll")]
 public static extern int OleLoadFromStream(UCOMIStream pStm,ref Guid
iidInterface,[MarshalAs(UnmanagedType.Interface)] out object ppvObj);
 [DllImport("OLE32.DLL", EntryPoint="CreateStreamOnHGlobal")]
 public static extern int CreateStreamOnHGlobal(int hGlobalMemHandle, bool
fDeleteOnRelease, out UCOMIStream pOutStm);
 [DllImport("ole32.dll")]
 public static extern int RegisterDragDrop(HandleRef hwnd, IDropTarget
target);
 //public static extern int RegisterDragDrop(IntPtr hwnd, IDropTarget
target);
 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true,
EntryPoint="RegisterClipboardFormatA")]
 public static extern int RegisterClipboardFormat( string lpszFormat  );

..

> How did you declare the IDropTarget interface?
>
> Mattias
Mattias Sj?gren - 20 Jul 2004 09:44 GMT
>   [PreserveSig()]
>   int DragEnter(object pDataObj, int grfKeyState, long pt, out int
>pdwEffect);

You should change the first parameter's type to IDataObject.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

SunStar - 20 Jul 2004 16:33 GMT
hi , Mattias

    I tried to change the object to IDataObject, but the problem is still.

Regards

> >   [PreserveSig()]
> >   int DragEnter(object pDataObj, int grfKeyState, long pt, out int
[quoted text clipped - 3 lines]
>
> Mattias
Mattias Sj?gren - 23 Jul 2004 23:07 GMT
>     I tried to change the object to IDataObject, but the problem is still.

Well I don't see anything else wrong with the DragEnter method, but
you have the same problem in Drop. Also, DragOver and DragLeave are in
the wrong order - DragOver should be declared first.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

SunStar - 24 Jul 2004 06:11 GMT
After I changed the order of declarations of dragover, dragleave , it works
now

Mattias, Thank you very much , You are the real guru :O)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.