- 放送に接続していない時操作できないようにする
- 放送に接続したら操作できるようにする
- [開始]押したら操作できないようにする
- コメントを投稿したら操作できるようにする
- メソッドにまとめて引数boolでコントロールのEnabledを変更するようにする
- フォーム側に実装してClass1で呼び出して使用するようにする
下記のフォーム側のソースにメソッドを追加していきます
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ClassLibrary1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } }
- 引数boolのenabledメソッドを作る(bool変数は適当にEnabledとする)
- Class1から使えるようにpublicにする
- 戻り値は無いのでvoid
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ClassLibrary1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void enabled(bool Enabled) { } } }
- 内容を実装
- if〜else文で引数がtrueだったらコントロールのEnabledをtrueにする
/// <summary> /// bool引数でコントロールの操作をNO/OFFする /// </summary> /// <param name="Enabled">trueの時は操作できる</param> public void enabled(bool Enabled) { if (Enabled) { commentnumber.Enabled = true; comment.Enabled = true; start.Enabled = true; } else { commentnumber.Enabled = false; comment.Enabled = false; start.Enabled = false; } }
ClassLibrary1のソースで
- フォームを起動したら放送に接続しているか見る
- 放送に接続したら呼ばれるメソッドを追加する
- 放送から切断したら呼ばれるメソッドを追加する
以下のメソッドに追加していく
/// <summary> /// プラグイン実行時に呼ばれる /// </summary> public void Run() { if (form == null) { //フォームの生成 form = new Form1(); //オーナー設定 form.Owner = host.MainForm; //フォームの表示 form.Show(); //フォームクローズイベント form.FormClosing += new System.Windows.Forms.FormClosingEventHandler(form_FormClosing); } }
前回同様イベントは+=まで書いてTab2回押しでVC#に任せる
/// <summary> /// プラグイン実行時に呼ばれる /// </summary> public void Run() { if (form == null) { //フォームの生成 form = new Form1(); //オーナー設定 form.Owner = host.MainForm; //フォームの表示 form.Show(); //フォームクローズイベント form.FormClosing += new System.Windows.Forms.FormClosingEventHandler(form_FormClosing); //IsConnectedは放送に接続しているか form.enabled(host.IsConnected); //接続・切断のイベントを追加する host.BroadcastConnected += new Plugin.BroadcastConnectedEventHandler(host_BroadcastConnected); host.BroadcastDisConnected += new Plugin.BroadcastDisConnectedEventHandler(host_BroadcastDisConnected); } } void host_BroadcastConnected(object sender, EventArgs e) { throw new NotImplementedException(); } void host_BroadcastDisConnected(object sender, EventArgs e) { throw new NotImplementedException(); }
放送に接続したらコントロールを使えるようにする
/// <summary> /// 放送に接続したときに呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void host_BroadcastConnected(object sender, EventArgs e) { form.enabled(true); }
放送から切断したらコントロールを使えないようにする
/// <summary> /// 放送から切断したときに呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void host_BroadcastDisConnected(object sender, EventArgs e) { form.enabled(false); }
Form1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ClassLibrary1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// bool引数でコントロールの操作をNO/OFFする /// </summary> /// <param name="Enabled">trueの時は操作できる</param> public void enabled(bool Enabled) { if (Enabled) { commentnumber.Enabled = true; comment.Enabled = true; start.Enabled = true; } else { commentnumber.Enabled = false; comment.Enabled = false; start.Enabled = false; } } } }Class1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ClassLibrary1 { public class Class1 : Plugin.IPlugin { //ホストをもらう変数 Plugin.IPluginHost host = null; //フォームの変数 Form1 form = null; /// <summary> /// プラグインのホスト /// </summary> public Plugin.IPluginHost Host { get { return this.host; } set { this.host = value; } } /// <summary> /// プラグインの名前 /// </summary> public string Name { get { return "キリ番ゲッター"; } } /// <summary> /// プラグインのバージョン /// </summary> public string Version { get { return "1.0"; } } /// <summary> /// プラグインの説明 /// </summary> public string Description { get { return "キリ番にコメントを投稿する"; } } /// <summary> /// プラグイン実行時に呼ばれる /// </summary> public void Run() { if (form == null) { //フォームの生成 form = new Form1(); //オーナー設定 form.Owner = host.MainForm; //フォームの表示 form.Show(); //フォームクローズイベント form.FormClosing += new System.Windows.Forms.FormClosingEventHandler(form_FormClosing); //IsConnectedは放送に接続しているか form.enabled(host.IsConnected); //接続/切断のイベントを追加する host.BroadcastConnected += new Plugin.BroadcastConnectedEventHandler(host_BroadcastConnected); host.BroadcastDisConnected += new Plugin.BroadcastDisConnectedEventHandler(host_BroadcastDisConnected); } } /// <summary> /// 放送に接続したときに呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void host_BroadcastConnected(object sender, EventArgs e) { form.enabled(true); } /// <summary> /// 放送から切断したときに呼ばれる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void host_BroadcastDisConnected(object sender, EventArgs e) { form.enabled(false); } /// <summary> /// プラグインフォームを閉じたときに起きる /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void form_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { form = null; } /// <summary> /// NCVを起動したときに「AutoRun()」を実行するか /// </summary> public bool IsAutoRun { get { return false; } } /// <summary> /// IsAutoRunがtrueの時NCVを実行したときに呼ばれる /// </summary> public void AutoRun() { //throw new NotImplementedException(); } } }
VC#でデバック>ソリューションのビルド
先ほど保存したところに「ClassLibrary1.dll」が新しくできています
Projects\ClassLibrary1\ClassLibrary1\bin\Release\ClassLibrary1.dll
先ほど保存したところに「ClassLibrary1.dll」が新しくできています
Projects\ClassLibrary1\ClassLibrary1\bin\Release\ClassLibrary1.dll