03-03-2011 09:15 PM
Hello,
Is there any way to draw a rectangle by dragging the mouse on the canvas?
David
03-04-2011 06:29 PM
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.
Let me know if you have any questions
NickB
National Instruments
03-05-2011 07:38 AM
Thank you Nick. That is what I need.
David
03-07-2011 12:04 PM
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;
...
}