private static bool OpenArcScene() { IDocument doc = null; try { doc = new ESRI.ArcGIS.ArcScene.SxDocument(); } catch { return(false); } if (doc != null) { m_appROTEvent = new AppROTClass(); m_appROTEvent.AppRemoved += new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_application = doc.Parent; m_application.Visible = true; m_appHWnd = m_application.hWnd; } else { m_appROTEvent = null; m_application = null; return(false); } return(true); }
private void btnShutdown_Click(object sender, EventArgs e) { if (m_application != null) { //Try to close any modal dialogs by sending the Escape key //It doesn't handle the followings: //- VBA is up and has a modal dialog //- Modal dialog doesn't close with the Escape key Microsoft.VisualBasic.Interaction.AppActivate(m_application.Caption); int nestModalHwnd = 0; while ((nestModalHwnd = GetLastActivePopup(m_application.hWnd)) != m_application.hWnd) { SendKeys.SendWait("{ESC}"); } //Manage document dirty flag - abandon changes IDocumentDirty2 docDirtyFlag = (IDocumentDirty2)m_application.Document; docDirtyFlag.SetClean(); //Stop listening before exiting m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_appROTEvent = null; //Exit m_application.Shutdown(); m_application = null; //Reset UI for next automation txtShapeFilePath.Enabled = false; btnShutdown.Enabled = btnDrive.Enabled = false; cboApps.Enabled = btnStartApp.Enabled = true; } }
internal static void ClosingHandler() { if (m_appROTEvent != null) { m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_appROTEvent = null; } }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { //Clean up if (m_appROTEvent != null) { m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_appROTEvent = null; } }
private void btnStartApp_Click(object sender, EventArgs e) { IDocument doc = null; try { this.Cursor = Cursors.WaitCursor; switch (cboApps.SelectedItem.ToString()) { case "ArcMap": doc = new ESRI.ArcGIS.ArcMapUI.MxDocumentClass(); break; case "ArcScene": doc = new ESRI.ArcGIS.ArcScene.SxDocumentClass(); break; case "ArcGlobe": doc = new ESRI.ArcGIS.ArcGlobe.GMxDocumentClass(); break; } } catch { } //Fail if you haven't installed the target application finally { this.Cursor = Cursors.Default; } if (doc != null) { //Advanced (AppROT event): Handle manual shutdown, comment out if not needed m_appROTEvent = new AppROTClass(); m_appROTEvent.AppRemoved += new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); //Get a reference of the application and make it visible m_application = doc.Parent; m_application.Visible = true; m_appHWnd = m_application.hWnd; //Enable/disable controls accordingly txtShapeFilePath.Enabled = true; btnShutdown.Enabled = true; btnDrive.Enabled = ShouldEnableAddLayer; cboApps.Enabled = btnStartApp.Enabled = false; } else { m_appROTEvent = null; m_application = null; txtShapeFilePath.Enabled = false; btnShutdown.Enabled = btnDrive.Enabled = false; cboApps.Enabled = btnStartApp.Enabled = true; } }
static void m_appROTEvent_AppRemoved(AppRef pApp) { //Application manually shuts down. Stop listening if (pApp.hWnd == m_appHWnd) { m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_appROTEvent = null; m_application = null; m_appHWnd = 0; } }
void m_appROTEvent_AppRemoved(AppRef pApp) { //Application manually shuts down. Stop listening if (pApp.hWnd == m_appHWnd) //compare by hwnd { m_appROTEvent.AppRemoved -= new IAppROTEvents_AppRemovedEventHandler(m_appROTEvent_AppRemoved); m_appROTEvent = null; m_application = null; m_appHWnd = 0; //Reset UI has to be in the form UI thread of this application, //not the AppROT thread; if (this.InvokeRequired) //i.e. not on the right thread { this.BeginInvoke(new IAppROTEvents_AppRemovedEventHandler(AppRemovedResetUI), pApp); } else { AppRemovedResetUI(pApp); //call directly } } }