LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to draw a ractangle with the mouse click_drag?

Hello,

 

 

Is there any way to draw a rectangle by dragging the mouse on the canvas?

 

 

David

0 Kudos
Message 1 of 4
(3,557 Views)

Hey David -

 

There is a way to do this.  You simply need to remember the first point you clicked, and the when you get an EVENT_MOUSE_POINTER_MOVE, create a Rect from the first point to the current point.  With this rect, you can call CanvasDrawRect.  You will need to remember the last rect you drew, and delete the old rect before drawing the new one. 

 

Based on some of your other recent questions, I created a little sample that has a transparent canvas on top of a panel's z-plane order.  When you draw the rect on this canvas, it looks like you are drawing a selection box around the controls on the panel.  I also fill the rectangle with a picture control that has been set to have a transparent background, and an image with an alpha channel.  This looks similar to the Windows 7 highlight box that is drawn on the desktop.

 

The one thing that is a little tricky is that you have to hide the canvas when you are not dragging the mouse, otherwise the user can't interact with any of your controls.  This is demonstrated in the example though.

 

NickB_03-04_18-26-35.png

 

Let me know if you have any questions

 

NickB

National Instruments

Message 2 of 4
(3,538 Views)

Thank you Nick. That is what I need.

 

 

David

0 Kudos
Message 3 of 4
(3,517 Views)

If you want to only start drawing the rectangle when the mouse was clicked on the panel (and not on a control), you can set a global flag in the EVENT_LEFT_CLICK of each control callback, and then only set the mouseDown variable in the panel callback if the flag is not set.

 

int CVICALLBACK ctrlCB (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
        {
        case EVENT_LEFT_CLICK:
            gCtrlClicked = 1;
            break;
        }
    return 0;
}

 

int CVICALLBACK panelCB (int panel, int event, void *callbackData,
        int eventData1, int eventData2)
{

...
        case EVENT_LEFT_CLICK:
            if (gCtrlClicked)
                gCtrlClicked = 0;
            else {
                gMouseDown = 1;
                oldp = MakePoint (eventData2, eventData1);
                }
            break;

...

}

0 Kudos
Message 4 of 4
(3,475 Views)