(continued)
3. Write the controlling codes in your own application. Here I use Window SDK.
// hwndOrigin is a global variable. It refers to the child window
// of the Script Window (i.e. Edit).
HWND hwndOrigin;
/**
* Initializations:
* 1. Start Origin, but show the script window only.
* 2. Find the handle of the script window.
* 3. Find the handle of the child of the script window (Edit),
* and save it as a global variable.
*/
BOOL initOrigin()
{
// The /h switch hides everything but the script window.
// The -i CommandLine switch insures that the script window will be open.
// Here we assume that CommandLine.ini is stored in D:\Origin
LPCTSTR lpstrPath = "Origin Path Here\\Origin75 /h -i D:\\Origin\\CommandLine";
// The handle already exit.
if (hwndOrigin != NULL)
return true;
// Start Origin from the command line, and
// Find the script window and its child by using FindWindowEx API.
else {
WinExec(lpstrPath, SW_SHOW);
HWND hwndOriginSW = FindWindowEx(NULL, NULL, "ORIGIN_SW", NULL);
if (hwndOriginSW != NULL)
{
hwndOrigin = FindWindowEx(hwndOriginSW, NULL, "Edit", NULL);
if (hwndOrigin != NULL)
{
return true;
}
}
}
// Failure
return false;
}
/**
* Exit Origin by sending 'exit' command to the script window.
*/
void exitOrigin()
{
// doc -s suppresses the "save or not" dialog box.
LPCTSTR lpstrCommand = "doc -s; exit";
// Send the command to the script window.
// WM_KEYDOWN excutes the command.
if (hwndOrigin != NULL)
{
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) lpstrCommand);
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);
}
}
4. Perform fitting
if (!initOrigin())
{
AfxMessageBox("Origin initialization fails");
return 0;
}
// The file name that Origin will read data from
LPSTR lpstrFile = "D:\\Origin\\temp.txt";
// The file name that Origin will save resulting data to
LPSTR lpstrFile2 = "D:\\Origin\\temp2.txt";
// TODO
// Here are codes that can save the data to be fitted to 'lpstrFile'
//
// Open a new window by a user-created template 'Gauss'.
// The template includes the Origin C codes in step 2.
// Thus Origin C codes are automatically compiled when open
// the new data window
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) "win -t data D:\\Origin\\Gauss Data1");
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);
// Send the fitting command to the script window.
char buffer[256];
sprintf(buffer, "gaussian_fit %s %s", lpstrFile, "Data1");
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) buffer);
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);
// TODO
// Here are code that can read data stored in 'lpstrFile2';
//
exitOrigin();
return 0;
5. Reminders:
(1) This is just a prototype. There is much space to improve by combining 'Dialog builder' of Origin C.
(2) If the user's application supports COM, the above codes become the second choice.
(3) Origin runs partially behind the scene, only the Script Window is shown.
(4) The user's application manages its own process of Origin. Other running Origin windows will not be affected.
Edited by - nilaysoft on 02/23/2005 05:29:57 AM