VCB自动保存加载输入控件内容
本文地址:http://tongxinmao.com/Article/Detail/id/475
// --------------------------------------------------------------------------- #pragma hdrstop #include "ControlCfg.h" #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <Vcl.ExtCtrls.hpp> #include <Dialogs.hpp> #include <System.JSON.hpp> // --------------------------------------------------------------------------- #pragma package(smart_init) #define CFGFILE "ccfg.txt" typedef struct{ String name; String className; String arg1; String arg2; }CONTROL_CFG; static TList *controlList = new TList(); void restoreControl(TComponent *container) { if (!controlList) return; for (int i = 0; i < controlList->Count; i++) { CONTROL_CFG *cc = (CONTROL_CFG*)controlList->Items[i]; if (cc->className == "TLabel") { TLabel *c = (TLabel*)container->FindComponent(cc->name); c->Caption = cc->arg1; } if (cc->className == "TButton") { TButton *c = (TButton*)container->FindComponent(cc->name); c->Caption = cc->arg1; } if (cc->className == "TCheckBox") { TCheckBox *c = (TCheckBox*)container->FindComponent(cc->name); c->Checked = cc->arg1 == "Checked" ? true : false; } if (cc->className == "TComboBox") { TComboBox *c = (TComboBox*)container->FindComponent(cc->name); c->ItemIndex = cc->arg1.ToInt(); } if (cc->className == "TEdit") { TEdit *c = (TEdit*)container->FindComponent(cc->name); c->Text = cc->arg1; } if (cc->className == "TMemo") { TMemo *c = (TMemo*)container->FindComponent(cc->name); c->Text = cc->arg1; } if (cc->className == "TRichEdit") { TRichEdit *c = (TRichEdit*)container->FindComponent(cc->name); c->Text = cc->arg1; } // Form1-> PrintMsg(String().sprintf(L"控件类型:%s 名称:%s 属性:%s",cc->className.c_str(),cc->name.c_str(),cc->arg1.c_str())); } } void EnumerateAll(TComponent *container) { // Enumarate its children for (int i = 0; i < container->ComponentCount; i++) { // extract the control at index i TComponent *child = container->Components[i]; if (child->InheritsFrom(__classid(TComponent))) // this check is optional { TControl *control = (TControl*)child; TClass c = control->ClassType(); String className = c->ClassName(); String name = control->Name; String property = ""; if (control->ClassNameIs("TLabel")) { TLabel *pc = dynamic_cast<TLabel*>(control); property = (pc->Caption); } if (control->ClassNameIs("TButton")) { TButton *pc = dynamic_cast<TButton*>(control); property = (pc->Caption); } if (control->ClassNameIs("TEdit")) { TEdit *pc = dynamic_cast<TEdit*>(control); property = (pc->Text); } if (control->ClassNameIs("TMemo")) { TMemo *pc = dynamic_cast<TMemo*>(control); property = (pc->Text); } if (control->ClassNameIs("TRichEdit")) { TRichEdit *pc = dynamic_cast<TRichEdit*>(control); property = (pc->Text); } if (control->ClassNameIs("TComboBox")) { TComboBox *pc = dynamic_cast<TComboBox*>(control); property = (pc->ItemIndex); } if (control->ClassNameIs("TCheckBox")) { TCheckBox *pc = dynamic_cast<TCheckBox*>(control); property = (pc->Checked ? "Checked" : "noChecked"); } CONTROL_CFG *cc = new CONTROL_CFG; cc->name = name; cc->className = className; cc->arg1 = property; controlList->Add(cc); } } } static String getExePath() { return ExtractFilePath( ParamStr ( 0 ) ); } // http://www.cppfans.com/cbknowledge/skills/files/json.asp void loadcfg(TComponent *container) { if(!FileExists(getExePath()+CFGFILE)) return; TStrings *ts = new TStringList(); ts->LoadFromFile(getExePath()+CFGFILE); controlList->Clear(); UnicodeString jsonstr = ts->GetText(); TJSONValue *lpJson = System::Json::TJSONObject::ParseJSONValue(jsonstr); if(lpJson==NULL)return; TJSONArray* lpArray = dynamic_cast<TJSONArray*>(lpJson); for (int i = 0; i < lpArray->Count; i++) { CONTROL_CFG *cc = new CONTROL_CFG; TJSONValue *lpItem = lpArray->Items[i]; TJSONObject *lpJSONObject = dynamic_cast<TJSONObject*>(lpItem); // lpItem 可能是结构体、数组、或者简单的值 TJSONValue *lpValue = lpJSONObject->Values[L"name"]; if (lpValue) // 如果有 strval 这个关键字 { // 只要不是 TJSONObject 和 TJSONArray 就能用 Value() 获取到值 cc->name = lpValue->Value(); } lpValue = lpJSONObject->Values[L"cname"]; if (lpValue) { cc->className = lpValue->Value(); } lpValue = lpJSONObject->Values[L"arg1"]; if (lpValue) { cc->arg1 = lpValue->Value(); } lpValue = lpJSONObject->Values[L"arg2"]; if (lpValue) { cc->arg2 = lpValue->Value(); } controlList->Add(cc); } delete lpJson; restoreControl(container); } void savecfg(TComponent *container) { EnumerateAll(container); TJSONArray *lpStrings = new TJSONArray; for (int i = 0; i < controlList->Count; i++) { CONTROL_CFG *cc = (CONTROL_CFG*)controlList->Items[i]; TJSONObject *lpJson = new TJSONObject; lpJson->AddPair(L"name", cc->name); lpJson->AddPair(L"cname", cc->className); lpJson->AddPair(L"arg1", cc->arg1); lpJson->AddPair(L"arg2", cc->arg2); lpStrings->Add(lpJson); } String str = lpStrings->ToString(); TStrings *ts = new TStringList(); ts->SetText(str.c_str()); ts->SaveToFile(CFGFILE); delete lpStrings; } /* void PrintControl(TControl *control) { if(control==NULL || control==Form1) { controlList->Clear(); Form1-> PrintMsg("bianli form:"+ String(Form1-> ControlCount)); for(int i=0;i< Form1-> ControlCount;i++) { // Form1-> PrintMsg(Form1-> Controls[i]->Name); PrintControl(Form1-> Controls[i]); } return; } TClass c = control->ClassType(); String className = c->ClassName(); String name = control->Name; String property=""; if(control-> ClassNameIs("TEdit")) { TEdit *pc = dynamic_cast<TEdit*>(control); property=(pc->Text); } if(control-> ClassNameIs("TMemo")) { TMemo *pc = dynamic_cast<TMemo*>(control); property= (pc->Text); } if(control-> ClassNameIs("TComboBox")) { TComboBox *pc = dynamic_cast<TComboBox*>(control); property= (pc->ItemIndex); } if( control-> ClassNameIs("TPanel")) { TPanel *pPanel = dynamic_cast<TPanel*>(control); for(int i=1;i< pPanel-> ControlCount;i++) { PrintControl(pPanel-> Controls[i]); } } if( control-> ClassNameIs("TGroupBox")) { TGroupBox *pb = dynamic_cast<TGroupBox*>(control); for(int i=1;i< pb-> ControlCount;i++) { PrintControl(pb-> Controls[i]); } } if( control-> ClassNameIs("TPageControl")) { TPageControl *pb = dynamic_cast<TPageControl*>(control); for(int i=1;i< pb-> ControlCount;i++) { PrintControl(pb-> Controls[i]); } } if( control-> ClassNameIs("TTabSheet")) { TTabSheet *pb = dynamic_cast<TTabSheet*>(control); for(int i=1;i< pb-> ControlCount;i++) { PrintControl(pb-> Controls[i]); } } CONTROL_CFG *cc = new CONTROL_CFG; cc->name = name; cc->className = className; cc->arg1 = property; controlList->Add(cc); } */ /* for(iFormIdx=0; iFormIdx<Screen->FormCount; iFormIdx++) { TForm *pForm = Screen->Forms[iFormIdx]; for( int i=0;i<Application->ComponentCount;i++) { ((TEdit *)GroupBox1->FindChildControl("Edit" + IntToStr(i)))->Text = "我被改了"; ((TEdit *)FindComponent("Edit" + IntToStr(i)))->Text = "我被改了"; Components TCompont* 目前Form上指向所有控件的数组 Controls TControl* 目前Form上指向某一子区域上所有控件的数组 */
上一篇:使用BluetoothHidDevice将安卓手机同时模拟成鼠标和键盘
下一篇:PHP RSSI 距离计算