Explorer Launch and Drag and Drop

In the past, PC users were accustomed to starting up a program and then selecting a disk file (sometimes called a document) that contained data the program understood. Many MS-DOS-based programs worked this way. The old Windows Program Manager improved things by allowing the user to double-click on a program icon instead of typing a program name. Meanwhile, Apple Macintosh users were double-clicking on a document icon; the Macintosh operating system figured out which program to run.

While Windows Explorer still lets users double-click on a program, it also lets users double-click on a document icon to run the document's program. But how does Explorer know which program to run? Explorer uses the Windows Registry to make the connection between document and program. The link starts with the filename extension that you typed into AppWizard, but as you'll see, there's more to it than that. Once the association is made, users can launch your program by double-clicking on its document icon or by dragging the icon from Explorer to a running instance of your program. In addition, users can drag the icon to a printer, and your program will print it.

Program Registration

In Chapter 15, you saw how MFC applications store data in the Windows Registry by calling SetRegistryKey from the InitInstance function. Independent of this SetRegistryKey call, your program can write file association information in a different part of the Registry on startup. To activate this feature, you must type in the filename extension when you create the application with AppWizard. (Use the Advanced button in AppWizard Step 4.) After you do that, AppWizard adds the extension as a substring in your template string and adds the following line in your InitInstance function:

RegisterShellFileTypes(TRUE);

Now your program adds two items to the Registry. Under the HKEY_CLASSES_ROOT top-level key, it adds a subkey and a data string as shown here (for the EX17A example):

.17A = Ex17a.Document

The data item is the file type ID that AppWizard has chosen for you. Ex17a.Document, in turn, is the key for finding the program itself. The Registry entries for Ex17a.Document, also beneath HKEY_CLASSES_ROOT, are shown here.

click to view at full size.

Notice that the Registry contains the full pathname of the EX17A program. Now Explorer can use the Registry to navigate from the extension to the file type ID to the actual program itself. After the extension is registered, Explorer finds the document's icon and displays it next to the filename, as shown here.

Double-Clicking on a Document

When the user double-clicks on a document icon, Explorer executes the associated SDI program, passing in the selected filename on the command line. You might notice that AppWizard generates a call to EnableShellOpen in the application class InitInstance function. This supports execution via DDE message, the technique used by the File Manager in Windows NT 3.51. Explorer can launch your SDI application without this call.

Enabling Drag and Drop

If you want your already-running program to open files dragged from Explorer, you must call the CWnd function DragAcceptFiles for the application's main frame window. The application object's public data member m_pMainWnd points to the CFrameWnd (or CMDIFrameWnd) object. When the user drops a file anywhere inside the frame window, the window receives a WM_DROPFILES message, which triggers a call to FrameWnd::OnDropFiles. The following line in InitInstance, generated by AppWizard, enables drag and drop:

m_pMainWnd->DragAcceptFiles();

Program Startup Parameters

When you choose Run from the Start menu, or when you double-click the program directly in Explorer, there is no command-line parameter. The InitInstance function processes the command line with calls to ParseCommandLine and ProcessShellCommand. If the command line contains something that looks like a filename, the program immediately loads that file. Thus, you create a Windows shortcut that can run your program with a specific document file.

Experimenting with Explorer Launch and Drag and Drop

Once you have built EX17A, you can try running it from Explorer. You must execute the program directly, however, in order to write the initial entries in the Registry. Be sure that you've saved at least one 17A file to disk, and then exit EX17A. Start Explorer, and then open the \vcpp32\ex17a directory. Double-click on one of the 17A files in the panel on the right. Your program should start with the selected file loaded. Now, with both EX17A and Explorer open on the desktop, try dragging another file from Explorer to the EX17A window. The program should open the new file just as if you had chosen File Open from the EX17A menu.

You might also want to look at the EX17A entries in the Registry. Run the Regedit program (possibly named Regedt32 in Windows NT), and expand the HKEY_CLASSES_ROOT key. Look under ".17A" and "Ex17a.Document." Also expand the HKEY_CURRENT_USER (or HKEY_USERS\.DEFAULT) key, and look under "Software." You should see a Recent File List under the subkey ex17a. The EX17A program calls SetRegistryKey with the string "Local AppWizard-Generated Applications", so the program name goes beneath the ex17a subkey.



Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net