Delphi 动态创建组件
procedure TForm1.CreatePythonComponents; begin // Destroy P4D components FreeAndNil(PythonEngine1); FreeAndNil(PythonType1); FreeAndNil(PythonModule1); { TPythonEngine } PythonEngine1 := TPythonEngine.Create(Self); PyVersions[cbPyVersions.ItemIndex].AssignTo(PythonEngine1); PythonEngine1.IO := PythonGUIInputOutput1; { TPythonModule } PythonModule1 := TPythonModule.Create(Self); PythonModule1.Name := 'PythonModule1'; PythonModule1.Engine := PythonEngine1; PythonModule1.ModuleName := 'spam'; with PythonModule1.Errors.Add do begin Name := 'PointError'; ErrorType := etClass; end; with PythonModule1.Errors.Add do begin Name := 'EBadPoint'; ErrorType := etClass; ParentClass.Name := 'PointError'; end; { TPythonType } PythonType1 := TPythonType.Create(Self); PythonType1.Name := 'PythonType1'; PythonType1.Engine := PythonEngine1; PythonType1.OnInitialization := PythonType1Initialization; PythonType1.TypeName := 'Point'; PythonType1.Prefix := 'Create'; PythonType1.Services.Basic := [bsRepr,bsStr,bsGetAttrO,bsSetAttrO]; PythonType1.TypeFlags := [tpfBaseType]; PythonType1.Module := PythonModule1; PythonEngine1.LoadDll; end;
procedure TfrmMain.CreateBrowser(url: string); begin FreeAndNil(webBrowserMain); webBrowserMain := TWebBrowser.Create(frmMain); TWinControl(webBrowserMain).Name := 'WebBrowserMain'; TWinControl(webBrowserMain).Parent := frmMain; webBrowserMain.Silent := true; //don't show JS errors webBrowserMain.Visible:= true; webBrowserMain.Align := alClient; ... end;