EventHandler in C++?

Apr 8, 2009 at 4:45 AM
Hi,

Now I'm trying to create some samples using WiimoteLib in C++/CLI.
How can I add event in C++?

Here is a code sample using C# to retrieve events.
---
namespace WiimoteLib_Sample {
    public partial class SampleForm : Form {
        Wiimote wm = new Wiimote();
        public SampleForm() {
            InitializeComponent();
            wm.WiimoteChanged += wm_WiimoteChanged;
            wm.Connect();
        }
        void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args){
            WiimoteState ws = args.WiimoteState;    //WiimoteStateの値を取得
            if (this.IsHandleCreated) {
                this.Invoke( (MethodInvoker)delegate() {
                    this.DrawForms( ws );
                });
            }
        }
        public void DrawForms(WiimoteState ws) {
            this.label1.Text = "Button A:" + (ws.ButtonState.A);
            this.label2.Text = "Button B:" + (ws.ButtonState.B);
            this.label3.Text = "Button 1:" + (ws.ButtonState.One);
            this.label4.Text = "Button 2:" + (ws.ButtonState.Two);
        }
    }
}
---
In WiimoteLib.chm
[Syntax] of Wiimote..::.WiimoteChanged Event
C#
public event EventHandler<WiimoteChangedEventArgs> WiimoteChanged
Visual Basic (Declaration)
Public Event WiimoteChanged As EventHandler(Of WiimoteChangedEventArgs)
Visual C++
public:
 event EventHandler<WiimoteChangedEventArgs^>^ WiimoteChanged {
	void add (EventHandler<WiimoteChangedEventArgs^>^ value);
	void remove (EventHandler<WiimoteChangedEventArgs^>^ value);
}
It looks there are "add/remove" functions to add/remove events.

On VC2008Express,
---Form1.h---
#pragma once
namespace WiimoteLib_Button {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace WiimoteLib;
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:    Wiimote^    wm;
    public:
        delegate void WiimoteEventHandler( const String^, void* );

    public:
      void wm_WiimoteChanged(System::Object^ sender, WiimoteChangedEventArgs args){
         WiimoteState^ ws;
         ws = args.WiimoteState;
      }
    public:
        Form1(void)
        {
            wm = gcnew Wiimote();
            InitializeComponent();
            wm->WiimoteChanged+=
                gcnew WiimoteEventHandler(this,&Form1::wm_WiimoteChanged); //it cause a comple error C3352.
            wm->Connect();
        }
<snip>
---
It might be a small mistake/misunderstanding...

Of course, now I can use WiimoteLib for LED and Vibrator.
http://www.brianpeek.com/forums/t/800.aspx

Thanks in advance.
akihiko
Apr 10, 2009 at 3:14 AM
Edited Apr 10, 2009 at 3:57 AM
Hi,

The compile error has been solved like below:

    wm->WiimoteChanged +=
        gcnew EventHandler<WiimoteLib::WiimoteChangedEventArgs ^>(this, &Form1::wm_WiimoteChanged);

I'm not sure it is correct or not...
However, the callback function makes another error.

    public:
        void wm_WiimoteChanged(Object^ sender,WiimoteLib::WiimoteChangedEventArgs^ args){
         WiimoteState^ ws;
         ws = args->WiimoteState;

         if (this->IsHandleCreated) {  //this cause error
             this->Invoke( (MethodInvoker)delegate()) {
                 DrawForms(ws);
             }
         }
    }

I'll continue to explore.... If someone already tried to use WiimoteLib on C++/CLI, share your knowledge, please.

akihiko
Apr 10, 2009 at 3:35 AM
I haven't looked at your code, but if you want to see an example of WiimoteLib used with C++/CLI visit http://giimote.googlecode.com.
Apr 10, 2009 at 4:05 AM
thaanks,

It was a great pointer!
I've find some misunderstandings!

Akihiko
Apr 11, 2009 at 1:39 PM

Giimote codes are interested,
it uses WiimoteLib event handler however it haven't use delegates...
--from release note--
Changed event handlers to use generic event handler class instead of Brian Peek's custom delegates(Which were removed in version 1.3 of WiimoteLib
--
From my code..

 wm->WiimoteChanged +=
 gcnew System::EventHandler<WiimoteChangedEventArgs^>(
 this, &Form1::wm_WiimoteChanged);

public:
 void wm_WiimoteChanged(Object^ sender,WiimoteLib::WiimoteChangedEventArgs^ args){
  WiimoteState^ ws;
  ws = args->WiimoteState;
  this->DrawForms(ws);
 }
// Do I need to use Try, Catch and Invoke() in this callback function?
// now this code cause error
public: void DrawForms(WiimoteState^ ws) {
 this->label1->Text = L"test";
 this->label1->Text = "Button:A " + (ws->ButtonState.A); // [error] another thread cannot access to label1
}

It rests just one step to arrive...
akihiko
 

May 26, 2009 at 6:22 AM

// WiimoteLib in C++/CLI by akihiko.shirai.as
#pragma once
namespace WLCLauncher {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
  using namespace WiimoteLib;
public ref class Form1 : public System::Windows::Forms::Form
{
  public: Wiimote^ wm;
public:
Form1(void)
{
  Control::CheckForIllegalCrossThreadCalls = false;
  wm = gcnew Wiimote();
InitializeComponent();
  wm->WiimoteChanged +=
  gcnew System::EventHandler<WiimoteChangedEventArgs^>(
  this, &Form1::wm_WiimoteChanged);
  wm->SetReportType(InputReport::Buttons, true);
  wm->Connect();
}

public: void  wm_WiimoteChanged(Object^ sender,WiimoteLib::WiimoteChangedEventArgs^ args){
  WiimoteState^ ws;
  ws = args->WiimoteState;
  //Button A to launch notepad.exe :-)
  if (ws->ButtonState.A) {
   System::Diagnostics::Process::Start("notepad.exe");
  }
  //Home to exit
  if (ws->ButtonState.Home) {
   Environment::Exit(0);
  }
}

 

// this code will be published in my book "WiiRemote Programming on your PC" (in Japanese)

akihiko