Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

After another day on Google I managed to figure out how to use a form's member function as a callback in openCV. This involves declaring a delegate function and using System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate to generate a pointer. This is a sufficiently obscure procedure that I will detail exactly what I did below ( based on the information here).

First, I added the following members to my form class, called frmImaging:

//Delegate handling for track bar
public: delegate void ChangeThresholdDelegate(int Th, void *param);
public: ChangeThresholdDelegate^ ChangeThresholdDelegateInstance;
public: cv::TrackbarCallback ChangeThresholdCallbackPointer;
private: System::Void ChangeThreshold(int Threshold, void *param);//The callback function I want to use

I then added the following code to the form constructor:

ChangeThresholdDelegateInstance = gcnew ChangeThresholdDelegate(this, &frmImaging::ChangeThreshold);
IntPtr delegatePointer = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(ChangeThresholdDelegateInstance);
ChangeThresholdCallbackPointer = reinterpret_cast<cv::TrackbarCallback>(delegatePointer.ToPointer());

and then I can create a trackbar with frmImaging->ChangeThreshold(int Th, void *param) as the callback function:

createTrackbar("Histogram threshold", "histogram", &Threshold, hbins, ChangeThresholdCallbackPointer,NULL);//

This procedure allows the trackbar callback to interact with members of a Visual C++ CLI form.

I would still be interested in knowing if there is a more straightforward method.
guy