1.1.6 Creating Custom DialogX-Function Custom Dialog
X-Functions support custom dialog created in a dialog builder as interface. If the user wants to use the customized dialog, and also wants to support Auto Update in the dialog, or wants to open the dialog from the Origin menu, then they can create an X-Function with customized dialog. And then can create an OPX file to share it to others.
Creating a Dailog Resource DLL
Here is an example to show you how to build a resource-only DLL in Visual Studio 2008 or 2010 which is accessible in Origin C.
Follow the section Creating a Resource-Only DLL, we will get two files: Welcome.dll and resource.h.
Creating X-Function
- Copy the above two files Welcome.dll and resource.h to Origin <User files Folder\OriginC\>. Note: the resource DLL file and resource.h file should be saved in the same folder.
- Press F10 to open X-Function Builder.
- Type "CustomDlg" as the X-Function name and set the X-Function variables as below:
- Click the Tree View button to switch to Tree View, and choose Usage Context -> Menus -> Auto GetN Dialog to Custom. Then type the custom dialog function name, for example "OpenCustomDlg", into the Custom Dialog Function edit box. Click Save button to save this X-Function to the default folder under User Files folder and then close X-Function Builder. Note:X-Function should be save to the X-Functions folder under OriginExe folder or User Files Folder
- Create a new header file named CustomDlg.h and save it to the "OriginC" folder. Type the following code into this file. The code in the CustomDialog construct function is used to show how to get the X-Function settings from the X-Function to the dialog, and how to set back the settings from the dialog to the X-Function by way of the Origin C TreeNode variable.
#include <..\Originlab\DialogEx.h>
#include "resource.h"
// file name of the resource without extension, here stand for Welcome.dll,
//and it assume the DLL is in the same folder as this .h file
#define STR_DLG_RESOURCE_DLL "Welcome"
// the file name of resource DLL
#ifdef _OWIN64
#define STR_DLG_RESOURCE_DLL "Welcome_64" // 64 bit DLL
#else //!_OWIN64
#define STR_DLG_RESOURCE_DLL "Welcome" // 32 bit DLL
#endif// _OWIN64
class CustomDialog : public ResizeDialog
{
public:
// Important: IDD_MY_DIALOG is the dialog resource ID.
// STR_DLG_RESOURCE_DLL is the name of the dialog resource DLL file.
CustomDialog(TreeNode& trXFSettings) : ResizeDialog( IDD_MY_DIALOG, STR_DLG_RESOURCE_DLL )
{
m_trXFSettings = trXFSettings;
}
int DoModalEx(HWND hParent = NULL)
{
InitMsgMap();
return ResizeDialog::DoModal(hParent);
}
protected:
EVENTS_BEGIN
ON_INIT(OnInitDialog)
ON_OK(OnOK)
EVENTS_END
BOOL OnInitDialog()
{
ResizeDialog::OnInitDialog();
Edit ed;
ed = GetDlgItem(IDC_EDIT1);
// init dialog control with xf setting
ed.Text = ftoa(m_trXFSettings.factor.dVal);
return TRUE;
}
BOOL OnOK()
{
Edit ed;
ed = GetDlgItem(IDC_EDIT1);
m_trXFSettings.factor.dVal = atof(ed.Text);
// If the input is 0, it will be changed to 1 automatically.
if ( m_trXFSettings.factor.dVal == 0 )
m_trXFSettings.factor.dVal = 1;
return TRUE;
}
private:
TreeNode m_trXFSettings;
};
- Open Origin Code Builder, in the menu choose File -> Open, select your X-Function, make sure the Add to Workspace check box is checked in the Open dialog, and click OK. CustomDlg.XFC is added to the User folder in Origin C Workspace view and shows the function body in Edit view.
- Add the additional header file after the line //put additional include files here, as in the following.
//put additional include files here
#include "CustomDlg.h"
- Add a function to open the custom dialog after the line //put your own support static functions here, as below. The function name is input into the Tree View Custom Dialog Function edit box. The function interface, return type, and argument list should remain the same, as in the following.
//put your own support static functions here
bool OpenCustomDlg(TreeNode& tr, PEVENT_FUNC pfn, LPCSTR lpcszTitle, LPCSTR
lpcszDescription, HWND hWndParent)
{
CustomDialog dlg(tr);
if( dlg.DoModal(GetWindow()) )
{
tr.AutoUpdate.nVal = AU_AUTO; // Default is 2(Manual)
return true;
}
else // click Cancel button on dialog
{
return false; // will not call main function
}
}
- Put the following code into the X-Function's main function - CustomDlg function
out = in * factor;
// put comments like "Col(A)*8" on the Comments label of output column
DataRange drIn;
drIn = in.GetSourceDataRange();
Worksheet wks;
int c1, c2;
drIn.GetRange(wks, c1, c2);
Column col(wks, c1);
if( wks && col )
{
string strOutputComment;
strOutputComment.Format("Col(%s)*%g", col.GetName(), factor);
out.SetOutputComment(strOutputComment);
}
- Click button.
Using X-Function
- Create a worksheet with two column and fill some different data.
- Highlight column A and Run "CustomDlg -d" in the Command Window.
- Fill "8" to Factor.
- Click the OK button to close the dialog.
- A new column will be created with an Auto Update lock (Column(A)*factor).
- If the input is 0, it will be changed to 1 automatically.
- Changing any data in column A will trigger Auto Update to update the output column.
How to share your custom dialog with others
You can use Package Manager to distribute it as a single package file to other Origin users. Then he can install your application by dropping the package file into Origin directly.
- Select menu Tool->Package Manager. Click Add Files button to add the four files. CustomDlg.XFC, CustomDlg.h, resource.h, Welcome.dll.
- Select File-> Save to save them as "CustomDlg.opx" file and share it with other users.
|