BorderRemove example for C++ Builder

HRGN hRgnAll;
LEADRasterProcess * pRasterProc= NULL;


/*Create the RasterProcess object and UnlockSupport*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//...
     CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
   LEADEventSink1->Connect (pRasterProc, DIID__LEADRasterProcessEvents);
   LEADRasterView1->Raster->UnlockSupport (L_SUPPORT_DOCUMENT, AnsiToOLESTR("TestKey"));
//...
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   if(pRasterProc)
      pRasterProc->Release();
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
   int nRet;

   /*Border Remove
   This example returns a region corresponding to the borders of a bitmap
   For the example, windows regions are returned in the BorderRemove Event and combined.
   In practice it would be easier and faster to just return a LEAD region representing the changes
   The border is removed from the image*/

   //Enable the doc clean event
   pRasterProc->EnableDocCleanEvents = True;
   //Create a Nil region
   hRgnAll= CreateRectRgn(0, 0, 0, 0);
   nRet= pRasterProc->BorderRemove (LEADRasterView1->Raster,
              (BorderRemoveFlags)(BORDER_CALLBACK_REGION + BORDER_USE_VARIANCE),
                                  BORDER_ALL, 20, 9, 3);
   if (nRet == 0)
   {
      LEADRasterView1->Raster->FreeRgn ();
       LEADRasterView1->Raster->SetRgnHandle ((long)hRgnAll, 0, 0, L_RGN_SET);
      LEADRasterView1->RgnFrameType = RGNFRAME_COLOR;
   }
   //delete the Windows Rgn
   DeleteObject (hRgnAll);
}

void __fastcall TForm1:: LEADEventSink1Invoke(TObject *Sender, int DispID,
      const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params,
      Pointer varResult, Pointer ExcepInfo, Pointer ArgErr)
{
   switch (DispID)
   {
        case LEADRASTERPROCESSEVENTS_BORDERREMOVE:
      {
         int hRgn;
         int uBorderToRemove;
         float fBoundingRectLeft;
         float fBoundingRectTop;
         float fBoundingRectWidth;
         float fBoundingRectHeight;
         char * szBorder;
         char crOut[256];

         hRgn= OleVariant(Params.rgvarg[5]);
         uBorderToRemove= OleVariant(Params.rgvarg[4]);
         fBoundingRectLeft= OleVariant(Params.rgvarg[3]);
         fBoundingRectTop= OleVariant(Params.rgvarg[2]);
         fBoundingRectWidth= OleVariant(Params.rgvarg[1]);
         fBoundingRectHeight= OleVariant(Params.rgvarg[0]);
          CombineRgn ((void*)hRgnAll, (void*)hRgnAll, (void*)hRgn, (int)RGN_OR);
          switch (uBorderToRemove)
         {
              case BORDER_TOP:
               szBorder= "Top";
               break;

              case BORDER_LEFT:
               szBorder= "Left";
               break;

              case BORDER_RIGHT:
               szBorder= "Right";
               break;

              case BORDER_BOTTOM:
               szBorder= "Bottom";
               break;
         }
         sprintf(crOut,"Border - %s Bounds:%f,%f,%f,%f",
                      szBorder,
                      fBoundingRectLeft,
                      fBoundingRectTop,
                      fBoundingRectWidth,
                      fBoundingRectHeight);

         OutputDebugString(crOut);
          pRasterProc->DocCleanSuccess = SUCCESS_REMOVE;
        }
      break;
   }
}