1.18.1.4.3 Graph Preview DialogGraph Preview Dialog
This section shows how to create custom dialog with a graph preview.
Prepare Dialog Resource
We first need a dialog resource containing a static control, in which the preview graph will nest. Here we will use a built-in resource, IDD_SAMPLE_SPLITTER_DLG, in OriginC\Originlab\ODlg8.dll.
Prepare Source File
In Code Builder, click New button, type file name, and set Location as the same path of the above dialog resource dll oDlg8.dll - Origin install path OriginC\Originlab subfolder.
Including Needed Headers
//These headers contain declarations of dialog and controls
#include <..\Originlab\DialogEx.h>
#include <..\Originlab\GraphPageControl.h>
Adding User Defined Preview ClassClasses, User Defined
//forbid some action on preview graph
#define PREVIEW_NOCLICK_BITS (NOCLICK_DATA_PLOT|NOCLICK_LAYER|NOCLICK_LAYERICON)
#define PREVIEW_TEMPLATE "Origin" //preview graph template
class MyPreviewCtrl
{
public:
MyPreviewCtrl(){}
~MyPreviewCtrl()
{
//destroy temporary books when dialog closed.
if ( m_wksPreview.IsValid() )
m_wksPreview.Destroy();
}
void Init(int nCtrlID, WndContainer& wndParent)
{
//create preview graph control
Control ctrl = wndParent.GetDlgItem(nCtrlID);
GraphControl gCtrl;
gCtrl.CreateControl(ctrl.GetSafeHwnd());
gCtrl.Visible = true;
GraphPageControl gpCtrl;
gpCtrl.Create(gCtrl, PREVIEW_NOCLICK_BITS, PREVIEW_TEMPLATE);
GraphPage gpPreview;
gpPreview = gpCtrl.GetPage();
gpPreview.Rename("MyPreview");
m_glPreview = gpPreview.Layers(0); //first layer
if ( !m_wksPreview )
{
//temporary worksheet to hold preview data.
m_wksPreview.Create("Origin", CREATE_TEMP);
m_wksPreview.SetSize(-1, 2); //two columns
//long name will be displayed as axis title
Column colX(m_wksPreview, 0);
colX.SetLongName("Preview X");
Column colY(m_wksPreview, 1);
colY.SetLongName("Preview Y");
//prepare datarange
DataRange drPrev;
drPrev.Add(m_wksPreview, 0, "X");
drPrev.Add(m_wksPreview, 1, "Y");
//plot preview curve, although it has no points now.
int nPlot = m_glPreview.AddPlot(drPrev, IDM_PLOT_LINE);
DataPlot dp = m_glPreview.DataPlots(nPlot);
if ( dp ) //set preview curve color
dp.SetColor(SYSCOLOR_RED);
}
}
//update preview curve with external data.
void Update(const vector& vX, const vector& vY)
{
if ( m_wksPreview.IsValid() )
{
Dataset dsX(m_wksPreview, 0);
Dataset dsY(m_wksPreview, 1);
if ( !dsX.IsValid() || !dsY.IsValid() )
return; //no columns for preview
//update source data will also update preview graph.
dsX = vX;
dsY = vY;
//rescale graph for better view.
m_glPreview.Rescale();
}
}
private:
//preview graph on dialog
GraphLayer m_glPreview;
//temporary worksheet to put preview data.
Worksheet m_wksPreview;
};
Adding Dialog Class
class MyGraphPreviewDlg : public MultiPaneDlg
{
public:
//dialog resource ID and the DLL containing it.
MyGraphPreviewDlg() : MultiPaneDlg(IDD_SAMPLE_SPLITTER_DLG,
GetAppPath(TRUE) + "OriginC\\Originlab\\ODlg8")
{
}
~MyGraphPreviewDlg()
{
}
int DoModalEx(HWND hParent = NULL)
{
InitMsgMap();
//show dialog until user closes it.
return DoModal(hParent, DLG_NO_DEFAULT_REPOSITION);
}
protected:
EVENTS_BEGIN
ON_INIT(OnInitDialog)
ON_BN_CLICKED(IDC_LOAD, OnDraw)
EVENTS_END
//message handler of dialog events
BOOL OnInitDialog();
BOOL OnDraw(Control ctrl);
private:
//member stands for the preview control
MyPreviewCtrl m_Preview;
};
BOOL MyGraphPreviewDlg::OnInitDialog()
{
m_Preview.Init(IDC_FB_BOX, *this);
Button btn = GetItem(IDC_LOAD);
if( btn )
btn.Text = "Draw";
return true;
}
BOOL MyGraphPreviewDlg::OnDraw(Control ctrl)
{
vector vecX, vecY;
vecX.Data(1.0, 10.0, 0.5);
vecY.SetSize(vecX.GetSize());
for(int ii = 0; ii < vecX.GetSize(); ++ii)
vecY[ii] = rnd();
m_Preview.Update(vecX, vecY);
return true;
}
Open the Dialog
void open_preview_dlg()
{
MyGraphPreviewDlg dlg;
dlg.DoModalEx(GetWindow());
return;
}
Execute the function above, and click the Draw button. You will notice the preview be updated.
|