/// <summary> /// 程序中所有图表的窗口的句柄值,用来对窗口进行禁用或者启用窗口 /// </summary> /// <param name="AllDrawings"></param> /// <returns></returns> /// <remarks></remarks> public static IntPtr[] GetWindwosHandles(AmeDrawings AllDrawings) { IntPtr[] arrHandles = new IntPtr[AllDrawings.Count() - 1 + 1]; try { int index = 0; AmeDrawings with_1 = AllDrawings; if (with_1.PlanView != null) { arrHandles[index] = with_1.PlanView.Application.Window.WindowHandle32; index++; } if (with_1.SectionalView != null) { arrHandles[index] = with_1.SectionalView.Application.Hwnd; index++; } foreach (ClsDrawing_Mnt_Base mnt in with_1.MonitorData) { arrHandles[index] = mnt.Application.Hwnd; index++; } } catch (Exception ex) { MessageBox.Show("获取窗口句柄出错" + "\r\n" + ex.Message + "\r\n" + "报错位置:" + ex.TargetSite.Name, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(arrHandles); }
/// <summary> /// 退出主程序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <remarks> /// 这里用FormClosing事件来控制主程序的退出,而不用Dispose事件来控制, /// 是为了解决其子窗口在各自的FormClosing事件中只是将其进行隐藏并取消了默认的关闭操作, /// 所以这里在主程序最先触发的FormClosing事件中就直接执行Me.Dispose()方法,这样就可以 /// 跳过子窗口的FormClosing事件,而直接退出主程序了。 /// </remarks> public void mainForm_FormClosing(object sender, FormClosingEventArgs e) { AmeDrawings AllDrawing = GlbApp.ExposeAllDrawings(); if (AllDrawing.Count() > 0) { DialogResult result = MessageBox.Show("还有图表未处理,是否要关闭所有绘图并退出程序", "tip", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); if (result == DialogResult.Yes) //关闭AME主程序,同时也关闭所有的绘图程序。 { Hide(); //' --- 通过新的工作线程来执行绘图程序的关闭。 //Dim thd As New Thread(AddressOf Me.QuitDrawingApplications) //With thd // .Name = "关闭所有绘图程序" // .Start(AllDrawing) // thd.Join() //End With // --- //通过Main Thread来执行绘图程序的关闭。 QuitDrawingApplications(AllDrawing); } //不关闭图表,但关闭程序 else if (result == DialogResult.No) { } //图表与程序都不关闭 else if (result == DialogResult.Cancel) { e.Cancel = true; return; } } // ---------------------- 先隐藏主界面,以达到更好的UI效果。 Hide(); //--------------------------- 断后工作 try { //关闭隐藏的Excel数据库中的所有工作簿 foreach (Workbook wkbk in GlbApp.ExcelApplication_DB.Workbooks) { wkbk.Close(false); } GlbApp.ExcelApplication_DB.Quit(); //这一步非常重要哟 GlbApp.ExcelApplication_DB = null; } catch (Exception) { //有可能会出现数据库文件已经被关闭的情况 } //保存主程序窗口关闭时的界面位置与大小 APPLICATION_MAINFORM with_1 = this; mySettings_UI mysetting = new mySettings_UI(); mysetting.WindowLocation = with_1.Location; mysetting.WindowSize = with_1.Size; mysetting.WindowState = with_1.WindowState; mysetting.Save(); //--------------------------- //这里在主程序最先触发的FormClosing事件中就直接执行Me.Dispose()方法, //这样就可以跳过子窗口的FormClosing事件,而直接退出主程序了。 Dispose(); }