Implementing Drag-Drop Insertion of OLE Objects

ID: Q86270

The information in this article applies to:

SUMMARY

An OLE client application can support the drag-drop feature provided by the SHELL.DLL component of Microsoft Windows version 3.1. The drag-drop feature supports dragging a file from the Windows File Manager and dropping it on any registered drag-drop recipient. If the user drops a file onto an OLE client application and the application manages that file type, the application opens the file for editing. If the OLE client application does not manage that file type, it can create an embedded object for the dropped file.

MORE INFORMATION

When an OLE client creates an embedded object from a dropped file, it inserts a packaged object that refers to the dropped file. The OLE server for a packaged object (an object of the Package class) is the Microsoft Windows Object Packager (PACKAGER.EXE) application. Packager supports associating a command line, a file, or an object with an icon.

A packaged object can contain either embedded or linked objects. When the user double-clicks a packaged object, Windows invokes the application associated with the packaged object and specifies the object on the application's command line. For example, when the user double-clicks a package object labeled BOOTLOG.TXT, Packager calls the WinExec function to start Notepad and specifies BOOTLOG.TXT as the command-line argument.

If an application can accept dropped files, it must call the DragAcceptFiles function. Once an application registers itself, the user can drag an object from the File Manager onto the application. When the user drops a file, the application receives a WM_DROPFILES message. When the client calls the DragQueryFile function, Windows provides the number and names of the dropped files. The DragQueryPoint function provides the location in the application's window where the user dropped the files.

The recommended procedure to provide drag-drop support in an OLE client application is as follows:

The Code to insert a drag-drop object in an application might resemble the following:

char       szFile[80];      // Used to store name of dropped file
OLESTATUS  OleStatus;       // Code returned from OLE functions
WORD       wNumFiles, cb;

case WM_DROPFILES:
   // Retrieve number of dropped files
   wNumFiles = DragQueryFile((HANDLE)wParam, 0xFFFF, NULL, 0);

   // Simple case of one dropped file; more than one can be dropped
   if (wNumFiles == 1)
      {
      cb = DragQueryFile((HANDLE)wParam, 0, szFile, 80);

      // If no characters copied, there were no files
      if (cb == 0)
         return(0L);

      // Call OleCreateFromFile to insert a Package object containing
      // the dropped file as an embedded object or call
      // OleCreateLinkFromFile to insert a Package object containing a
      // link to the dropped file.
      OleStatus = OleCreateFromFile("StdFileEditing", lpOleClient,
         "Package", (LPSTR)szFile, lhDoc, lpszUniqueName, lplpObject,
         olerender_draw, 0);

      // Follow the procedure to create an object from the clipboard

      DragFinish((HANDLE)wParam);
      }

Additional reference words: 1.00 KBCategory: kbole kbprg KBSubcategory: LeOneCli

Last Reviewed: February 17, 1995