Special CWinApp Services
Besides running the message loop and giving you an opportunity to initialize the application and clean up after it, CWinApp provides several other services.
Shell Registration
By default, the MFC Application Wizard makes it possible for the user to open data files that your application has created by double-clicking them in File Explorer or File Manager. If your application is an MDI application and you specify an extension for the files your application creates, the MFC Application Wizard adds calls to the RegisterShellFileTypes and EnableShellOpen member functions of CWinApp to the InitInstance
override that it writes for you.
RegisterShellFileTypes
registers your application's document types with File Explorer or File Manager. The function adds entries to the registration database that Windows maintains. The entries register each document type, associate a file extension with the file type, specify a command line to open the application, and specify a dynamic data exchange (DDE) command to open a document of that type.
EnableShellOpen
completes the process by allowing your application to receive DDE commands from File Explorer or File Manager to open the file chosen by the user.
This automatic registration support in CWinApp
eliminates the need to ship a .reg file with your application or to do special installation work.
If you want to initialize GDI+ for your application (by calling GdiplusStartup in your InitInstance function), you have to suppress the GDI+ background thread.
You can do this by setting the SuppressBackgroundThread
member of the GdiplusStartupInput structure to TRUE. When suppressing the GDI+ background thread, the NotificationHook
and NotificationUnhook
calls should be made just prior to entering and exiting the application's message loop. For more information on these calls, see GdiplusStartupOutput. Therefore, a good place to call GdiplusStartup
and the hook notification functions would be in an override of the virtual function CWinApp::Run, as shown below:
int CMyWinApp::Run()
{
GdiplusStartupInput gdiSI;
GdiplusStartupOutput gdiSO;
ULONG_PTR gdiToken;
ULONG_PTR gdiHookToken;
gdiSI.SuppressBackgroundThread = TRUE;
GdiplusStartup(&gdiToken, &gdiSI, &gdiSO);
gdiSO.NotificationHook(&gdiHookToken);
int nRet = CWinApp::Run();
gdiSO.NotificationUnhook(gdiHookToken);
GdiplusShutdown(gdiToken);
return nRet;
}
If you do not suppress the background GDI+ thread, DDE commands can be prematurely issued to the application before its main window has been created. The DDE commands issued by the shell can be prematurely aborted, resulting in error messages.
File Manager Drag and Drop
Files can be dragged from the file view window in File Manager or File Explorer to a window in your application. You might, for example, enable one or more files to be dragged to an MDI application's main window, where the application could retrieve the file names and open MDI child windows for those files.
To enable file drag and drop in your application, the MFC Application Wizard writes a call to the CWnd member function DragAcceptFiles for your main frame window in your InitInstance
. You can remove that call if you do not want to implement the drag-and-drop feature.
Note
You can also implement more general drag-and-drop capabilities—dragging data between or within documents—with OLE. For information, see the article OLE drag and drop.
Keeping Track of the Most Recently Used Documents
As the user opens and closes files, the application object keeps track of the four most recently used files. The names of these files are added to the File menu and updated when they change. The framework stores these file names in either the registry or in the .ini file, with the same name as your project and reads them from the file when your application starts up. The InitInstance
override that the MFC Application Wizard creates for you includes a call to the CWinApp member function LoadStdProfileSettings, which loads information from the registry or .ini file, including the most recently used file names.
These entries are stored as follows:
In Windows NT, Windows 2000, and later, the value is stored to a registry key.
In Windows 3.x, the value is stored in the WIN.INI file.
In Windows 95 and later, the value is stored in a cached version of WIN.INI.