home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / DRAGDROP / DRAGDROP.C_ / DRAGDROP.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  10.6 KB  |  321 lines

  1. //**************************************************************************
  2. //
  3. //  APPLICATION: 
  4. //      DragDrop.exe                                               
  5. //
  6. //
  7. //  Author:
  8. //      Microsoft Product Support Services.
  9. //
  10. //
  11. //  PURPOSE:                                                                
  12. //      This sample uses the Windows 3.1 Drag and Drop interface.  It 
  13. //      displays  the name of the files, in a list box, that are dragged 
  14. //      from FileManager on this app.                                                            
  15. //
  16. //
  17. //  FUNCTIONS:                                                              
  18. //
  19. //  WinMain()     - Creates the main window and its child list box.         
  20. //                - Registers the main window to accept Drag/Drop messages. 
  21. //                - Enters the message loop.                                
  22. // 
  23. //  MainWndProc() - Processes main window's messages.                       
  24. //                - Also handles Drag/Drop messages.                        
  25. // 
  26. //  About()       - Processes the About dialog's messages.                  
  27. // 
  28. //  Info()        - Processes the Info dialog's messages.                   
  29. //                - Displays info on the files dropped.                     
  30. //
  31. //
  32. //  Copyright (c) 1992 Microsoft Corporation. All rights reserved.
  33. //
  34. //**************************************************************************
  35.  
  36. #include <windows.h>
  37. #include "shellapi.h"   // Contains Drag/Drop APIs
  38. #include "dragdrop.h"
  39.  
  40. HANDLE  ghInst;
  41. HWND    ghListBox;
  42. POINT   gpointDrop = {0,0};  // Point where the files were dropped
  43. WORD    gwFilesDropped = 0;  // Total number of files dropped
  44.  
  45. //**************************************************************************
  46. //
  47. //  FUNCTION: WinMain()                                                     
  48. //
  49. //  PURPOSE:                                                                
  50. //                                                                           
  51. //  This function creates the main window and the list box, registers the   
  52. //  main window to accept Drag/Drop messages, and goes in the message loop. 
  53. //
  54. //**************************************************************************
  55.  
  56. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  57.                     LPSTR lpCmdLine,  int nCmdShow)
  58. {
  59.     MSG         msg;
  60.     WNDCLASS    wc;
  61.     HWND        hMainWnd;
  62.     RECT        rectMain;
  63.  
  64.     if (!hPrevInstance)
  65.     {
  66.         wc.style = NULL;
  67.         wc.lpfnWndProc = MainWndProc;
  68.         wc.cbClsExtra = 0;
  69.         wc.cbWndExtra = 0;
  70.         wc.hInstance = hInstance;
  71.         wc.hIcon = LoadIcon (hInstance, "DragDrop");
  72.         wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  73.         wc.hbrBackground = GetStockObject (WHITE_BRUSH);
  74.         wc.lpszMenuName =  "DragDropMenu";
  75.         wc.lpszClassName = "DragDropWClass";
  76.  
  77.         if (!RegisterClass (&wc))
  78.             return (FALSE);
  79.     }
  80.  
  81.     hMainWnd = CreateWindow ("DragDropWClass",
  82.                             "Drag and Drop Client Application",
  83.                             WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
  84.                             | WS_MINIMIZEBOX,
  85.                             CW_USEDEFAULT,
  86.                             CW_USEDEFAULT,
  87.                             MAIN_WIDTH,
  88.                             MAIN_HEIGHT,
  89.                             NULL,
  90.                             NULL,
  91.                             hInstance,
  92.                             NULL);
  93.  
  94.     if (!hMainWnd)
  95.         return (FALSE);
  96.  
  97.     GetClientRect (hMainWnd, &rectMain);
  98.  
  99.     ghListBox = CreateWindow ("ListBox",
  100.                         NULL,
  101.                         WS_CHILD | WS_VSCROLL | LBS_NOINTEGRALHEIGHT
  102.                         | LBS_SORT | WS_VISIBLE,
  103.                         0,
  104.                         0,
  105.                         rectMain.right - rectMain.left,
  106.                         rectMain.bottom - rectMain.top,
  107.                         hMainWnd,
  108.                         1,
  109.                         hInstance,
  110.                         NULL);
  111.  
  112.     if (!ghListBox)
  113.         return (FALSE);
  114.  
  115.     // Register the the main window for Drag/Drop messages.
  116.     DragAcceptFiles (hMainWnd, TRUE);
  117.  
  118.     ShowWindow (hMainWnd, nCmdShow);
  119.     UpdateWindow (hMainWnd);
  120.  
  121.     ghInst = hInstance;
  122.  
  123.     while (GetMessage (&msg,
  124.                         NULL,
  125.                         NULL,
  126.                         NULL))
  127.     {
  128.         TranslateMessage (&msg);
  129.         DispatchMessage (&msg);
  130.     }
  131.     return (msg.wParam);
  132. } // WinMain()
  133.  
  134.  
  135. //**************************************************************************
  136. //
  137. //  FUNCTION: MainWndProc()                                                     
  138. //
  139. //  PURPOSE:                                                                
  140. //                                                                           
  141. //  This function handles messages belonging to the main window.
  142. //  It also handles and processes Drag/Drop messages.
  143. //
  144. //**************************************************************************
  145.  
  146. long FAR PASCAL __export MainWndProc (HWND hWnd,   UINT message,
  147.                  WPARAM wParam, LPARAM lParam)
  148. {
  149.     FARPROC lpDialogProc;
  150.     HANDLE  hFilesInfo;
  151.     WORD    wIndex;
  152.     char    szFileName [FILE_NAME_LENGTH];
  153.  
  154.     switch (message)
  155.     {
  156.         case WM_DROPFILES:
  157.             hFilesInfo = (HANDLE) wParam;
  158.  
  159.             // Retrieve the window coordinates of the mouse
  160.             // pointer when the drop was made
  161.             DragQueryPoint ((HANDLE) wParam, (LPPOINT) &gpointDrop);
  162.  
  163.             // Get the total number of files dropped
  164.             gwFilesDropped = DragQueryFile (hFilesInfo,
  165.                        (UINT)-1,
  166.                                            NULL,
  167.                                            0);
  168.  
  169.             // Retrieve each file name and add to the list box
  170.             for (wIndex=0; wIndex < gwFilesDropped; wIndex++)
  171.             {
  172.                 DragQueryFile (hFilesInfo,
  173.                                wIndex,
  174.                                (LPSTR) szFileName,
  175.                                FILE_NAME_LENGTH);
  176.  
  177.                 SendMessage (ghListBox,
  178.                              LB_ADDSTRING,
  179.                              0,
  180.                              (LONG) (LPSTR) szFileName);
  181.             } // for 
  182.  
  183.             DragFinish (hFilesInfo);
  184.             break;
  185.  
  186.         case WM_COMMAND:
  187.             switch (wParam)
  188.             {
  189.                 case IDM_ABOUT:
  190.                     lpDialogProc = MakeProcInstance (About, ghInst);
  191.                     DialogBox (ghInst,
  192.                                "AboutBox",
  193.                                hWnd,
  194.                                lpDialogProc);
  195.                     FreeProcInstance (lpDialogProc);
  196.                     break;
  197.  
  198.                 case IDM_INFO:
  199.                     if (gwFilesDropped)
  200.                     {
  201.                         lpDialogProc = MakeProcInstance (Info, ghInst);
  202.                         DialogBox (ghInst,
  203.                                 "InfoBox",
  204.                                 hWnd,
  205.                                 lpDialogProc);
  206.                         FreeProcInstance (lpDialogProc);
  207.                     }
  208.                     else
  209.                         MessageBox (hWnd, "Files have not been dropped yet.",
  210.                                     "DragDrop", MB_OK | MB_ICONASTERISK);
  211.                     break;
  212.  
  213.                 default:
  214.                     return (DefWindowProc (hWnd, message, wParam, lParam));
  215.             }  // switch (wParam) 
  216.             break;
  217.  
  218.         case WM_DESTROY:
  219.             PostQuitMessage (0);
  220.             break;
  221.  
  222.         default:
  223.             return (DefWindowProc (hWnd, message, wParam, lParam));
  224.  
  225.     } // switch (message)
  226.  
  227.     return (0);
  228.  
  229. } // MainWndProc()
  230.  
  231.  
  232. //**************************************************************************
  233. //
  234. //  FUNCTION: About()                                                     
  235. //
  236. //  PURPOSE:                                                                
  237. //                                                                           
  238. //  This function handles messages belonging to the "About" dialog box.
  239. //  The only message that it looks for is WM_COMMAND, indicating that the
  240. //  user has pressed the "OK" button.  When this happens, it takes down
  241. //  the dialog box.
  242. //  
  243. //**************************************************************************
  244.  
  245. BOOL FAR PASCAL __export About (HWND hDlg,   unsigned message,
  246.                        WORD wParam, LONG lParam)
  247. {
  248.     switch (message)
  249.     {
  250.         case WM_INITDIALOG:
  251.             return (TRUE);
  252.  
  253.         case WM_COMMAND:
  254.             switch (wParam)
  255.             {
  256.                 case IDOK:
  257.                 case IDCANCEL:
  258.                     EndDialog (hDlg, TRUE);
  259.                     return (TRUE);
  260.  
  261.                 default:
  262.                     break;
  263.             } // switch (wParam)
  264.     } // switch (message)
  265.  
  266.     return (FALSE);
  267.  
  268. } // About()
  269.  
  270.  
  271. //**************************************************************************
  272. //
  273. //  FUNCTION: Info()                                                     
  274. //
  275. //  PURPOSE:                                                                
  276. //                                                                           
  277. //  This function handles messages belonging to the "Info" dialog box.
  278. //  It displays information on the files dropped.  The only message that
  279. //  it looks for is WM_COMMAND, indicating that the user has pressed the
  280. //  "OK" button.  When this happens, it takes down the dialog box.
  281. //  
  282. //**************************************************************************
  283.  
  284. BOOL FAR PASCAL __export Info (HWND hDlg,   unsigned message,
  285.                       WORD wParam, LONG lParam)
  286. {
  287.     char    szTemp [STRING_LEN];
  288.  
  289.     switch (message)
  290.     {
  291.         case WM_INITDIALOG:
  292.             wsprintf (szTemp, "%d", gwFilesDropped);
  293.             SetDlgItemText (hDlg, IDD_NUMFILES, (LPSTR) szTemp);
  294.             
  295.             wsprintf (szTemp, "%d", gpointDrop.x);
  296.             SetDlgItemText (hDlg, IDD_XCORD, (LPSTR) szTemp);
  297.  
  298.             wsprintf (szTemp, "%d", gpointDrop.y);
  299.             SetDlgItemText (hDlg, IDD_YCORD, (LPSTR) szTemp);
  300.  
  301.             return (TRUE);
  302.  
  303.         case WM_COMMAND:
  304.             switch (wParam)
  305.             {
  306.                 case IDOK:
  307.                 case IDCANCEL:
  308.                     EndDialog (hDlg, TRUE);
  309.                     return (TRUE);
  310.  
  311.                 default:
  312.                     break;
  313.             } // switch (wParam)
  314.     } // switch (message)
  315.  
  316.     return (FALSE);
  317.  
  318. } // Info()
  319.  
  320.  
  321.