|
|
MS Visual C++: My First Graph
Adding Control to the View Class:
In some applications, such as an SDI or MDI application, you will want to embed a control in a window of the application. The Create member function of the wrapper class, inserted by Component Gallery, can create an instance of the control dynamically, without the need of a dialog box.
The Create member function has the following parameters:
|
lpszWindowName |
A pointer to the text to be displayed in the control’s Text or Caption property (if any). |
|
dwStyle |
Windows styles. For a complete list, see CWnd::CreateControl. |
|
rect |
Specifies the control’s size and position. |
|
pParentWnd |
Specifies the control’s parent window, usually a CDialog. It must not be NULL. |
|
nID |
Specifies the control ID and can be used by the container to refer to the control. |
One example of using this function to dynamically create an OLE control would be in a form view of an SDI application. You could then create an instance of the control in the WM_CREATE handler of the application.
For this example, CMyView is the main view class, CFlpGrf is the wrapper class, and FlpGrf.H is the header (.H) file of the wrapper class.
Implementing this feature is a four-step process:
1. Insert FlpGrf.H
in CMYVIEW.H, just before the CMyView class definition:
#include "FlpGrf.h"
2. Add a member variable (of type CFlpGrf) to the protected section of the CMyView class definition located in CMYVIEW.H:
{
...
protected:
CFlpGrf m_Graph1;
...
};
3. Add a WM_CREATE message handler to class CMyView.
4. In the handler function, CMyView::OnCreate, make a call to the control’s Create function using the this pointer as the parent window:
{
if (MyView::OnCreate(lpCreateStruct) == -1)
return -1;
m_Graph1.Create(NULL, WS_VISIBLE, CRect( 50,50,350,200), this, 0 );
m_Graph1.SetColumn(0);
m_Graph1.SetColumnAxis(0);
m_Graph1.SetColumn(1);
m_Graph1.SetColumnAxis(1);
m_Graph1.SetColumnType(1);
m_Graph1.SetGraphTitle("My First Graph;July 22, 1996");
// VT_R8 (dblVal), VT_DATE (date), VT_BSTR (bstrVal)
VARIANT va;
VariantInit(&va);
CString str;
va.vt = VT_BSTR;
str = _T("Jan");
va.bstrVal = str.AllocSysString();
m_Graph1.SetDataValue(1, 0, va);
str = _T("Feb");
va.bstrVal = str.SetSysString(&va.bstrVal);
m_Graph1.SetDataValue(2, 0, va);
str = _T("Mar");
va.bstrVal = str.SetSysString(&va.bstrVal);
m_Graph1.SetDataValue(3, 0, va);
SysFreeString( va.bstrVal );
va.vt = VT_R8;
va.dblVal = 1.3;
m_Graph1.SetDataValue(1, 1, va);
va.dblVal = 4.3;
m_Graph1.SetDataValue(2, 1, va);
va.dblVal = 5.2;
m_Graph1.SetDataValue(2, 2, va);
return 0;
}
![]() |
Last modified on: Friday, August 16, 2002