ControlSavable 2015/2/21 | Visual Studio 2012 Pro用
VC++の練習をしています。
今回、テキストボックスのテキストやコンボボックスのリスト項目を全てSQLiteのデータベースにセーブ・ロードできるようにコントロールをカスタマイズしてみました。
https://ringing-web.com/wp-content/uploads/2015/02/ControlSavable_20150221_001.zip
↑ControlSavableのソースです。(VS2012Proのプロジェクトですが、ソースとライブラリだけ抜き出されてください。)
上記UMLに沿って、VC++で作ってみました。
あ、使っているSQLiteライブラリはKompexというものでGPLですので、ControlSavableもソースを公開しています。
以下、ソース抜粋です。
class AFX_EXT_CLASS IContolSavable { public: IContolSavable(void); ~IContolSavable(void); virtual void Save( CControlSettingsDAOContext& con ) = 0; virtual void Load( CControlSettingsDAOContext& con ) = 0; virtual void Save( CControlSettingsDAOContext& con, const CString& header ) = 0; virtual void Load( CControlSettingsDAOContext& con, const CString& header ) = 0; virtual void GetExt(CString& Ext) { Ext = _T("_EXT_"); } }; class AFX_EXT_CLASS IHasName { public: IHasName(void); ~IHasName(void); virtual void GetName(CString& Name) = 0; }; class AFX_EXT_CLASS IHasTextCollection { public: IHasTextCollection(void); ~IHasTextCollection(void); virtual void GetTextCollection(CArray& arrayTextCollection ) = 0; virtual void SetTextCollection(const CArray& arrayTextCollection ) = 0; }; class AFX_EXT_CLASS CControlSavableEnumAdapter : public IContolSavable { public: CControlSavableEnumAdapter(void); ~CControlSavableEnumAdapter(void); static void GetChildControlSavable( CWnd* pCWnd, CTypedPtrArray &objPtrArray ); private: static BOOL CALLBACK EnumChildProc( HWND hWnd, LPARAM lParam ); }; BOOL CALLBACK CControlSavableEnumAdapter::EnumChildProc( HWND hWnd, LPARAM lParam ) { CWnd* pCWnd = CWnd::FromHandle( hWnd ); CTypedPtrArray* pArray = (CTypedPtrArray*)lParam; if( pArray == NULL ) return TRUE; IContolSavable* pWndSavable = dynamic_cast(pCWnd); if( pWndSavable == NULL ) return TRUE; pArray->Add( pWndSavable ); return TRUE; } void CControlSavableEnumAdapter::GetChildControlSavable( CWnd* pCWnd, CTypedPtrArray &objPtrArray ) { if( &objPtrArray != NULL ) { EnumChildWindows(pCWnd->GetSafeHwnd(), CControlSavableEnumAdapter::EnumChildProc, (LPARAM) &objPtrArray ); } } class AFX_EXT_CLASS CControlSaveAdapter : CControlSavableEnumAdapter, IHasName { public: CControlSaveAdapter(void); ~CControlSaveAdapter(void); virtual void Save( CControlSettingsDAOContext& con ); virtual void Load( CControlSettingsDAOContext& con ); virtual void Save( CControlSettingsDAOContext& con, const CString& header ); virtual void Load( CControlSettingsDAOContext& con, const CString& header ); virtual void MakeKey( CString& key ); }; class AFX_EXT_CLASS CCollectionControlSaveAdapter : public CControlSaveAdapter, public IHasTextCollection { public: CCollectionControlSaveAdapter(void); ~CCollectionControlSaveAdapter(void); virtual void Save( CControlSettingsDAOContext& con ); virtual void Load( CControlSettingsDAOContext& con ); virtual void Save( CControlSettingsDAOContext& con, const CString& header ); virtual void Load( CControlSettingsDAOContext& con, const CString& header ); }; class AFX_EXT_CLASS CSavableButton : public CButton, public CControlSaveAdapter { public: CSavableButton(void); ~CSavableButton(void); virtual void GetName( CString& name ); }; class AFX_EXT_CLASS CSavableTextBox : public CEdit, public CControlSaveAdapter { public: CSavableTextBox(void); ~CSavableTextBox(void); virtual void GetName( CString& name ); }; class AFX_EXT_CLASS CSavableComboBox : public CComboBox, public CCollectionControlSaveAdapter { public: CSavableComboBox(void); ~CSavableComboBox(void); virtual void GetName( CString& name ); virtual void GetTextCollection(CArray& arrayTextCollection ); virtual void SetTextCollection(const CArray& arrayTextCollection); }; class AFX_EXT_CLASS CSavableListBox : public CListBox, public CCollectionControlSaveAdapter { public: CSavableListBox(void); ~CSavableListBox(void); virtual void GetName( CString& name ); virtual void GetTextCollection(CArray& arrayTextCollection ); virtual void SetTextCollection(const CArray& arrayTextCollection); };