HOWTO: Use Drag-Drop in an Edit Control or a Combo Box

ID: Q86724


The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK)
  • Microsoft Win32 Software Development Kit (SDK)
  • Microsoft Windows 2000


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 query words: combobox

Keywords : kbComboBox kbCtrl kbDragDrop kbEditCtrl kbNTOS kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 26, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.