質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

2回答

2967閲覧

【C# Wiimote】電子黒板プログラム作成、デリケードについて

Gluek731

総合スコア26

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/02/09 10:18

前回に電子黒板システムの実装を試み、取れないバグがあったので質問させてもらった者です。
今回はコーディングについて悩みがあったので質問させていただきます。

キャリブレーションをフォーム内のボタンを押した際に処理を行うようにしたいのですが、関数の引数の欄に専用のライブラリ(WiimoteState)を追加してビルドすると「デリゲート 'System.EventHandler' に一致する 'button4_Click' のオーバーロードはありません。」と言うメッセージを吐いてきます。

キャリブレーションの仕組みとしては以下の通りです

  1. 左上に照準を描画する
  2. 赤外線を感知した際にその位置を記録する
  3. 描画した照準を消し、今度は右上に描画
  4. 2と同じ
  5. 3と同じく、今度は左下に描画
  6. 2と同じ
  7. 3と同じく、今度は右下に描画
  8. 2と同じ

この処理は赤外線を感知し、その値を格納する関数を持つ専用のライブラリが必要です。そのライブラリを何とかそのボタンの関数に渡したいのですが、何かいい方法は無いでしょうか。

分かる方が居ましたらご回答お願いします。

###ソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Windows.Forms; 9using WiimoteLib; //ライブラリの使用を宣言 10 11namespace WiimoteLib01 12{ 13 public partial class Form1 : Form 14 { 15 Wiimote wm = new Wiimote(); //クラスを作成 16 System.Drawing.Point ScreenSize; //画面サイズ格納 17 Boolean isConnected = false; //Wiimoteの接続判定 18 public Form1() 19 { 20 21 InitializeComponent(); 22 //他スレッドからコントロールの許可 23 Control.CheckForIllegalCrossThreadCalls = false; 24 } 25 //WiiRemoteの状態が変化した時に呼ばれる関数 26 #region 27 void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args) 28 { 29 if (isConnected == true) 30 { 31 WiimoteState ws = args.WiimoteState; 32 Calibration(ws); 33 DrawForms(ws); 34 IR_Cursor(ws); 35 Events(ws); 36 EffectsOut(ws); 37 } 38 else 39 { 40 this.wm.SetLEDs(0); 41 this.wm.SetRumble(false); 42 this.wm.Disconnect(); 43 this.wm.Dispose(); 44 } 45 }//wm_WiimoteChanged 46 #endregion 47 //ボタンイベント 48 #region 49 public void Events(WiimoteState ws) 50 { 51 52 }//Events 53 #endregion 54 //赤外線マウス移動 55 #region 56 public void IR_Cursor(WiimoteState ws) 57 { 58 //スクリーンの大きさ 59 ScreenSize.X = Screen.PrimaryScreen.Bounds.Width; 60 ScreenSize.Y = Screen.PrimaryScreen.Bounds.Height; 61 float Ix1 = 0.5f, Iy1 = 0.5f, Ix0 = 0.5f, Iy0 = 0.5f; 62 float Ix, Iy; 63 int px, py; 64 if (ws.IRState.IRSensors[0].Found) 65 { 66 Ix1 = ws.IRState.IRSensors[1].Position.X; 67 Iy1 = ws.IRState.IRSensors[1].Position.Y; 68 Ix0 = ws.IRState.IRSensors[0].Position.X; 69 Iy0 = ws.IRState.IRSensors[0].Position.Y; 70 if (Ix1 < Ix0) 71 { 72 Ix0 = Ix1; Iy0 = Iy1; 73 Ix1 = ws.IRState.IRSensors[0].Position.X; 74 Iy1 = ws.IRState.IRSensors[0].Position.Y; 75 } 76 Ix = Ix0; Iy = Iy0; 77 px = (int)(ScreenSize.X * (1 - Ix)); 78 py = (int)(Iy * ScreenSize.Y); 79 System.Windows.Forms.Cursor.Position = new System.Drawing.Point(px, py); 80 } 81 }//IR_Cursor 82 #endregion 83 //接続が押されたら 84 #region 85 private void button1_Click(object sender, EventArgs e) 86 { 87 if (this.isConnected == false) 88 { 89 this.wm = new Wiimote(); 90 this.wm.Connect(); 91 this.wm.SetReportType(InputReport.IRAccel, true); 92 this.wm.SetLEDs(0); 93 this.wm.SetRumble(false); 94 this.button1.Enabled = false; 95 this.button2.Enabled = true; 96 this.wm.WiimoteChanged += wm_WiimoteChanged; 97 this.isConnected = true; 98 99 } 100 }//button1_Click 101 #endregion 102 //切断が押されたら 103 #region 104 private void button2_Click(object sender, EventArgs e) 105 { 106 if (this.isConnected == true) 107 { 108 this.wm.WiimoteChanged -= wm_WiimoteChanged; 109 this.button1.Enabled = true; 110 this.button2.Enabled = false; 111 this.isConnected = false; 112 } 113 }//button2_Click 114 #endregion 115 //装飾 116 #region 117 public void EffectsOut(WiimoteState ws) 118 { 119 120 }//EffectsOut 121 #endregion 122 //フォーム描画関数 123 #region 124 private void DrawForms(WiimoteState ws) 125 { 126 //グラフィックス取得 127 Graphics g = this.pictureBox1.CreateGraphics(); 128 g.Clear(Color.Black); 129 //赤外線を発見したら 130 if (ws.IRState.IRSensors[0].Found) 131 { 132 g.FillEllipse(Brushes.Red, 133 ws.IRState.IRSensors[0].Position.X * 256, 134 ws.IRState.IRSensors[0].Position.Y * 128, 5, 5); 135 g.FillEllipse(Brushes.Blue, 136 ws.IRState.IRSensors[1].Position.X * 256, 137 ws.IRState.IRSensors[1].Position.Y * 128, 5, 5); 138 } 139 g.Dispose();//グラフィックスの解放 140 label1.Text = "IR[0] " + ws.IRState.IRSensors[0].RawPosition.ToString() 141 + "/nIR[1] " + ws.IRState.IRSensors[1].RawPosition.ToString(); 142 }//DrawForms 143 #endregion 144 145 private void label1_Click(object sender, EventArgs e) 146 { 147 148 } 149 150 private void pictureBox1_Click(object sender, EventArgs e) 151 { 152 153 } 154 155 private void Form1_Load(object sender, EventArgs e) 156 { 157 158 } 159 160 private void pictureBox2_Click(object sender, EventArgs e) 161 { 162 163 } 164 165 //フルスクリーン表示にする処理 166 #region 167 private void button3_Click(object sender, EventArgs e) 168 { 169 //ウィンドウサイズが普通ならば 170 if (this.WindowState == FormWindowState.Normal) 171 { 172 //大きくする 173 this.FormBorderStyle = FormBorderStyle.None; 174 this.WindowState = FormWindowState.Maximized; 175 } 176 else 177 { 178 //小さくする 179 this.FormBorderStyle = FormBorderStyle.None; 180 this.WindowState = FormWindowState.Normal; 181 } 182 183 }//button3_Click 184 #endregion 185 //キャリブレーションを行う処理 186 #region 187 public void Calibration(WiimoteState ws) 188 { 189 190 int i=1; 191 //ウィンドウのサイズを格納する変数 192 float X_Screen = 0; 193 float Y_Screen = 0; 194 195 //Imageオブジェクトの生成 196 Bitmap canvas = new Bitmap(pictureBox2.Width, pictureBox2.Height); 197 Bitmap canvas2 = new Bitmap(pictureBox3.Width, pictureBox3.Height); 198 Bitmap canvas3 = new Bitmap(pictureBox4.Width, pictureBox4.Height); 199 Bitmap canvas4 = new Bitmap(pictureBox5.Width, pictureBox5.Height); 200 //ImageオブジェクトのGraphicオブジェクトを作成 201 Graphics g = Graphics.FromImage(canvas); 202 Graphics g2 = Graphics.FromImage(canvas2); 203 Graphics g3 = Graphics.FromImage(canvas3); 204 Graphics g4 = Graphics.FromImage(canvas4); 205 //画像の読み込み 206 Image img = Image.FromFile(@"C:\Users\Gluek\Documents\Visual Studio 2010\Projects\WiimoteLib01\WiimoteLib01\img\rticle.png"); 207 //cavasの座標に描画する 208 g.DrawImage(img, 0, 0, 50, 50); 209 g2.DrawImage(img, 0, 0, 50, 50); 210 g3.DrawImage(img, 0, 0, 50, 50); 211 g4.DrawImage(img, 0, 0, 50, 50); 212 //gとimgのリソース解放 213 //img.Dispose(); 214 g.Dispose(); 215 g2.Dispose(); 216 g3.Dispose(); 217 g4.Dispose(); 218 //キャリブレーションの処理(未完) 219 pictureBox2.Image = canvas; 220 while (i < 2) 221 { 222 if (ws.IRState.IRSensors[0].Found) 223 { 224 pictureBox2.Image.Dispose(); 225 pictureBox2.Image = null; 226 i = 2; 227 } 228 } 229 i = 1; 230 while (i < 2) 231 { 232 pictureBox5.Image = canvas4; 233 if (ws.IRState.IRSensors[0].Found) 234 { 235 pictureBox5.Image.Dispose(); 236 pictureBox5.Image = null; 237 i = 2; 238 } 239 } 240 }//Calibration 241 #endregion 242 //キャリブレーションを実行する処理 243 #region 244 private void button4_Click(object sender, EventArgs e, WiimoteState ws)//ここでエラーが出る 245 { 246 Calibration(ws); 247 } 248 #endregion 249 } 250}

回答よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

こんにちは。

sho_csさんの言う通り、イベントハンドラの宣言を変更しては行けません。
C#のフレームワークはprivate void button4_Click(object sender, EventArgs e)をリンクしようとするのですが、Gluek731さんが、private void button4_Click(object sender, EventArgs e, WiimoteState ws)と変更したため、見失ってます。

ところで、Form1の頭で定義しているWiimote wm = new Wiimote();の中にWiimoteStateが入っていませんか? もし、入っているなら、button4_Click()がCalibration()を呼び出す時、それを渡せばよい可能性があります。ドキュメントで確認してみてください。

投稿2016/02/09 12:19

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

イベントを受けるメソッドの引数は定義されたものから変更することができません。
以下のブログの解説がわかりやすいかと思います。
[C#]イベントハンドラとはなんぞや – gomokulog

投稿2016/02/09 10:41

sho_cs

総合スコア3541

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問