Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








Windows 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.

Windows Explorer still lets users double-click on a program, but it also lets users double-click on a document icon to run the document's program. But how does Windows Explorer know which program to run? Windows Explorer uses the Windows Registry to make the connection between document and program. The link starts with the filename extension that you typed into the MFC Application Wizard, 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 Windows 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 14, 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 the MFC Application Wizard. After you do that, the MFC Application Wizard 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 Ex16a example):

.16a = Ex16a.Document

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


The Registry contains the full pathname of the Ex16a program. Now Windows 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, Windows Explorer will find the document's icon and display it next to the filename.



Double-Clicking on a Document


When the user double-clicks on a document icon, Windows Explorer executes the associated SDI program, passing in the selected filename on the command line. You might notice that the MFC Application Wizard 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. Windows 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 Windows 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 the MFC Application Wizard, 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 Windows 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've built Ex16a, you can try running it from Windows 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 16A file to disk, and then exit Ex16a. Start Windows Explorer and locate the directory in which you saved 16A files. Double-click on one of the 16A files in the panel on the right. Your program should start with the selected file loaded. Now, with both Ex16a and Windows Explorer open on the desktop, try dragging another file from Windows Explorer to the Ex16a window. The program should open the new file just as if you had chosen File Open from the Ex16a menu.

You might also want to look at the Ex16a entries in the Registry. Run the Regedit program (possibly named Regedt32 in Windows 2000 and Windows XP), and expand the HKEY_CLASSES_ROOT key. Look under .16a and Ex16a.Document. Also expand the HKEY_CURRENT_USER key, and look at Local AppWizard-Generated Applications under Software. You should see a Recent File List under the subkey Ex16a. The Ex16a program calls SetRegistryKey with the string Local AppWizard-Generated Applications, so the program name goes beneath the Ex16a subkey.



/ 319