#region Using using System; using System.Windows.Forms; using ORiN2.interop.CAO; #endregion Using namespace FL_netCaoTester { public partial class Form1 : Form { #region Fields private delegate void SetMessageCallback(CaoMessage pICaoMsg); public CaoEngine caoEng; public CaoWorkspaces caoWss; public CaoWorkspace caoWs; public CaoControllers caoCtrls; public CaoController caoCtrl; //public CaoExtension caoExt; private bool ctrlLoaded; private Form2 Ctrl; #endregion Fields #region Constructor public Form1() { InitializeComponent(); } #endregion Constructor #region Load/Closed private void Form1_Load(object sender, EventArgs e) { this.CtrlEnable(false); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { this.CtrlClose(); } #endregion Load/Closed #region Controller private void button_Add_Click(object sender, EventArgs e) { this.CtrlLoad(); } private void button_Del_Click(object sender, EventArgs e) { this.CtrlClose(); } private void CtrlLoad() { if (!this.ctrlLoaded) { try { button_Add.Enabled = false; // CAO エンジン生成 this.caoEng = new CaoEngine(); this.caoWss = this.caoEng.Workspaces; this.caoWs = this.caoWss.Item(0); // コントローラコレクションの取得 this.caoCtrls = this.caoWs.Controllers; // コントローラに接続 this.caoCtrl = this.caoCtrls.Add(this.textBox_CtrlName.Text, this.textBox_ProvName.Text, this.textBox_MachineName.Text, this.textBox_Option.Text); // OnMessage イベントハンドラの登録 this.caoCtrl.OnMessage += new _ICaoControllerEvents_OnMessageEventHandler(caoCtrl_OnMessage); this.CtrlEnable(true); // コントローラ用ウィンドウ表示 this.Ctrl = new Form2(); this.Ctrl.ShowDialog(this); } catch (Exception ex) { MessageBox.Show(ex.Message); button_Add.Enabled = true; } } } private void CtrlClose() { if (this.ctrlLoaded) { try { button_Del.Enabled = false; // コントローラオブジェクトの解放 if (this.caoCtrl != null) { this.caoCtrl.OnMessage -= new _ICaoControllerEvents_OnMessageEventHandler(caoCtrl_OnMessage); //this.caoCtrls.Remove(this.caoCtrl.Name); this.caoCtrls.Remove(this.caoCtrl.Index); System.Runtime.InteropServices.Marshal.ReleaseComObject(this.caoCtrl); this.caoCtrl = null; } if (this.caoCtrls != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.caoCtrls); this.caoCtrls = null; } if (this.caoWs != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.caoWs); this.caoWs = null; } if (this.caoWss != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.caoWss); this.caoWss = null; } if (this.caoEng != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(this.caoEng); this.caoEng = null; } this.CtrlEnable(false); if (this.Ctrl != null || !this.Ctrl.IsDisposed) { this.Ctrl.Close(); this.Ctrl = null; } } catch (Exception ex) { MessageBox.Show(ex.Message); button_Del.Enabled = true; } } } private void CtrlEnable(bool enable) { if (enable) { this.ctrlLoaded = true; this.button_Add.Enabled = false; this.button_Del.Enabled = true; this.textBox_CtrlName.Enabled = false; this.textBox_ProvName.Enabled = false; this.textBox_MachineName.Enabled = false; this.textBox_Option.Enabled = false; } else { this.ctrlLoaded = false; this.button_Add.Enabled = true; this.button_Del.Enabled = false; this.textBox_CtrlName.Enabled = true; this.textBox_ProvName.Enabled = true; this.textBox_MachineName.Enabled = true; this.textBox_Option.Enabled = true; } } #endregion Controller #region OnMessage void caoCtrl_OnMessage(CaoMessage pICaoMess) { try { // 別スレッドのメソッド呼び出し //SetMessageCallback XXX = new SetMessageCallback(YYY); //Invoke(XXX, pICaoMess); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion OnMessage } }