-->

example “virtual treeview” IterateSubtree in C++Bu

2019-09-21 08:50发布

问题:

I need an example how to use "virtual treeview" IterateSubtree in C++ Embarcadero Xe1-7.

I have the problem with this code:

void __fastcall TMyForm::BuSearchClick(TObject *)
{
  MyTreeView->IterateSubtree(NULL, SearchDataSomeId, (void*)&PNodeData, TVirtualNodeStates(), false, false);
}

void __fastcall TMyForm::SearchDataSomeId(TBaseVirtualTree*, PVirtualNode Node, void *Data, bool &Abort)
{
}

The compiler gives the following error:

[bcc32 Error] MyFile.cpp(363): E2034 Cannot convert 'void (_fastcall * (_closure )(TBaseVirtualTree *,TVirtualNode *,void *,bool &))(TBaseVirtualTree *,TVirtualNode *,void *,bool &)' to '_di_TVTGetNodeProc'

回答1:

You're trying to use what Delphi/C++Builder calls a method pointer or __closure. Virtual Treeview expects an anonymous method. See here for more details.

I think that creating an anonymous method in C++Builder involves subclassing from TProc and implementing the Invoke method, but it seems to be very poorly documented.

If subclassing TProc doesn't work or is too hard to figure out, I can think of a couple of options:

  • Hack the Virtual Treeview source to create an IterateSubtree overload taking a method pointer.
  • Ask a new question on Stack Overflow in hopes that someone else knows how to create a Delphi anonymous method in C++Builder.


回答2:

thanks very much`

i have found the solution

typedef void (*TIterateSubtreeCallBack)(TBaseVirtualTree*, PVirtualNode Node, void *_Data, bool &Abort);   

class TMyVTGetNodeProcRef : public TCppInterfacedObject<TVTGetNodeProc>    
{
private:   
  TIterateSubtreeCallBack callback;   
public:   
  TMyVTGetNodeProcRef(TIterateSubtreeCallBack _callback) : callback(_callback) {}    
  INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);   

  void __fastcall Invoke(TBaseVirtualTree* Sender, TVirtualNode *Node, void *Data, bool &Abort)   
  {   
    return callback(Sender, Node, Data, Abort);   
  }   
};   

void __fastcall TMyForm::BuSearchClick(TObject *)   
{   
  Node= MyTreeView->IterateSubtree(NULL, new TMyVTGetNodeProcRef(SearchDataId), (void*)&PNodeData, TVirtualNodeStates(), false, false);   
}   

void TMyForm::SearchDataId(TBaseVirtualTree*Tr, PVirtualNode Node, void *_Data, bool &Abort)   
{   
 put my code ...   
}   
//---------------------------------------------------------------------------