1

我很难找到如何在 C# 中使用HOperatorSet.SetDrawingObjectCallback(HTuple drawID, HTuple drawObjectEvent, HTuple callbackFunction)( DocuHTuple callbackFunction ),特别是回调的部分。除了中文网站(链接),我找不到任何关于如何正确执行此操作的示例。该网站本身也不是很容易找到,并且那里使用的代码会引发致命异常。为了让其他人有更好的资源来了解如何使用该HOperatorSet.SetDrawingObjectCallback方法,我决定创建这个问题并自己回答。

4

1 回答 1

0

这里的关键是HTuple callbackFunction是一个指向类型委托的函数指针HDrawingObject.HDrawingObjectCallback。然后,此委托链接到您正确的回调方法。回调方法包括一个intptrtodrawingId和 thewindowId以及一个string type.

这是代码片段,希望能让你的一天变得更好:

public class SomeClass
{
    public SomeClass()
    {
        DrawingObjectCallback = DrawingObjectCallbackHandler;
    }
    
    public void AddDrawingObject()
    {
        HOperatorSet.CreateDrawingObjectCircle(50, 50, 100, out var drawId);
        
        //Get the pointer to HDrawingObjectCallback which links to our handler
        var ptr = Marshal.GetFunctionPointerForDelegate(DrawingObjectCallback);
                
        //Select the events you want to listen to
        var listenTo = new HTuple("on_resize", "on_drag");
        
        //Finally call the method
        HOperatorSet.SetDrawingObjectCallback(drawId, listenTo, ptr);
        
        HOperatorSet.AttachDrawingObjectToWindow(HalconWindow.HalconID, drawId);
    }

    public HDrawingObject.HDrawingObjectCallback DrawingObjectCallback { get; }
    public void DrawingObjectCallbackHandler(IntPtr drawId, IntPtr windowHandle, string type)
    {
        int id = new HTuple(drawId);
        int windowId  = new HTuple(windowHandle);
        
        //Now you have the two Ids as integers and can work from here!
    }
}
于 2020-07-15T05:42:06.367 回答