Ask Your Question

how to call windows CLI form from createTrackbar

asked 2015-02-10 15:29:02 -0500

Guyygarty gravatar image

updated 2015-02-11 17:33:46 -0500


I am using OpenCV from Visual C++ 2013, using the CLI managed interface for handling the GUI. I have an openCV named window that displays the histogram of an image and I want to use a trackbar to set the threshold for binarization.

My problem is that I cant figure out how to define the onChange function within the context of CLI. I have tried defining a public method public: void myForm::ChangeThreshold(int,void*); in the form but I am forbidden from generating a pointer referencing it since it is a manged member. I can generate an unmanaged function but it would need to have a handle to the form so that it can update the threshold value in a textbox myForm::txtThreshold.

Any suggestions would be appreciated.


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-12 16:36:26 -0500

Guyygarty gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-02-10 15:29:02 -0500

Seen: 690 times

Last updated: Feb 12 '15