Code sample Home

Get several entities by LC_EVENT_LBDOWN event

When user presses left mouse button and <Ctrl> key, we try to retrieve all entities at cursor position. For each found entity, we change its color to yellow/red.

...
lcEventSetProc( LC_EVENT_LBDOWN, EventProc, 0, 0 );
...
  
//-----------------------------------------------
void CALLBACK EventProc (HANDLE hEvent)
{
  int EventType;
  EventType = lcPropGetInt( hEvent, LC_PROP_EVENT_TYPE );
  switch( EventType ){
    ...
    case LC_EVENT_LBDOWN:  
      OnLBDown( hEvent );  
      break;
    ...
  }
}

// Variant 1
//-----------------------------------------------
void OnLBDown (HANDLE hEvent)
{
  HANDLE hLcWnd, hEnt;
  int    i, nEnts, Color;
  double X, Y, Delta, Lef, Bot, Rig, Top;
  BOOL   bCtrl;

  // State of Ctrl key: 1-pressed, 0-released
  bCtrl = lcPropGetInt( hEvent, LC_PROP_EVENT_INT4 ); 
  if (bCtrl){
    // graphics window handle
    hLcWnd = lcPropGetHandle( hEvent, LC_PROP_EVENT_WND );
    // get size of pickbox square
    Delta = lcPropGetFloat( hLcWnd, LC_PROP_WND_PICKBOXSIZE );
    // cursor coordinates (drawing coordinate space)
    X = lcPropGetFloat( hEvent, LC_PROP_EVENT_FLOAT1 );
    Y = lcPropGetFloat( hEvent, LC_PROP_EVENT_FLOAT2 );
    // get all entities that crossed by pickbox
    Lef = X - Delta;
    Bot = Y - Delta;
    Rig = X + Delta;
    Top = Y + Delta;
    nEnts = lcWndGetEntsByRect( hLcWnd, Lef, Bot, Rig, Top, true, -1 );
    if (nEnts > 0){
      for (i=0; i<nEnts; ++i){
        hEnt = lcWndGetEntity( i );
        // change entity color
        Color = lcPropGetInt( hEnt, LC_PROP_ENT_COLOR );
        if (Color == RGB(255,0,0)){
          lcPropPutInt( hEnt, LC_PROP_ENT_COLOR, RGB(255,255,0) );
        }else{
          lcPropPutInt( hEnt, LC_PROP_ENT_COLOR, RGB(255,0,0) );
        }
      }
      lcWndGetEntity( -1 );  // free memory
      lcWndRedraw( hLcWnd );
    }
    // disable LiteCAD default actions on this event
    lcEventReturnCode( 1 );
  }
}

// Variant 2
//-----------------------------------------------
void OnLBDown (HANDLE hEvent)
{
  HANDLE hLcWnd, hEnt;
  int    i, nEnts, Color;
  double Lef, Bot, Rig, Top;
  BOOL   bCtrl;

  // State of Ctrl key: 1-pressed, 0-released
  bCtrl = lcPropGetInt( hEvent, LC_PROP_EVENT_INT4 ); 
  if (bCtrl){
    // graphics window handle
    hLcWnd = lcPropGetHandle( hEvent, LC_PROP_EVENT_WND );
    // pickbox coordinates
    Lef = lcPropGetFloat( hLcWnd, LC_PROP_WND_CURLEF );
    Bot = lcPropGetFloat( hLcWnd, LC_PROP_WND_CURBOT );
    Rig = lcPropGetFloat( hLcWnd, LC_PROP_WND_CURRIG );
    Top = lcPropGetFloat( hLcWnd, LC_PROP_WND_CURTOP );
    nEnts = lcWndGetEntsByRect( hLcWnd, Lef, Bot, Rig, Top, true, -1 );
    if (nEnts > 0){
      for (i=0; i<nEnts; ++i){
        hEnt = lcWndGetEntity( i );
        // change entity color
        Color = lcPropGetInt( hEnt, LC_PROP_ENT_COLOR );
        if (Color == RGB(255,0,0)){
          lcPropPutInt( hEnt, LC_PROP_ENT_COLOR, RGB(255,255,0) );
        }else{
          lcPropPutInt( hEnt, LC_PROP_ENT_COLOR, RGB(255,0,0) );
        }
      }
      lcWndGetEntity( -1 );  // free memory
      lcWndRedraw( hLcWnd );
    }
    // disable LiteCAD default actions on this event
    lcEventReturnCode( 1 );
  }
}