Block With工具,用来提取开域with的工具,如下代码:
1 ADOQuery_Tmp.Data := fData; 2 ADOQuery_Tmp.Filtered := false; 3 ADOQuery_Tmp.Filter := '选择=1'; 4 ADOQuery_Tmp.Filtered := true;
经过使用此工具,提取ADOQuery_Tmp字符串后,代码为:
1 With ADOQuery_Tmp do 2 Begin 3 Data := fData; 4 Filtered := false; 5 Filter := '选择=1'; 6 Filtered := true; 7 End;
我们可以使用开源的CnPack代码进行扩展,打开CnWizards_D11.dpr(Dephi2007进行开发),在EditorWizard文件夹下,新增CnEditorCodeBlockWith单元
代码如下
1 {******************************************************************************} 2 { CnPack For Delphi/C++Builder } 3 { 中国人自己的开放源码第三方开发包 } 4 { (C)Copyright 2001-2012 CnPack 开发组 } 5 { ------------------------------------ } 6 { } 7 { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 } 8 { 改和重新发布这一程序。 } 9 { } 10 { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 } 11 { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 } 12 { } 13 { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 } 14 { 还没有,可访问我们的网站: } 15 { } 16 { 网站地址:http://www.cnpack.org } 17 { 电子邮件:master@cnpack.org } 18 { } 19 {******************************************************************************} 20 21 unit CnEditorCodeBlockWith; 22 23 interface 24 25 {$I CnWizards.inc} 26 27 {$IFDEF CNWIZARDS_CNEDITORWIZARD} 28 29 uses 30 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 31 StdCtrls, IniFiles, ToolsAPI, CnWizClasses, CnWizUtils, CnConsts, CnCommon, 32 CnEditorWizard, CnWizConsts, CnEditorCodeTool,ufrmBlockSet; 33 34 type 35 36 //============================================================================== 37 // 赋值交换工具类 38 //============================================================================== 39 40 { TCnEditorCodeSwap } 41 42 TCnEditorCodeBlockWith = class(TCnEditorCodeTool) 43 private 44 FBlockWith: string; 45 protected 46 function ProcessText(const Text: string): string; override; 47 function GetStyle: TCnCodeToolStyle; override; 48 public 49 function GetCaption: string; override; 50 function GetHint: string; override; 51 procedure GetEditorInfo(var Name, Author, Email: string); override; 52 53 property BlockWith: string read FBlockWith write FBlockWith; 54 constructor Create(AOwner: TCnEditorWizard); override; 55 procedure Execute; override; 56 end; 57 58 {$ENDIF CNWIZARDS_CNEDITORWIZARD} 59 60 implementation 61 62 {$IFDEF CNWIZARDS_CNEDITORWIZARD} 63 64 //============================================================================== 65 // 赋值交换工具类 66 //============================================================================== 67 68 { TCnEditorCodeSwap } 69 70 constructor TCnEditorCodeBlockWith.Create(AOwner: TCnEditorWizard); 71 begin 72 inherited; 73 ValidInSource := True; 74 BlockMustNotEmpty := True; 75 end; 76 77 procedure TCnEditorCodeBlockWith.Execute; 78 begin 79 frmBlockSet :=TfrmBlockSet.create(nil); 80 try 81 if frmBlockSet.ShowModal=mrOk then 82 FBlockWith :=frmBlockSet.Block 83 else 84 Exit; 85 finally 86 frmBlockSet.Free; 87 end; 88 if Trim(FBlockWith)='' then 89 Exit 90 else 91 begin 92 if Copy(FBlockWith,Length(FBlockWith),1)='.' then 93 FBlockWith :=Copy(FBlockWith,1,Length(FBlockWith)-1); 94 end; 95 inherited; 96 end; 97 98 function TCnEditorCodeBlockWith.GetStyle: TCnCodeToolStyle; 99 begin 100 result := csSelText; 101 end; 102 103 function TCnEditorCodeBlockWith.GetCaption: string; 104 begin 105 Result := SCnEditorCodeBlockWithMenuCaption; 106 end; 107 108 function TCnEditorCodeBlockWith.GetHint: string; 109 begin 110 Result := SCnEditorCodeBlockWithMenuHint; 111 end; 112 113 function TCnEditorCodeBlockWith.ProcessText(const Text: string): string; 114 var 115 FStrings:TStringList; 116 FCount,i:Integer; 117 FTemp:string; 118 FHead:string; 119 FEnd:string; 120 EditView: IOTAEditView; 121 StartPos:Integer; 122 BlankStr:string; 123 begin 124 FStrings := TStringList.Create; 125 try 126 FHead :=Format('With %s do',[FBlockWith]); 127 128 FStrings.Text := Text; 129 EditView :=CnOtaGetTopMostEditView; 130 StartPos := EditView.Block.StartingColumn; 131 BlankStr :=StringofChar(' ',StartPos-1); 132 FCount := FStrings.Count; 133 FHead :=FHead+#13#10+BlankStr+'Begin'; 134 FEnd :=BlankStr+'End;'; 135 for i:=0 to FCount-1 do 136 begin 137 FTemp :=FStrings[i]; 138 if i=0 then 139 FTemp :=BlankStr+' '+StringReplace(FTemp,FBlockWith+'.','',[rfIgnoreCase,rfReplaceAll]) 140 else 141 FTemp :=' '+StringReplace(FTemp,FBlockWith+'.','',[rfIgnoreCase,rfReplaceAll]); 142 FStrings[i] :=FTemp; 143 end; 144 Result :=FHead+#13#10+ FStrings.Text+FEnd; 145 finally 146 FreeAndNil(FStrings); 147 end; 148 end; 149 150 procedure TCnEditorCodeBlockWith.GetEditorInfo(var Name, Author, Email: string); 151 begin 152 Name := SCnEditorCodeBlockWithName; 153 Author := '董继超'; 154 Email := 'djcsch@163.com'; 155 end; 156 157 initialization 158 RegisterCnEditor(TCnEditorCodeBlockWith); // 注册专家 159 160 {$ENDIF CNWIZARDS_CNEDITORWIZARD} 161 end.
然后进行编译即可。
ufrmBlockSet.pas为:
1 unit ufrmBlockSet; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type 10 TfrmBlockSet = class(TForm) 11 edtBlock: TEdit; 12 Label1: TLabel; 13 Button1: TButton; 14 Button2: TButton; 15 procedure Button1Click(Sender: TObject); 16 private 17 { Private declarations } 18 public 19 { Public declarations } 20 Block:string; 21 end; 22 23 var 24 frmBlockSet: TfrmBlockSet; 25 26 implementation 27 28 {$R *.dfm} 29 30 procedure TfrmBlockSet.Button1Click(Sender: TObject); 31 begin 32 if Trim(edtBlock.Text)='' then 33 begin 34 edtBlock.SetFocus; 35 raise Exception.Create('前缀不能为空,请重新输入!'); 36 end; 37 Block :=Trim(edtBlock.Text); 38 end; 39 40 end.