Writing a Drag Event HandlerCognex VisionPro

This topic shows you how to write a drag handler that is called while you drag an interactive shape within a Cognex Display control. VisionPro provides two drag handlers for interactive shapes. One drag handler, Dragging, is called repeatedly as you drag the shape. The other, DraggingStopped, is called only when you stop dragging the shape.

You may want to use a drag handler to limit where a shape can be dragged or to constrain its dimensions. This example uses a drag handler to limit a rectangle to a square shape when the Shift key is held down.

Before You Begin

This example assumes that you have installed VisionPro and that you are familiar displaying images in a VisionPro display control.

The examples in this walkthrough are given in both C# and Visual Basic.

Create the Shape and Add It to the Display Control

This example shows the use of CogRectangle. It assumes that you have created a Cognex Display Control called cogDisplay1.

C# Code

CogRectangle mRect = new CogRectangle();

mRect.Interactive = true;
mRect.GraphicDOFEnable = CogRectangleDOFConstants.All;
cogDisplay1.InteractiveGraphics.Add(mRect,"rect",true);

VB.NET Code

Dim mRect As New CogRectangle

mRect.Interactive = True
mRect.GraphicDOFEnable = CogRectangleDOFConstants.All
cogDisplay1.InteractiveGraphics.Add(mRect, "rect", True)
Add Key Handlers to the Display Object

This example needs to know whether the Shift key is pressed, so it defines a KeyDown and KeyUp handler for the display object. The state of the shift key is stored in the global variable _shiftIsDown.

First, add the handlers to the the KeyDown and KeyUp events.

C# Code

cogDisplay1.KeyDown +=new KeyEventHandler(cogDisplay1_KeyDown);
cogDisplay1.KeyUp += new KeyEventHandler(cogDisplay1_KeyUp);

VB.NET Code

AddHandler cogDisplay1.KeyDown, AddressOf cogDisplay1_KeyDown
AddHandler cogDisplay1.KeyUp, AddressOf cogDisplay1_KeyUp

Then define the event handlers.

C# Code

private void cogDisplay1_KeyDown(object sender, KeyEventArgs e) {
    _shiftIsDown =  e.Shift;
}

private void cogDisplay1_KeyUp(object sender, KeyEventArgs e) {
    _shiftIsDown =  e.Shift;
}

VB.NET Code

Private Sub cogDisplay1_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs)
    _shiftIsDown = e.Shift
End Sub

Private Sub cogDisplay1_KeyUp(ByVal sender As System.Object, ByVal e As KeyEventArgs)
    _shiftIsDown = e.Shift
End Sub
Add Drag Handlers to the Rectangle

The dragging handlers limit the rectangle to a square when the Shift key is pressed.

First, add the handlers to the Dragging and DraggingStopped events.

C# Code

mRect.Dragging += new CogDraggingEventHandler(mRect_Dragging);
mRect.DraggingStopped += new CogDraggingStoppedEventHandler(mRect_DraggingStopped);

VB.NET Code

AddHandler mRect.Dragging, AddressOf mRect_Dragging
AddHandler mRect.DraggingStopped, AddressOf mRect_DraggingStopped

Next, write the drag handlers. In this example, the height and width of the shape are set to be the same whenever the Shift key is down.

C# Code

private void mRect_Dragging(object sender, CogDraggingEventArgs e) {
    CogRectangle dragRect = (CogRectangle) e.DragGraphic;

    if (_shiftIsDown)
        dragRect.Height = dragRect.Width;
}

private void mRect_DraggingStopped(object sender, CogDraggingEventArgs e) {
        mRect_Dragging(sender, e);
}

VB.NET Code

Private Sub mRect_Dragging(ByVal sender As System.Object, ByVal e As CogDraggingEventArgs)
    Dim dragRect As CogRectangle

    dragRect = CType(e.DragGraphic, CogRectangle)

    If _shiftIsDown Then
        dragRect.Height = dragRect.Width
    End If
End Sub

Private Sub mRect_DraggingStopped(ByVal sender As System.Object, ByVal e As CogDraggingEventArgs)
    mRect_Dragging(sender, e)
End Sub

One dragging handler, mRect_Dragging is called as long as you drag the shape. The other, mrect_DraggingStopped is called after dragging ends.

Both handlers take the same arguments. The second argument provides a DragGraphic property that represents the shape that would result if you stopped dragging it at the time the function is called. Since DragGraphic returns a generic ICogInteractiveGraphic, you have to cast it to the specific shape you are dragging.

To make sure that the constraint imposed by the Shift key is enforced even when dragging stops, the mrect_DraggingStopped just calls the regular mrect_Dragging handler.