www.pudn.com > DalsaNetlink.rar > CyExternalProgramPage.cpp


// CyExternalProgramPage.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "resource.h" 
#include "resource.hm" 
#include "CyExternalProgramPage.h" 
#include "CyApp.h" 
#include "CyAcquisitionPage.h" 
#include  
#include  
#include  
 
#include  
#include  
using namespace std; 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CyExternalProgramPage property page 
 
IMPLEMENT_DYNCREATE(CyExternalProgramPage, CPropertyPage) 
 
CyExternalProgramPage::CyExternalProgramPage() : CPropertyPage(CyExternalProgramPage::IDD) 
{ 
	//{{AFX_DATA_INIT(CyExternalProgramPage) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
CyExternalProgramPage::~CyExternalProgramPage() 
{ 
} 
 
void CyExternalProgramPage::DoDataExchange(CDataExchange* pDX) 
{ 
	CPropertyPage::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CyExternalProgramPage) 
	DDX_Control(pDX, IDC_EXTERNAL_PROGRAM, mProgram); 
	DDX_Control(pDX, IDC_EXTERNAL_DIRECTORY, mDirectory); 
	DDX_Control(pDX, IDC_EXTERNAL_ARGS, mArguments); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CyExternalProgramPage, CPropertyPage) 
	//{{AFX_MSG_MAP(CyExternalProgramPage) 
	ON_BN_CLICKED(IDC_LAUNCH_EXTERNAL, OnLaunchExternal) 
	ON_BN_CLICKED(IDC_EXTERNAL_DIRECTORY_BROWSE, OnDirectoryBrowse) 
	ON_BN_CLICKED(IDC_EXTERNAL_PROGRAM_BROWSE, OnProgramBrowse) 
	ON_WM_HELPINFO() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CyExternalProgramPage message handlers 
 
void CyExternalProgramPage::OnLaunchExternal()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lAppConfig = lApp->GetConfig(); 
 
	CString lTemp; 
    string lProgram; 
    string lArguments; 
    string lDirectory; 
 
    // get the info from the dialog 
    mProgram.GetWindowText( lTemp ); 
    lProgram = (LPCTSTR) lTemp; 
    mArguments.GetWindowText( lTemp ); 
    lArguments = (LPCTSTR) lTemp; 
    mDirectory.GetWindowText( lTemp ); 
    lDirectory = (LPCTSTR) lTemp; 
 
    // create a string with the program and arguments. 
    ostringstream lProgramString; 
    lProgramString << lProgram << " " << lArguments; 
 
    // start the external process 
    STARTUPINFO lStartup; 
    memset( &lStartup, 0, sizeof( lStartup ) ); 
    lStartup.cb = sizeof( lStartup ); 
    PROCESS_INFORMATION lProcess; 
    if ( ::CreateProcess( NULL, 
                          (char *) lProgramString.str().c_str(), 
                          NULL, 
                          NULL, 
                          TRUE, 
                          0, 
                          NULL, 
                          lDirectory.c_str(), 
                          & lStartup, 
                          & lProcess ) ) 
    { 
        CloseHandle( lProcess.hProcess ); 
 
        // save the current settings for next time 
        AfxGetApp()->WriteProfileString( "", 
                                         "ExternalProgramPath", 
                                         lProgram.c_str() ); 
        AfxGetApp()->WriteProfileString( "", 
                                         "ExternalProgramDirectory", 
                                         lDirectory.c_str() ); 
        AfxGetApp()->WriteProfileString( "", 
                                         "ExternalProgramArguments", 
                                         lArguments.c_str() ); 
    } 
    else 
    { 
        LPVOID lpMsgBuf; 
        FormatMessage(  
            FORMAT_MESSAGE_ALLOCATE_BUFFER |  
            FORMAT_MESSAGE_FROM_SYSTEM |  
            FORMAT_MESSAGE_IGNORE_INSERTS, 
            NULL, 
            GetLastError(), 
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 
            (LPTSTR) &lpMsgBuf, 
            0, 
            NULL  
        ); 
 
        // Process any inserts in lpMsgBuf. 
        // ... 
        // Display the string. 
        MessageBox( (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); 
 
        // Free the buffer. 
        LocalFree( lpMsgBuf ); 
    } 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//The call back is used to set the initial directory and 
//display the selected path in thhe top of the box. 
static string gStartDirectory; 
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)  
{ 
    TRACE( _T("BrowseCallbackProc(0x%x,%d,%p,%p)\n"), hwnd, uMsg, lp, pData ); 
    TCHAR szDir[MAX_PATH]; 
     
    if (uMsg == BFFM_INITIALIZED ) 
    { 
        //Initial directory is set here 
        strcpy( szDir, gStartDirectory.c_str() ); 
        { 
            // WParam is TRUE since you are passing a path. 
            // It would be FALSE if you were passing a pidl. 
            SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szDir); 
        } 
    } 
 
    return 0; 
} 
 
