This section briefly describes how to add FlowChartX ActiveX control in CView class in the MFC Document/View application. For detailed information how to use FlowChartX under Microsoft VC++ 7 please, refer to VC++ 7 Quick Start (MFC, Dialog application).
In MSVC environment choose "File -> New -> Project". In the dialog box that appears, select "Visual C++ Projects" from Project Types list and "MFC Application" from Templates list. Give your project a suitable name and click OK. MFC Application Wizard starts building a new project. Click "Application type" in the dialog that appears and set it to "Single Document". Leave the default values for the rest of the options. Next click "Advanced features" and make sure "ActiveX controls" check box is selected. Choose any other options you might need and press Finish. The wizard generates the new project.
Note |
---|
Instead deriving your own View class from CView MFC Class, you can derive it from CFormView Class. That saves you writing explicit code to notify the control to draw itself in OnDraw. Most of code written below as an example is also applicable for CFormView-derived classes. |
Choose "Project -> Add Class...". In dialog that appears select "Visual C++ -> MFC" from Categories tree and "MFC Class From ActiveX Control" from Templates list and hit Open to open next dialog. From "Available ActiveX Controls" combo box find FlowChartX and add IFlowChart interface to "Generated classes" list. Fill in appropriate names for the class, the .h, .cpp files and click Finish.
Add following code in View header file just before the class declaration:
C++ Copy Code |
---|
#include "FlowChart.h" |
Let's assume the control header file is called FlowChart.h. Declare a new variable for FlowChartX control in the private section of the View class. Since the control is windowless and cannot create its own scrollers add manually two scrollbars. Here is how source code should look:
C++ Copy Code |
---|
private: |
Go back to View class implementation file and make the necessary adjustments to make the control visible in the View area. First, handle WM_CREATE message in the View class. Most easily you can do that by using Messages pane in Properties bar. The OnCreate method should also create the FlowChartX control and both scrollbars as shown below:
C++ Copy Code |
---|
int CMFCViewView::OnCreate(LPCREATESTRUCT lpCreateStruct) |
Now a handler for WM_SIZE message should be added. It's triggered when the user resizes application window and we should put code to resize control visible area and both scrollbars. Following code snippet implements that:
C++ Copy Code |
---|
void CMFCViewView::OnSize(UINT nType, int cx, int cy) |
To notify the control when the user interacts with external scrollbars, handle WM_HSCROLL and WM_VSCROLL messages sent to CMFCViewView window. Underneath you can find one possible implementation how to do that:
C++ Copy Code |
---|
void CMFCViewView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) |
After external scrollbars are connected to FlowChartX control, it is time to add some functionality to OnDraw method. By default, CView doesn't notify windowless controls to repaint themselves, which results in not properly updated appearance. Following code changes handle that problem:
C++ Copy Code |
---|
void CMFCViewView::OnDraw(CDC* pDC) |
The application performance can be improved further more by disabling background repainting of the View window. It is redundant, because the control is painted on the whole client area. That can be done by handling WM_ERASEBKGND message and returning TRUE instead of calling the base class method as shown below:
C++ Copy Code |
---|
BOOL CMFCViewView::OnEraseBkgnd(CDC* pDC) |
As a result FlowChartX paints flicker-free when the user moves or resizes the main window.
Additional features might, for example, include zoom-in and zoom-out. Let's draw an arrow-like button in lower-right corner besides the scrollbars. When clicked 'Zoom' menu pops up. As a first step to do that create the menu in the Resource Editor - something like that:
ID of the menu is IDR_CONTEXTMENU and IDs of the items are respectively - IDM_ZOOM50, IDM_ZOOM100, IDM_ZOOM200. Add a message handler for WM_LBUTTONDOWN message and implement it as follows:
C++ Copy Code |
---|
void CMFCViewView::OnLButtonDown(UINT nFlags, CPoint point) |
Finally add command handles for the three menu items:
C++ Copy Code |
---|
// Menu handles |
Remember to add following three lines in the message map implementation:
C++ Copy Code |
---|
BEGIN_MESSAGE_MAP(CMFCViewView, CView) |
Finish by adding following three lines to View header file:
C++ Copy Code |
---|
public: |