フリーソフト

« Quicktext - All tags | トップページ | [Windows live] Windows live writerで投稿した画像の上書き防止 »

[C++Builder] オートコンプリート IAutoComplete2の使い方

ソースをそのままのせます。

ヘッダーファイル

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <ShlObj.h> #include <ComObj.hpp> #include <ShlGuid.h> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE で管理されるコンポーネント   TMemo *Memo1;   TEdit *Edit1;   void __fastcall FormCreate(TObject *Sender); private: // ユーザー宣言   IAutoComplete2 *FAutoComplete;   IUnknown *FStrings; public: // ユーザー宣言   __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- class TEnumString: public IEnumString { private:   TStringList *FStrings;   ULONG FIndex;   ULONG FRefCount; public:   //IUnknown implementation   STDMETHODIMP_(ULONG) AddRef();   STDMETHODIMP_(ULONG) Release();   STDMETHODIMP QueryInterface(REFIID riid, void** ppvObject); protected:   HRESULT STDMETHODCALLTYPE Next(ULONG celt,LPOLESTR *rgelt,ULONG *pceltFetched);   HRESULT STDMETHODCALLTYPE Skip(ULONG celt);   HRESULT STDMETHODCALLTYPE Reset(void);   HRESULT STDMETHODCALLTYPE Clone(IEnumString **ppenum); public:   __fastcall TEnumString(void);   __fastcall TEnumString(TStrings *Strings);   __fastcall ~TEnumString(void);   void __fastcall Clear(void);   void __fastcall Add(UnicodeString S);   void __fastcall Assign(TStrings *Source); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif

ソースファイル
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; /* TEnumString */ //--------------------------------------------------------------------------- __fastcall TEnumString::TEnumString(void)   :IEnumString() {   FStrings=new TStringList;   FStrings->Sorted=true;   FStrings->Duplicates=dupIgnore; } //--------------------------------------------------------------------------- __fastcall TEnumString::TEnumString(TStrings *Strings)   :IEnumString() {   FStrings=new TStringList;   FStrings->Sorted=true;   FStrings->Duplicates=dupIgnore;   this->Assign(Strings); } //--------------------------------------------------------------------------- __fastcall TEnumString::~TEnumString(void) {   delete FStrings; } //--------------------------------------------------------------------------- STDMETHODIMP_(ULONG) TEnumString::AddRef() {   return ::InterlockedIncrement(reinterpret_cast<LONG*>(&FRefCount)); } //--------------------------------------------------------------------------- STDMETHODIMP_(ULONG) TEnumString::Release() {   ULONG nCount=(ULONG)::InterlockedDecrement(reinterpret_cast<LONG*>(&FRefCount)); if (nCount==0) delete this;   return nCount; } //--------------------------------------------------------------------------- STDMETHODIMP TEnumString::QueryInterface(REFIID riid, void** ppvObject) {   HRESULT hr=E_NOINTERFACE;   if (ppvObject!=NULL) {     *ppvObject=NULL;     if (IID_IUnknown==riid)       *ppvObject=static_cast<IUnknown*>(this);     if (IID_IEnumString==riid)       *ppvObject=static_cast<IEnumString*>(this);     if (*ppvObject!=NULL) {       hr = S_OK;       ((LPUNKNOWN)*ppvObject)->AddRef();     }   } else {     hr=E_POINTER;   }   return hr; } //--------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE TEnumString::Next(ULONG celt,LPOLESTR *rgelt,ULONG *pceltFetched) {   HRESULT hr=S_FALSE;   if (!celt) celt=1;   ULONG i=0;   for (i = 0;i<celt;i++) {     if (FIndex>(ULONG)FStrings->Count-1) break;     UnicodeString Str=FStrings->Strings[FIndex];     rgelt[i] = (LPWSTR)::CoTaskMemAlloc((ULONG) sizeof(WCHAR)*(Str.Length()+1));     lstrcpy(rgelt[i], Str.w_str());     if (pceltFetched) *pceltFetched++;     FIndex++;   }   if (i == celt) hr=S_OK;   return hr; } //--------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE TEnumString::Skip(ULONG celt) {   HRESULT hr=S_FALSE;   if ((FIndex+celt)<=(ULONG)FStrings->Count) {     FIndex+=celt;     hr=S_OK;   } else {     FIndex=FStrings->Count;     hr=S_FALSE;   }   return hr; } //--------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE TEnumString::Reset(void) {   FIndex=0;   return S_OK; } //--------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE TEnumString::Clone(IEnumString **ppenum) {   HRESULT Result=S_OK;   try {     *ppenum=new TEnumString(FStrings);     Result=S_OK;   } catch(...) {     Result=E_UNEXPECTED;   }   return Result; } //--------------------------------------------------------------------------- void __fastcall TEnumString::Clear(void) {   FStrings->Clear(); } //--------------------------------------------------------------------------- void __fastcall TEnumString::Add(UnicodeString S) {   FStrings->Add(S); } //--------------------------------------------------------------------------- void __fastcall TEnumString::Assign(TStrings *Source) {   FStrings->Assign(Source); } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner)   : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) {   CoInitialize(NULL);   OleCheck(CoCreateInstance(CLSID_AutoComplete,NULL,CLSCTX_INPROC_SERVER,IID_IAutoComplete2,(void **)(&FAutoComplete)));   FStrings=new TEnumString(Memo1->Lines);   OleCheck(FAutoComplete->SetOptions(ACO_AUTOSUGGEST|ACO_UPDOWNKEYDROPSLIST));   OleCheck(FAutoComplete->Init(Edit1->Handle,FStrings,NULL,NULL)); } //---------------------------------------------------------------------------

« Quicktext - All tags | トップページ | [Windows live] Windows live writerで投稿した画像の上書き防止 »

C++Builder」カテゴリの記事

システム関連」カテゴリの記事