Using Drag-Drop in an Edit Control or a Combo Box

Last reviewed: November 2, 1995
Article ID: Q86724
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

In the Microsoft Windows environment, an application can register an edit control or a combo box as a drag-drop client through the DragAcceptFiles function. The application must also subclass the control to process the WM_DROPFILES message that Windows sends when the user drops a file.

MORE INFORMATION

The following seven steps demonstrate how to implement drag-drop in an edit control. The procedure to implement drag-drop in a combo box is identical.

  1. Add SHELL.LIB to the list of libraries required to build the file.

  2. Add the name of the subclass procedure (MyDragDropProc) to the EXPORTS section of the module definition (DEF) file.

  3. Include the SHELLAPI.H file in the application's source code.

  4. Declare the following procedure and variables:

          BOOL FAR PASCAL MyDragDropProc(HWND, unsigned, WORD, LONG);
    

          FARPROC lpfnDragDropProc, lpfnOldEditProc;
    
          char    szTemp64[64];
    
    

  5. Add the following code to the initialization of the dialog box:

          case WM_INITDIALOG:
    
             // ... other code
    
             // ------- edit control section --------
             hWndTemp = GetDlgItem(hDlg, IDD_EDITCONTROL);
             DragAcceptFiles(hWndTemp, TRUE);
    
             // subclass the drag-drop edit control
             lpfnDragDropProc = MakeProcInstance(MyDragDropProc, hInst);
    
             if (lpfnDragDropProc)
                lpfnOldEditProc = SetWindowLong(hWndTemp, GWL_WNDPROC,
                      (DWORD)(FARPROC)lpfnDragDropProc);
             break;
    
    

  6. Write a subclass window procedure for the edit control.

          BOOL FAR PASCAL MyDragDropProc(HWND hWnd, unsigned message,
    
                                         WORD wParam, LONG lParam)
          {
             int wFilesDropped;
    
             switch (message)
                {
             case WM_DROPFILES:
                // Retrieve number of files dropped
                // To retrieve all files, set iFile parameter
                // to -1 instead of 0
                wFilesDropped = DragQueryFile((HDROP)wParam, 0,
                      (LPSTR)szTemp64, 63);
    
                if (wFilesDropped)
                   {
                   // Parse the file path here, if desired
                   SendMessage(hWnd, WM_SETTEXT, 0, (LPSTR)szTemp64);
                   }
                else
                   MessageBeep(0);
    
                DragFinish((HDROP)wParam);
                break;
    
             default:
                return CallWindowProc(lpfnOldEditProc, hWnd, message,
                      wParam, lParam);
                break;
             }
             return TRUE;
          }
    
    

  7. After the completion of the dialog box procedure, free the edit control subclass procedure.

          if (lpfnDragDropProc)
    
             FreeProcInstance(lpfnDragDropProc);
    


Additional reference words: 3.10 3.50 3.51 4.00 95 combobox
KBCategory: kbui
KBSubcategory: UsrDnd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.