Code sample Home

Get one entity by LC_EVENT_LBDOWN event

When user presses left mouse button and <Ctrl> key, we try to retrieve an entity at cursor position. If any entity have been found, we change its color to red/yellow.

...
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;
  BOOL   bCtrl;
  int    Xwin, Ywin;
  int    Color;

  // 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 );
    // cursor coordinates (window coordinate space, pixels)
    Xwin = lcPropGetInt( hEvent, LC_PROP_EVENT_INT1 );
    Ywin = lcPropGetInt( hEvent, LC_PROP_EVENT_INT2 );
    // get entity under cursor
    hEnt = lcWndGetEntByPoint( hLcWnd, Xwin, Ywin );
    if (hEnt){
      // 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) );
      }
      lcWndRedraw( hLcWnd );
    }
    // disable LiteCAD default actions on LBDown event
    lcEventReturnCode( 1 );
  }
}

// Variant 2
//-----------------------------------------------
void OnLBDown (HANDLE hEvent)
{
  HANDLE hLcWnd, hEnt;
  BOOL   bCtrl;
  double X, Y, Delta;
  int    Color;

  // 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 entity near X, Y
    hEnt = lcWndGetEntByPoint2( hLcWnd, X, Y, Delta );
    if (hEnt){
      // 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) );
      }
      lcWndRedraw( hLcWnd );
    }
    // disable LiteCAD default actions on LBDown event
    lcEventReturnCode( 1 );
  }
}
Used functions:

lcWndGetEntByPoint
lcWndGetEntByPoint2
lcWndRedraw
lcEventReturnCode

lcPropGetFloat
lcPropGetHandle
lcPropGetInt
lcPropPutInt