The Microsoft Foundation Class (MFC) Library provides an object-oriented wrapper over much of the Win32 and COM APIs. Although it can be used to create very simple desktop applications, it is most useful when we need to develop more complex user interfaces with multiple controls. We can use MFC 11.0 to create applications with Office-style user interfaces.
MSDN provides hierarchy charts included with each class, here.
We will create an application that resembles File Explorer. We will create a window that contains two panes. The left pane will contain a CMFCShellTreeCtrl object that displays our Desktop in a hierarchical view. The right pane will contain a CMFCShellListCtrl that shows the files in the folder that is selected in the left pane.
1>------ Build started: Project: MFCShellControls, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> MFCShellControlsView.cpp 1> MFCShellControlsDoc.cpp 1> MFCShellControls.cpp 1> MainFrm.cpp 1> CalendarBar.cpp 1> Generating Code... 1> MFCShellControls.vcxproj -> c:\users\khyuck\documents\visual studio 2012\Projects\MFCShellControls\Debug\MFCShellControls.exe ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========If the application builds successfully, run the application by selecting Start Debugging from the Debug menu.
In this section, we will add an instance of CMFCShellListCtrl to the view that the wizard created. Open the view header file by double-clicking MFCShellControlsView.h in the Solution Explorer.
Locate the #pragma once directive near the top of the header file. Immediately underneath it add this code to include the header file for CMFCShellListCtrl:
#include <afxShellListCtrl.h>Now add a member variable of type CMFCShellListCtrl. First, locate the following comment in the header file:
// Generated message map functionsImmediately above that comment add this code:
private: CMFCShellListCtrl m_wndList;
// AttributesImmediately under it, add the following method declaration:
public: CMFCShellTreeCtrl& GetShellTreeCtrl();Next, open the MainFrm.cpp source file by double-clicking it in the Solution Explorer. At the bottom of that file, add the following method definition:
CMFCShellTreeCtrl& CMainFrame::GetShellTreeCtrl() { return m_wndTree; }
class CMFCShellControlsView : public CViewNext, in the Properties window, click the Messages icon.
int CMFCShellControlsView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy (0, 0, 0, 0); m_wndList.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT, rectDummy, this, 1); return 0; }
void CMFCShellControlsView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); m_wndList.SetWindowPos(NULL, -1, -1, cx, cy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); }
The last step is to connect the CMFCShellTreeCtrl and CMFCShellListCtrl objects by using the CMFCShellTreeCtrl::SetRelatedList method. After we call this method, the CMFCShellListCtrl will automatically display the contents of the item selected in the CMFCShellTreeCtrl. We will do this in the OnActivateView method, which is overridden from CView::OnActivateView.
In the MFCShellControlsView.h header file, inside the CMFCShellControlsView class declaration, add the following method declaration:
protected: virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView);Next, add the definition for this method to the MFCShellControlsView.cpp source file:
void CMFCShellControlsView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { if (bActivate && AfxGetMainWnd() != NULL) { ((CMainFrame*)AfxGetMainWnd())->GetShellTreeCtrl().SetRelatedList(&m_wndList); } CView::OnActivateView(bActivate, pActivateView, pDeactiveView); }Because we are calling methods from the CMainFrame class, we must add an #include directive at the top of the MFCShellControlsView.cpp source file just the #include "stdafx.h":
#include "MainFrm.h"
Verify that the application was created successfully by building and running it. To build the application, from the Build menu select Build Solution. If the application builds successfully,
1>------ Build started: Project: MFCShellControls, Configuration: Debug Win32 ------ 1> MFCShellControlsView.cpp 1> MFCShellControls.vcxproj -> c:\users\khyuck\documents\visual studio 2012\Projects\MFCShellControls\Debug\MFCShellControls.exe ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========run it by selecting Start Debugging from the Debug menu.
We should now see the details for the item selected in the CMFCShellTreeCtrl in the view pane. When we click a node in the CMFCShellTreeCtrl, the CMFCShellListCtrl will be automatically updated. Likewise, if we double-click a folder in the CMFCShellListCtrl, the CMFCShellTreeCtrl should be automatically updated. Right click any item in the tree control or in the list control. Note that we get the same context menu as if we were using the real File Explorer.