void CyExternalProgramPage::OnDirectoryBrowse()  
{ 
    // Get the directory from the control 
	CString lTemp; 
    string lDirectory; 
    mDirectory.GetWindowText( lTemp ); 
    lDirectory = (LPCTSTR) lTemp; 
 
    char lSelection[MAX_PATH]; 
    strcpy( lSelection, lDirectory.c_str() ); 
 
    BROWSEINFO lPBI; 
    memset( &lPBI, 0, sizeof( BROWSEINFO ) ); 
    lPBI.hwndOwner = GetSafeHwnd(); 
    lPBI.pszDisplayName = lSelection; 
    lPBI.lpszTitle = "Browse for folder"; 
    lPBI.ulFlags = BIF_VALIDATE; 
    lPBI.ulFlags &= ~BIF_EDITBOX ; 
    lPBI.lpfn = BrowseCallbackProc; 
    gStartDirectory = lDirectory; 
 
    ITEMIDLIST * lItemID = SHBrowseForFolder( & lPBI ); 
    if ( lItemID != NULL ) 
    { 
        SHGetPathFromIDList( (LPITEMIDLIST)lItemID ,lSelection ); 
 
        mDirectory.SetWindowText( lSelection ); 
    } 
    else if ( GetLastError() != 0 ) 
    { 
        LPVOID lpMsgBuf; 
        FormatMessage(  
            FORMAT_MESSAGE_ALLOCATE_BUFFER |  
            FORMAT_MESSAGE_FROM_SYSTEM |  
            FORMAT_MESSAGE_IGNORE_INSERTS, 
            NULL, 
            GetLastError(), 
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 
            (LPTSTR) &lpMsgBuf, 
            0, 
            NULL  
        ); 
 
        // Process any inserts in lpMsgBuf. 
        // ... 
        // Display the string. 
        MessageBox( (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION ); 
 
        // Free the buffer. 
        LocalFree( lpMsgBuf ); 
    } 
} 
 
void CyExternalProgramPage::OnProgramBrowse()  
{ 
    // Get the directory from the control 
	CString lTemp; 
    string lProgram; 
    mProgram.GetWindowText( lTemp ); 
    lProgram = (LPCTSTR) lTemp; 
 
    char lSelection[MAX_PATH]; 
    strcpy( lSelection, lProgram.c_str() ); 
 
    // Browsing for the camera file 
    OPENFILENAME lOFN; 
    memset( &lOFN, 0, sizeof( OPENFILENAME ) ); 
    lOFN.hwndOwner = GetSafeHwnd(); 
    lOFN.lStructSize = sizeof( OPENFILENAME ); 
    lOFN.lpstrFilter  = "Executable File (*.exe)\0*.exe\0\0"; 
    lOFN.lpstrFile = lSelection; 
    lOFN.nMaxFile = MAX_PATH; 
    lOFN.lpstrTitle = "Browse for a camera configuration program"; 
    lOFN.Flags = OFN_FILEMUSTEXIST | 
                 OFN_PATHMUSTEXIST; 
    lOFN.lpstrDefExt = "exe"; 
    if ( GetOpenFileName( &lOFN ) ) 
    { 
        mProgram.SetWindowText( lSelection ); 
    } 
    else if ( CommDlgExtendedError() != 0 ) 
    { 
        ostringstream lMessage; 
        lMessage << "Error finding file because of error: " << CommDlgExtendedError(); 
        MessageBox( lMessage.str().c_str(), "Error", MB_OK | MB_ICONINFORMATION ); 
    } 
} 
 
BOOL CyExternalProgramPage::OnInitDialog()  
{ 
	CPropertyPage::OnInitDialog(); 
	 
    CString lProgram   ( AfxGetApp()->GetProfileString( "", 
                                                        "ExternalProgramPath" ) ); 
    CString lDirectory ( AfxGetApp()->GetProfileString( "", 
                                                        "ExternalProgramDirectory" ) ); 
    CString lArguments ( AfxGetApp()->GetProfileString( "", 
                                                        "ExternalProgramArguments" ) ); 
 
    mProgram.SetWindowText  ( lProgram ); 
    mDirectory.SetWindowText( lDirectory ); 
    mArguments.SetWindowText( lArguments ); 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
BOOL CyExternalProgramPage::OnHelpInfo(HELPINFO* pHelpInfo)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	return CPropertyPage::OnHelpInfo(pHelpInfo); 
} 
 
BOOL CyExternalProgramPage::OnSetActive()  
{ 
	return CPropertyPage::OnSetActive(); 
}