UtilMethods.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. unit UtilMethods;
  2. interface
  3. uses
  4. Controls, ZhAPI, ActnList, ZjIDTree, DB, ZjGridDBA, ZjGrid, Windows, Messages,
  5. sdDB, VCLZip, VCLUnZip, Dialogs, Forms, ShlObj, Classes, StrUtils, Math, ADODB,
  6. IdGlobal;
  7. type
  8. TRoundMode = (rmNearest, rmUp, rmDown);
  9. TBookmarkRefreshEvent = procedure (AExpandFrame: Boolean) of object;
  10. {RoundTo}
  11. function GetCompareDigitValue(ADigit: Integer): Double;
  12. function QuantityRoundTo(AValue: Double): Double;
  13. function PriceRoundTo(AValue: Double): Double;
  14. function TotalPriceRoundTo(AValue: Double): Double;
  15. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  16. function CommonCalcRoundTo(AValue: Double): Double;
  17. {Interface Control}
  18. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  19. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  20. {DataBase Rela}
  21. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  22. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  23. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  24. {Message}
  25. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  26. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  27. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  28. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  29. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  30. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  31. {Get Common Path}
  32. function GetAppFilePath: string;
  33. function GetMyProjectsFilePath: string;
  34. function GetEmptyDataBaseFileName: string;
  35. function GetTemplateBillsFileName: string;
  36. function GetTemplateXlsFileName: string;
  37. function GetBackupFilePath: string;
  38. function GetReportTemplatePath: string;
  39. function GetAppTempPath: string;
  40. {Select & Save File Choose}
  41. function GetFilter(AExt: string): string;
  42. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  43. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  44. function SaveFile(var FileName: string; const AExt: string): Boolean;
  45. function SelectExcelFile(var AFileName: string): Boolean;
  46. function SaveExcelFile(var FileName: string): Boolean;
  47. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  48. AParentHandle: THandle = 0; AHasNewFolderBtn: Boolean = True): Boolean;
  49. function FixPathByAppPath(AFileName: string): string;
  50. function BrowseFolder(var APath: string; const ATitle: string;
  51. AParentHandle: THandle; AHasNewFolderBtn: Boolean = True): Boolean;
  52. {CheckStrings}
  53. function CheckPeg(const AStr: string): Boolean;
  54. function CheckValidPassword(APassword: string): Boolean;
  55. function ValidInteger(var AKey: Char): Boolean;
  56. function TrimInvalidChar(const AText: string): string;
  57. {MergeStrings}
  58. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  59. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  60. {CodeTransform}
  61. function B_CodeToIndexCode(const AB_Code: string): string;
  62. function ChinessNum(const ADigitNum: Integer): string;
  63. function Num2Peg(ANum: Double): string;
  64. {Compare Code}
  65. //function CompareCodeWithChar(const ACode1, ACode2: string): Integer;
  66. {Tree Relative}
  67. function CreateTree: TZjIDTree;
  68. procedure DisConnectTree(ATree: TZjIDTree);
  69. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  70. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  71. {Generate Name/Directory/Path}
  72. function GetTempFileDir: string;
  73. function GetTempFilePath: string;
  74. function GetTempName(ALength: Integer = 8): string;
  75. function GetTempFileName: string; overload;
  76. function GetTempFileName(const APath, AExt: string): string; overload;
  77. function GenerateTempFolder(AGeneratePath: string): string;
  78. function GetNewGUIDFileName(const AGeneratePath: string): string;
  79. {Progress bar Control}
  80. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  81. procedure UpdateProgress(APosition: Integer; const AHint: string);
  82. procedure DisableSysProgress;
  83. procedure EnableSysProgress;
  84. {Interface RePaint Control}
  85. procedure BeginUpdateWindow(AHandle: THandle);
  86. procedure EndUpdateWindow(AHandle: THandle);
  87. {Zip}
  88. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  89. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  90. {Copy By Stream}
  91. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  92. procedure AppendTestLog(const AFileName, ALog: string);
  93. // From here, Add by chenshilong, 2014-04-11
  94. function GetVersion(APartCount: Integer = 4): string;
  95. function ExtractFileNameWithoutExt(const AFileName: string): string;
  96. function ShortText(AText: string; AWidth: Integer): string; // 文本缩略显示
  97. function CustomWidthText(AText: string; AWidth: Integer): string;
  98. function ReplaceCharsForJson(AText: string): string; // 替换Json文本中的特殊字符
  99. function RecoverCharsFromJson(AText: string): string; // 下载得到的Json文本恢复成用户需要的文本
  100. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  101. function DeleteFolder(const FolderStr: string): Boolean;
  102. function HasExt(const AFileName: string): Boolean;
  103. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  104. function FileCount(APath, AExt: string): Integer;
  105. function ChangeExt(AFile, ANewExt: string): string;
  106. function CheckExt(AName, AExt: string): string;
  107. function TempName(ALength: Integer): string;
  108. function URLFileName(AURL: string): string;
  109. function ReplaceChars(AStr: string): string;
  110. implementation
  111. uses
  112. SysUtils, dxBar, MainFrm, ConstUnit, Globals, StdCtrls, ShellAPI;
  113. var
  114. SysProgressDisabled: Boolean;
  115. {RoundTo}
  116. function InnerRoundTo(const AValue: Extended; const ADigit: Integer; RoundMode: TRoundMode): Extended;
  117. var
  118. LFactor, Offset, HFactor: Extended;
  119. begin
  120. LFactor := IntPower(10, ADigit);
  121. HFactor := IntPower(10, abs(ADigit));
  122. Result := AValue;
  123. case RoundMode of
  124. rmNearest:
  125. begin
  126. if AValue >= 0 then
  127. Offset := 0.5
  128. else
  129. Offset := -0.5;
  130. Result := Trunc((AValue * HFactor) + Offset) * LFactor;
  131. end;
  132. rmUP:
  133. begin
  134. if Frac(AValue / LFactor) > 0 then
  135. Result := Trunc(AValue * HFactor + 1) * LFactor
  136. else
  137. Result := Trunc(AValue * HFactor) * LFactor;
  138. end;
  139. rmDown:
  140. begin
  141. Result := Trunc(AValue * HFactor) * LFactor;
  142. end;
  143. end;
  144. end;
  145. function GetCompareDigitValue(ADigit: Integer): Double;
  146. begin
  147. if ADigit > 0 then
  148. Result := IntPower(10, - ADigit - 1)
  149. else
  150. Result := 0.1;
  151. end;
  152. function QuantityRoundTo(AValue: Double): Double;
  153. begin
  154. if Assigned(OpenProjectManager.CurProjectData) then
  155. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.QuantityDigit)
  156. else
  157. Result := CommonRoundTo(AValue, iQuantityDigit);
  158. end;
  159. function PriceRoundTo(AValue: Double): Double;
  160. begin
  161. if Assigned(OpenProjectManager.CurProjectData) then
  162. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.PriceDigit)
  163. else
  164. Result := CommonRoundTo(AValue, iPriceDigit);
  165. end;
  166. function TotalPriceRoundTo(AValue: Double): Double;
  167. begin
  168. if Assigned(OpenProjectManager.CurProjectData) then
  169. Result := CommonRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.TotalPriceDigit)
  170. else
  171. Result := CommonRoundTo(AValue, iTotalPriceDigit);
  172. end;
  173. function GetTrueDigit(AValue: Double): Integer;
  174. const
  175. sFormat12 = '0.############';
  176. var
  177. sValue, sDigitValue: string;
  178. iPointPos: Integer;
  179. begin
  180. sValue := FormatFloat(sFormat12, AValue);
  181. iPointPos := Pos('.', sValue);
  182. Result := Min(0, -(Length(sValue) - iPointPos));
  183. end;
  184. function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
  185. var
  186. X: Double;
  187. P: Pointer;
  188. I: Integer;
  189. Buf: array [0..7] of Byte;
  190. begin
  191. P := @AValue;
  192. CopyMemory(@Buf[0], P, SizeOf(AValue));
  193. // 进位处理,从后往前,如果当前byte为$FF,则往前一byte进1
  194. // 注意到尾数开始的位置停止
  195. // 这里说的前后是二进制串,实际存储前后相反
  196. for I := 0 to 6 do
  197. if (I < 6) and (Buf[I] = $FF) then
  198. Buf[I] := 0
  199. else
  200. begin
  201. Buf[I] := Buf[I] + 1;
  202. Break;
  203. end;
  204. P := @X;
  205. CopyMemory(P, @Buf[0], SizeOf(X));
  206. if (ADigit < 0) and (ADigit < GetTrueDigit(AValue)) then
  207. Result := AValue
  208. else
  209. Result := InnerRoundTo(X, ADigit, RoundMode);
  210. end;
  211. function CommonCalcRoundTo(AValue: Double): Double;
  212. begin
  213. Result := CommonRoundTo(AValue, -10);
  214. end;
  215. {Interface Control}
  216. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  217. begin
  218. if Assigned(AControl) then
  219. begin
  220. if Assigned(AControl.Parent) then
  221. AControl.Parent.RemoveControl(AControl);
  222. AControl.Parent := AParent;
  223. AControl.Align := AAlign;
  224. end;
  225. end;
  226. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  227. begin
  228. if Assigned(ADxBtn) then
  229. if ADxBtn is TdxBarButton then
  230. TdxBarButton(ADxBtn).Action := AAction;
  231. end;
  232. {DataBase Rela}
  233. function GetsdDataSetNewID(ADataSet: TsdDataSet; const AIndex: string): Integer;
  234. var
  235. idx: TsdIndex;
  236. begin
  237. idx := ADataSet.FindIndex(AIndex);
  238. if idx.RecordCount > 0 then
  239. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  240. else
  241. Result := 1;
  242. end;
  243. procedure ExecuteSql(AConnection: TADOConnection; const ASql: string);
  244. var
  245. vQuery: TADOQuery;
  246. begin
  247. vQuery := TADOQuery.Create(nil);
  248. try
  249. vQuery.Connection := AConnection;
  250. vQuery.SQL.Add(ASql);
  251. vQuery.ExecSQL;
  252. finally
  253. vQuery.Free;
  254. end;
  255. end;
  256. function QueryData(AConnection: TADOConnection; const ASql: string): TADOQuery;
  257. begin
  258. Result := TADOQuery.Create(nil);
  259. Result.Connection := AConnection;
  260. Result.SQL.Clear;
  261. Result.SQL.Add(ASql);
  262. Result.Open;
  263. end;
  264. {Message}
  265. function GetValidHandle(AHandle: THandle = 0): THandle;
  266. begin
  267. if AHandle = 0 then
  268. begin
  269. if (Screen.ActiveForm <> nil) and (Screen.ActiveForm.HandleAllocated) then
  270. Result := Screen.ActiveForm.Handle
  271. else
  272. Result := Application.Handle;
  273. end
  274. else
  275. Result := AHandle;
  276. end;
  277. procedure WarningMessage(const AMsg: string; AHandle: THandle = 0);
  278. begin
  279. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('警告'), MB_OK or MB_ICONWARNING);
  280. end;
  281. procedure ErrorMessage(const AMsg: string; AHandle: THandle = 0);
  282. begin
  283. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('错误'), MB_OK or MB_ICONERROR);
  284. end;
  285. function QuestMessage(const AMsg: string; AHandle: THandle = 0): Boolean;
  286. begin
  287. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_OKCANCEL or MB_ICONQUESTION) = IDOK;
  288. end;
  289. function QuestMessageYesNo(const AMsg: string; AHandle: THandle = 0): Boolean;
  290. begin
  291. Result := MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('询问'), MB_YESNO or MB_ICONQUESTION) = IDYes;
  292. end;
  293. procedure TipMessage(const AMsg: string; AHandle: THandle = 0);
  294. begin
  295. MessageBox(GetValidHandle(AHandle), PChar(AMsg), PChar('提示'), MB_OK or MB_ICONINFORMATION);
  296. end;
  297. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  298. begin
  299. Allow := False;
  300. ErrorMessage(AMsg);
  301. end;
  302. {Get Common Path}
  303. function GetAppFilePath: string;
  304. begin
  305. Result := ExtractFilePath(ParamStr(0));
  306. end;
  307. function GetMyProjectsFilePath: string;
  308. begin
  309. Result := GetAppFilePath + '我的项目\';
  310. end;
  311. function GetEmptyDataBaseFileName: string;
  312. begin
  313. Result := GetAppFilePath + 'Data\Base.dat';
  314. end;
  315. function GetTemplateBillsFileName: string;
  316. begin
  317. Result := GetAppFilePath + 'Data\Template.xls';
  318. end;
  319. function GetTemplateXlsFileName: string;
  320. begin
  321. Result := GetAppFilePath + 'Data\Basic.xls';
  322. end;
  323. function GetBackupFilePath: string;
  324. begin
  325. Result := GetAppFilePath + 'TenderBackup\';
  326. end;
  327. function GetReportTemplatePath: string;
  328. begin
  329. Result := GetAppFilePath + 'ReportTemplates\';
  330. end;
  331. function GetAppTempPath: string;
  332. begin
  333. Result := GetAppFilePath + 'Temp\';
  334. if not DirectoryExists(Result) then
  335. CreateDirectoryInDeep(Result);
  336. end;
  337. {Select & Save File Choose}
  338. function GetFilter(AExt: string): string;
  339. begin
  340. if AExt = '' then
  341. Result := '所有文件(*.*)|*.*'
  342. else
  343. begin
  344. if AExt[1] <> '.' then
  345. AExt := '.' + AExt;
  346. Result := Format('(*%s)|*%s', [AExt, AExt]);
  347. end;
  348. end;
  349. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  350. var
  351. odFile: TOpenDialog;
  352. begin
  353. odFile := TOpenDialog.Create(nil);
  354. try
  355. odFile.Filter := GetFilter(AExt);
  356. if odFile.Execute then
  357. begin
  358. Application.ProcessMessages;
  359. AFileName := odFile.FileName;
  360. Result := True;
  361. end
  362. else
  363. Result := False;
  364. finally
  365. odFile.Free;
  366. end;
  367. end;
  368. function SelectFiles(AFiles: TStrings; const AExt: string): Boolean;
  369. var
  370. odFile: TOpenDialog;
  371. begin
  372. odFile := TOpenDialog.Create(nil);
  373. try
  374. odFile.Options := odFile.Options + [ofAllowMultiSelect];
  375. odFile.Filter := GetFilter(AExt);
  376. if odFile.Execute then
  377. begin
  378. Application.ProcessMessages;
  379. AFiles.Assign(odFile.Files);
  380. Result := True;
  381. end
  382. else
  383. Result := False;
  384. finally
  385. odFile.Free;
  386. end;
  387. end;
  388. function SaveFile(var FileName: string; const AExt: string): Boolean;
  389. var
  390. sdFile: TSaveDialog;
  391. begin
  392. sdFile := TSaveDialog.Create(nil);
  393. try
  394. sdFile.FileName := FileName;
  395. sdFile.DefaultExt := AExt;
  396. sdFile.Filter := GetFilter(AExt);
  397. Result := sdFile.Execute;
  398. if Result then
  399. FileName := sdFile.FileName;
  400. Application.ProcessMessages;
  401. finally
  402. sdFile.Free;
  403. end;
  404. end;
  405. function SelectExcelFile(var AFileName: string): Boolean;
  406. var
  407. odFile: TOpenDialog;
  408. begin
  409. odFile := TOpenDialog.Create(nil);
  410. try
  411. odFile.Filter := 'Excel文件(*.xls)|*.xls;|Excel文件(*.xlsx)|*.xlsx;';
  412. if odFile.Execute then
  413. begin
  414. Application.ProcessMessages;
  415. AFileName := odFile.FileName;
  416. Result := True;
  417. end
  418. else
  419. Result := False;
  420. finally
  421. odFile.Free;
  422. end;
  423. end;
  424. function SaveExcelFile(var FileName: string): Boolean;
  425. function CheckFileName(AFileName: string; AExt: string): string;
  426. begin
  427. if SameText(ExtractFileExt(AFileName), AExt) then
  428. Result := AFileName
  429. else
  430. Result := ExtractFilePath(AFileName) + ExtractFileName(AFileName) + AExt;
  431. end;
  432. var
  433. sdFile: TSaveDialog;
  434. begin
  435. sdFile := TSaveDialog.Create(nil);
  436. try
  437. sdFile.FileName := FileName;
  438. sdFile.Filter := 'Excel文件(*.xls)|*.xls;|Excel文件(*.xlsx)|*.xlsx;';
  439. Result := sdFile.Execute;
  440. if Result then
  441. begin
  442. case sdFile.FilterIndex of
  443. 1: FileName := CheckFileName(sdFile.FileName, '.xls');
  444. 2: FileName := CheckFileName(sdFile.FileName, '.xlsx');
  445. end;
  446. end;
  447. finally
  448. sdFile.Free;
  449. end;
  450. end;
  451. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  452. AParentHandle: THandle; AHasNewFolderBtn: Boolean): Boolean;
  453. var
  454. pID: PItemIDList;
  455. bInfo: TBrowseInfo;
  456. AHandle: THandle;
  457. PStr: array[0..1023] of Char;
  458. sPath: string;
  459. function BFCallBack(Hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  460. begin
  461. if uMsg = BFFM_INITIALIZED then
  462. begin
  463. SendMessage(Hwnd, BFFM_SETSELECTION, 1, lpData);
  464. end;
  465. Result := 0;
  466. end;
  467. begin
  468. Result := False;
  469. if AParentHandle = 0 then
  470. AHandle := Screen.ActiveForm.Handle
  471. else
  472. AHandle := AParentHandle;
  473. if ADirectory = '' then
  474. sPath := GetAppFilePath
  475. else
  476. sPath := ADirectory;
  477. with bInfo do
  478. begin
  479. hwndOwner := AParentHandle;
  480. iImage := 0;
  481. lParam := Integer(PChar(sPath));
  482. lpszTitle := PChar(ATitle);
  483. pidlRoot := nil;
  484. pszDisplayName := nil;
  485. if AHasNewFolderBtn then
  486. ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI
  487. else
  488. ulFlags := BIF_RETURNONLYFSDIRS;
  489. lpfn := @BFCallBack;
  490. end;
  491. pID := SHBrowseForFolder(bInfo);
  492. if pID <> nil then
  493. begin
  494. SHGetPathFromIDList(pID, PStr);
  495. ADirectory := StrPas(PStr);
  496. if (ADirectory <> '') and (ADirectory[Length(ADirectory)] <> '\') then
  497. ADirectory := ADirectory + '\';
  498. Result := True;
  499. end;
  500. end;
  501. function FixPathByAppPath(AFileName: string): string;
  502. begin
  503. Result := AFileName;
  504. if AnsiPos(':\', Result) = 0 then
  505. begin
  506. if (Result <> '') and (Result[1] = '\') then
  507. Delete(Result, 1, 1);
  508. Result := ExtractFilePath(Application.ExeName) + Result;
  509. end;
  510. end;
  511. var
  512. PathStr: string;
  513. function BrowseFolder(var APath: string; const ATitle: string; AParentHandle: THandle;
  514. AHasNewFolderBtn: Boolean): Boolean;
  515. function BFCallBackFunc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;
  516. begin
  517. Result := 0;
  518. case uMsg of
  519. BFFM_INITIALIZED : // 初始化设置目录。
  520. begin
  521. SendMessage(Hwnd,BFFM_SETSELECTION,-1,Integer(PathStr));
  522. end;
  523. end;
  524. end;
  525. var
  526. pID: PItemIDList;
  527. bInfo: TBrowseInfo;
  528. PStr: array[0..1023] of Char;
  529. sPath: string;
  530. Hdl: THandle;
  531. begin
  532. Result := False;
  533. PathStr := '';
  534. if AParentHandle = 0 then
  535. Hdl := Screen.ActiveForm.Handle
  536. else
  537. Hdl := AParentHandle;
  538. sPath := FixPathByAppPath(APath);
  539. if DirectoryExists(sPath) then
  540. PathStr := sPath;
  541. bInfo.hwndOwner := Hdl;
  542. bInfo.iImage := 0;
  543. bInfo.lParam := 0;
  544. bInfo.lpszTitle := PChar(ATitle);
  545. bInfo.pidlRoot := nil;
  546. bInfo.pszDisplayName := nil;
  547. if (not AHasNewFolderBtn) then
  548. bInfo.ulFlags := BIF_RETURNONLYFSDIRS
  549. else
  550. bInfo.ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI;
  551. bInfo.lpfn := @BFCallBackFunc;
  552. pID := SHBrowseForFolder(bInfo);
  553. if pID <> nil then
  554. begin
  555. SHGetPathFromIDList(pID,pStr);
  556. APath := StrPas(pStr);
  557. if (APath <> '') and (APath[Length(APath)] <> '\') then
  558. APath := APath + '\';
  559. Result := True;
  560. end;
  561. end;
  562. {CheckStrings}
  563. function CheckPeg(const AStr: string): Boolean;
  564. function GetPosition(const AName, AStr, AStrSpare: string): Integer;
  565. begin
  566. Result := Pos(AStr, AName);
  567. if Result = 0 then
  568. Result := Pos(AStrSpare, AName);
  569. end;
  570. var
  571. iPosK, iPosPlus: Integer;
  572. fNum: Double;
  573. begin
  574. Result := False;
  575. iPosK := GetPosition(AStr, 'K', 'k');
  576. iPosPlus := GetPosition(AStr, '+', '+');
  577. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  578. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum);
  579. end;
  580. function CheckValidPassword(APassword: string): Boolean;
  581. var
  582. iIndex, iLength: Integer;
  583. begin
  584. Result := True;
  585. if APassword = '' then Exit;
  586. iIndex := 1;
  587. iLength := Length(APassword);
  588. Result := (iLength >= 6) and (iLength <= 16);
  589. while Result and (iIndex < iLength) do
  590. begin
  591. Result := APassword[iIndex] in ['A'..'Z', 'a'..'z', '0'..'9'];
  592. Inc(iIndex);
  593. end;
  594. end;
  595. function ValidInteger(var AKey: Char): Boolean;
  596. begin
  597. if AKey in ['+', '-', '0'..'9', #8, #13, #35, #36,
  598. #37, #38, #39, #40, #46] then
  599. begin
  600. Result := True;
  601. end
  602. else
  603. begin
  604. AKey := #0;
  605. Result := False;
  606. end;
  607. end;
  608. function TrimInvalidChar(const AText: string): string;
  609. var
  610. i, iLength: Integer;
  611. begin
  612. Result := '';
  613. iLength := Length(AText);
  614. for i := 1 to iLength do
  615. begin
  616. if not (AText[i] in [#0]) then
  617. Result := Result + AText[i];
  618. end;
  619. end;
  620. {MergeStrings}
  621. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  622. var
  623. sgs1, sgs2: TStrings;
  624. i1, i2: Integer;
  625. bExist: Boolean;
  626. begin
  627. Result := ABGLCode1;
  628. sgs1 := TStringList.Create;
  629. sgs2 := TStringList.Create;
  630. try
  631. sgs1.Delimiter := ';';
  632. sgs2.Delimiter := ';';
  633. sgs1.DelimitedText := ABGLCode1;
  634. sgs2.DelimitedText := ABGLCode2;
  635. for i2 := 0 to sgs2.Count - 1 do
  636. begin
  637. bExist := False;
  638. for i1 := 0 to sgs1.Count - 1 do
  639. if SameText(sgs2[i2], sgs1[i1]) then
  640. begin
  641. bExist := True;
  642. Break;
  643. end;
  644. if not bExist then
  645. sgs1.Add(sgs2[i2]);
  646. end;
  647. Result := sgs1.DelimitedText;
  648. finally
  649. sgs1.Free;
  650. sgs2.Free;
  651. end;
  652. end;
  653. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  654. var
  655. sgsC1, sgsC2, sgsN1, sgsN2: TStrings;
  656. i1, i2: Integer;
  657. bExist: Boolean;
  658. begin
  659. sgsC1 := TStringList.Create;
  660. sgsC2 := TStringList.Create;
  661. sgsN1 := TStringList.Create;
  662. sgsN2 := TStringList.Create;
  663. try
  664. sgsC1.Delimiter := ';';
  665. sgsC2.Delimiter := ';';
  666. sgsC1.DelimitedText := ABGLCode1;
  667. sgsC2.DelimitedText := ABGLCode2;
  668. sgsN1.Delimiter := ';';
  669. sgsN2.Delimiter := ';';
  670. sgsN1.DelimitedText := ABGLNum1;
  671. sgsN2.DelimitedText := ABGLNum2;
  672. for i2 := 0 to sgsC2.Count - 1 do
  673. begin
  674. bExist := False;
  675. for i1 := 0 to sgsC1.Count - 1 do
  676. if SameText(sgsC2[i2], sgsC1[i1]) then
  677. begin
  678. bExist := True;
  679. Break;
  680. end;
  681. if bExist then
  682. begin
  683. sgsN1[i1] := FloatToStr(StrToFloatDef(sgsN1[i1], 0) + StrToFloatDef(sgsN2[i2], 0))
  684. end
  685. else
  686. begin
  687. sgsC1.Add(sgsC2[i2]);
  688. sgsN1.Add(sgsN2[i2]);
  689. end;
  690. end;
  691. ABGLCode1 := sgsC1.DelimitedText;
  692. ABGLNum1 := sgsN1.DelimitedText;
  693. finally
  694. sgsC1.Free;
  695. sgsC2.Free;
  696. sgsN1.Free;
  697. sgsN2.Free;
  698. end;
  699. end;
  700. function B_CodeToIndexCode(const AB_Code: string): string;
  701. var
  702. sgs: TStrings;
  703. i, iNum, iError: Integer;
  704. sError: string;
  705. begin
  706. sgs := TStringList.Create;
  707. try
  708. Result := '';
  709. sgs.Delimiter := '-';
  710. sgs.DelimitedText := AB_Code;
  711. for i := 0 to sgs.Count - 1 do
  712. begin
  713. Val(sgs[i], iNum, iError);
  714. if iError = 0 then
  715. sError := ''
  716. else
  717. sError := Copy(sgs[i], iError, Length(sgs[i])-iError+1);
  718. if iError = 0 then
  719. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError])
  720. else if iNum = 0 then
  721. Result := Result + '9999' + Format('%-4s', [sError])
  722. else
  723. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError]);
  724. end;
  725. finally
  726. sgs.Free;
  727. end;
  728. end;
  729. function ChinessNum(const ADigitNum: Integer): string;
  730. function TransChineseNum(const ANum, AZeroNum: Integer): string;
  731. begin
  732. Result := '';
  733. case ANum of
  734. 1: if AZeroNum <> 1 then Result := '一';
  735. 2: Result := '二';
  736. 3: Result := '三';
  737. 4: Result := '四';
  738. 5: Result := '五';
  739. 6: Result := '六';
  740. 7: Result := '七';
  741. 8: Result := '八';
  742. 9: Result := '九';
  743. end;
  744. if (Result = '') and ((AZeroNum <> 1) or (ANum = 0)) then Exit;
  745. case AZeroNum of
  746. 0: Result := Result;
  747. 1: Result := Result + '十';
  748. 2: Result := Result + '百';
  749. 3: Result := Result + '千';
  750. 4: Result := Result + '万';
  751. end;
  752. end;
  753. var
  754. iBai, iShi, iGe: Integer;
  755. begin
  756. Result := '';
  757. if (ADigitNum < 0) and (ADigitNum > 10000) then Exit;
  758. iBai := ADigitNum div 100;
  759. iShi := (ADigitNum mod 100) div 10;
  760. iGe := (ADigitNum mod 100) mod 10;
  761. Result := TransChineseNum(iBai, 2) + TransChineseNum(iShi, 1) + TransChineseNum(iGe, 0);
  762. end;
  763. function CheckDigit(ANum: Double): Integer;
  764. begin
  765. if abs(ANum - advRoundTo(ANum, -2)) > 0.0001 then
  766. Result := 3
  767. else if abs(ANum - advRoundTo(ANum, -1)) > 0.001 then
  768. Result := 2
  769. else if abs(ANum - AdvRoundTo(ANum, 0)) > 0.01 then
  770. Result := 1
  771. else
  772. Result := 0;
  773. end;
  774. function Num2Peg(ANum: Double): string;
  775. var
  776. fMod: Double;
  777. iK, iDigit: Integer;
  778. sDigit: string;
  779. begin
  780. iK := Trunc(ANum/1000);
  781. fMod := Frac(ANum/1000)*1000;
  782. iDigit := CheckDigit(fMod);
  783. case iDigit of
  784. 3: sDigit := FormatFloat('000.000', AdvRoundTo(fMod, -3));
  785. 2: sDigit := FormatFloat('000.00', AdvRoundTo(fMod, -2));
  786. 1: sDigit := FormatFloat('000.0', AdvRoundTo(fMod, -1));
  787. 0: sDigit := FormatFloat('000', Trunc(AdvRoundTo(fMod, 0)));
  788. end;
  789. { case iDigit of
  790. 3: Result := Format('K%d+%3.3f', [iK, ]);
  791. 2: Result := Format('K%d+%3.2f', [iK, AdvRoundTo(fMod, -2)]);
  792. 1: Result := Format('K%d+%3.1f', [iK, AdvRoundTo(fMod, -1)]);
  793. 0: Result := Format('K%d+%s', [iK, FormatFloat('000', [])]);
  794. end; }
  795. Result := Format('K%d+%s', [iK, sDigit]);
  796. end;
  797. {Tree Relative}
  798. function CreateTree: TZjIDTree;
  799. begin
  800. Result := TZjIDTree.Create;
  801. Result.KeyFieldName := 'ID';
  802. Result.ParentFieldName := 'ParentID';
  803. Result.NextSiblingFieldName := 'NextSiblingID';
  804. Result.AutoCreateKeyID := True;
  805. Result.AutoExpand := True;
  806. end;
  807. procedure DisConnectTree(ATree: TZjIDTree);
  808. begin
  809. ATree.DataSet := nil;
  810. ATree.Active := False;
  811. end;
  812. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  813. begin
  814. ATree.DataSet := ADataset;
  815. ATree.Active := True;
  816. end;
  817. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  818. var
  819. I: Integer;
  820. begin
  821. for I := 0 to AGridDBA.Columns.Count - 1 do
  822. AGrid.Cells[I + 1, 0].Text := AGridDBA.Columns[I].Title.Caption;
  823. end;
  824. {Generate Name/Path}
  825. function GetTempFileDir: string;
  826. var
  827. TempPath: string;
  828. begin
  829. TempPath := GetEnvironmentVariable('TMP');
  830. if TempPath = '' then
  831. TempPath := GetEnvironmentVariable('TEMP');
  832. if TempPath = '' then
  833. begin
  834. if not DirectoryExists(ExtractFileDir(ParamStr(0)) + '\Temp') then
  835. CreateDir(ExtractFileDir(ParamStr(0)) + '\Temp');
  836. TempPath := ExtractFileDir(ParamStr(0)) + '\Temp';
  837. end;
  838. Result := TempPath;
  839. end;
  840. function GetTempFilePath: string;
  841. begin
  842. //Result := GetTempFileDir + '\';
  843. Result := GetAppTempPath;
  844. end;
  845. function GetTempName(ALength: Integer): string;
  846. const
  847. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  848. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  849. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  850. 'W', 'X', 'Y', 'Z');
  851. var
  852. Temp: string;
  853. Num: Integer;
  854. begin
  855. Randomize;
  856. Temp := '';
  857. while Length(Temp) < ALength do
  858. begin
  859. Num := Random(37);
  860. If Num <> 36 Then
  861. Temp := Temp + CodedBuf[Num]; // 0..35
  862. end;
  863. Result := Temp;
  864. end;
  865. function GetTempFileName: string;
  866. var
  867. TempExt: string;
  868. begin
  869. Result := GetTempFilePath + GetTempName;
  870. TempExt := '.' + GetTempName(3);
  871. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  872. SameText(TempExt, '.ldb') do
  873. TempExt := '.' + GetTempName(3);
  874. Result := Result + TempExt;
  875. while FileExists(Result) do
  876. begin
  877. Result := GetTempFilePath + GetTempName;
  878. TempExt := '.' + GetTempName(3);
  879. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  880. SameText(TempExt, '.ldb') do
  881. TempExt := '.' + GetTempName(3);
  882. Result := Result + TempExt;
  883. end;
  884. end;
  885. function GetTempFileName(const APath, AExt: string): string;
  886. begin
  887. Result := APath + GetTempName + AExt;
  888. while FileExists(Result) do
  889. Result := APath + GetTempName + AExt;
  890. end;
  891. function GenerateTempFolder(AGeneratePath: string): string;
  892. begin
  893. Result := AGeneratePath + GetTempName;
  894. while DirectoryExists(Result) do
  895. Result := AGeneratePath + GetTempName;
  896. CreateDirectoryInDeep(Result);
  897. end;
  898. function GetNewGUIDFileName(const AGeneratePath: string): string;
  899. var
  900. gFile: TGUID;
  901. begin
  902. repeat
  903. CreateGUID(gFile);
  904. Result := AGeneratePath + GUIDToString(gFile);
  905. until not FileExists(Result);
  906. end;
  907. {Progress bar Control}
  908. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  909. begin
  910. if not SysProgressDisabled then
  911. UpdateProgress(APosition, AHint);
  912. end;
  913. procedure UpdateProgress(APosition: Integer; const AHint: string);
  914. begin
  915. MainForm.UpdateProgress(APosition, AHint);
  916. end;
  917. procedure DisableSysProgress;
  918. begin
  919. SysProgressDisabled := True;
  920. end;
  921. procedure EnableSysProgress;
  922. begin
  923. SysProgressDisabled := False;
  924. end;
  925. {Interface RePaint Control}
  926. procedure BeginUpdateWindow(AHandle: THandle);
  927. begin
  928. SendMessage(AHandle, WM_SETREDRAW, 0, 0);
  929. end;
  930. procedure EndUpdateWindow(AHandle: THandle);
  931. begin
  932. SendMessage(AHandle, WM_SETREDRAW, 1, 0);
  933. end;
  934. {Zip}
  935. function ZipComplete(const AFileName: string; ACheckMode: Integer): Boolean;
  936. const
  937. i4k = 4096;
  938. var
  939. iSize1, iSize2: Integer;
  940. begin
  941. iSize1 := FileSizeByName(AFileName);
  942. Sleep(300);
  943. iSize2 := FileSizeByName(AFileName);
  944. Result := (iSize1 = iSize2);
  945. if ACheckMode = 0 then
  946. Result := Result and (iSize1 > i4k);
  947. end;
  948. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  949. procedure AppendLog(const ALog: string);
  950. begin
  951. if SupportManager.ConfigInfo.IsLog then
  952. MeasureLog.AppendLogTo(ALog);
  953. end;
  954. var
  955. sTempFile: string;
  956. vZip: TVCLZip;
  957. iCount: Integer;
  958. begin
  959. AppendLog('Zip');
  960. sTempFile := GetTempFileName;
  961. vZip := TVCLZip.Create(nil);
  962. iCount := 0;
  963. try
  964. vZip.FilesList.Clear;
  965. vZip.ZipName := sTempFile;
  966. vzip.RootDir := AFileFolder;
  967. vZip.OverwriteMode := Always;
  968. //vZip.AddDirEntriesOnRecurse:=True;
  969. vZip.RelativePaths := True;
  970. //vZip.RecreateDirs := True;
  971. vZip.FilesList.Add(AFileFolder + '\*.*');
  972. vZip.Zip;
  973. AppendLog(Format('Check Zip File, FileSize - %d', [FileSizeByName(sTempFile)]));
  974. while not ZipComplete(sTempFile, ACheckMode) and (iCount < 10) do
  975. begin
  976. Sleep(200);
  977. Inc(iCount);
  978. AppendLog(Format('Check Zip File - %d, FileSize - %d', [iCount + 1, FileSizeByName(sTempFile)]));
  979. end;
  980. AppendLog('Check Zip File --> Pass');
  981. CopyFileOrFolder(sTempFile, AFileName);
  982. AppendLog('Zip --> Pass');
  983. finally
  984. vZip.Free;
  985. if FileExists(sTempFile) then
  986. DeleteFile(sTempFile);
  987. end;
  988. end;
  989. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  990. var
  991. vUnZip: TVCLZip;
  992. begin
  993. Result := True;
  994. vUnZip := TVCLZip.Create(nil);
  995. try
  996. vUnZip.FilesList.Clear;
  997. vUnZip.ZipName := AFileName;
  998. vUnZip.ReadZip;
  999. vUnZip.DestDir := AFileFolder;
  1000. vUnZip.OverwriteMode := Always;
  1001. vUnZip.RecreateDirs := True;
  1002. vUnZip.RelativePaths := True;
  1003. vUnZip.DoAll := True;
  1004. vUnZip.FilesList.Add('*.*');
  1005. try
  1006. vUnZip.UnZip;
  1007. except
  1008. Result := False;
  1009. end;
  1010. finally
  1011. vUnZip.Free;
  1012. end;
  1013. end;
  1014. {Copy By Stream}
  1015. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  1016. var
  1017. MS: TMemoryStream;
  1018. begin
  1019. MS := TMemoryStream.Create;
  1020. try
  1021. MS.LoadFromFile(ASourceFile);
  1022. if FileExists(ADestFile) then
  1023. DeleteFile(ADestFile);
  1024. MS.SaveToFile(ADestFile);
  1025. finally
  1026. MS.Free;
  1027. end;
  1028. end;
  1029. procedure AppendTestLog(const AFileName, ALog: string);
  1030. var
  1031. f: TextFile;
  1032. begin
  1033. try
  1034. if FileExists(AFileName) then
  1035. begin
  1036. AssignFile(f, AFileName);
  1037. Append(f);
  1038. Writeln(f, ALog);
  1039. end
  1040. else
  1041. begin
  1042. AssignFile(f, AFileName);
  1043. Rewrite(f);
  1044. Writeln(f, ALog);
  1045. end;
  1046. finally
  1047. CloseFile(f);
  1048. end;
  1049. end;
  1050. function GetVersion(APartCount: Integer = 4): string;
  1051. var
  1052. V1, V2, V3, V4: Word;
  1053. FInfoSize, FF: Cardinal;
  1054. FInfo: Pointer;
  1055. FFixed: PVSFIXEDFILEINFO;
  1056. begin
  1057. FInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), FF);
  1058. FInfo := AllocMem(FInfoSize);
  1059. try
  1060. GetFileVersionInfo(PChar(Application.ExeName), FF, FInfoSize, FInfo);
  1061. VerQueryValue(FInfo, '\', Pointer(FFixed), FInfoSize);
  1062. V1 := FFixed.dwFileVersionMS shr 16;
  1063. V2 := FFixed.dwFileVersionMS and $FFFF;
  1064. V3 := FFixed.dwFileVersionLS shr 16;
  1065. V4 := FFixed.dwFileVersionLS and $FFFF;
  1066. finally
  1067. Dispose(FInfo);
  1068. end;
  1069. Result := '';
  1070. case APartCount of
  1071. 1:
  1072. Result := Format('%d', [V1]);
  1073. 2:
  1074. Result := Format('%d.%d', [V1, V2]);
  1075. 3:
  1076. Result := Format('%d.%d.%d', [V1, V2, V3]);
  1077. 4:
  1078. Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4]);
  1079. end;
  1080. end;
  1081. function ExtractFileNameWithoutExt(const AFileName: string): string;
  1082. var
  1083. sFileName, Ext: string;
  1084. begin
  1085. Result := '';
  1086. if AFileName = '' then
  1087. Exit;
  1088. sFileName := AFileName;
  1089. if sFileName[Length(sFileName)] = '\' then
  1090. Delete(sFileName, Length(sFileName), 1);
  1091. Result := ExtractFileName(sFileName);
  1092. Ext := ExtractFileExt(sFileName);
  1093. Delete(Result, Length(Result) - Length(Ext) + 1, Length(Ext));
  1094. end;
  1095. function ShortText(AText: string; AWidth: Integer): string;
  1096. var vMM: TMemo;
  1097. begin
  1098. if Trim(AText) = '' then
  1099. begin
  1100. Result := '';
  1101. Exit;
  1102. end;
  1103. vMM := TMemo.Create(nil);
  1104. try
  1105. vMM.Visible := False;
  1106. vMM.WordWrap := True;
  1107. vMM.parent := Application.MainForm;
  1108. vMM.Width := AWidth - 10; // 10像素留给3个小点
  1109. vMM.Text := AText;
  1110. if vMM.Lines.Count > 1 then
  1111. Result := vMM.Lines[0] + '...'
  1112. else
  1113. Result := AText;
  1114. finally
  1115. vMM.Free;
  1116. end;
  1117. end;
  1118. function CustomWidthText(AText: string; AWidth: Integer): string;
  1119. var vMM: TMemo;
  1120. i: Integer;
  1121. begin
  1122. if Trim(AText) = '' then
  1123. begin
  1124. Result := '';
  1125. Exit;
  1126. end;
  1127. vMM := TMemo.Create(nil);
  1128. try
  1129. vMM.Visible := False;
  1130. vMM.WordWrap := True;
  1131. vMM.parent := Application.MainForm;
  1132. vMM.Width := AWidth;
  1133. vMM.Text := AText;
  1134. for i := 0 to vMM.Lines.Count - 1 do
  1135. begin
  1136. if i = 0 then
  1137. Result := vMM.Lines[i]
  1138. else
  1139. Result := Result + #10#13 + vMM.Lines[i];
  1140. end;
  1141. finally
  1142. vMM.Free;
  1143. end;
  1144. end;
  1145. function ReplaceCharsForJson(AText: string): string;
  1146. const
  1147. BefChar: array [0..7] of Char = ('{', '}', ',', ':', '"', '[', ']', '%');
  1148. AftChar: array [0..7] of string = ('{', '}', ',', ':', '"', '【', '】', '♂');
  1149. var I: Integer;
  1150. begin
  1151. AText := Trim(AText);
  1152. Result := AText;
  1153. if AText = '' then Exit;
  1154. for I := low(BefChar) to High(BefChar) do
  1155. begin
  1156. if Pos(BefChar[I], AText) > 0 then
  1157. AText := StringReplace(AText, BefChar[I], AftChar[I], [rfReplaceAll]);
  1158. end;
  1159. Result := AText;
  1160. end;
  1161. function RecoverCharsFromJson(AText: string): string;
  1162. const
  1163. BefStr: array [0..3] of string = ('♂', '\r\n', '\r', '\n');
  1164. AftStr: array [0..3] of string = ('%', '', '', '');
  1165. var I: Integer;
  1166. begin
  1167. AText := Trim(AText);
  1168. Result := AText;
  1169. if AText = '' then Exit;
  1170. for I := low(BefStr) to High(BefStr) do
  1171. begin
  1172. if Pos(BefStr[I], AText) > 0 then
  1173. AText := StringReplace(AText, BefStr[I], AftStr[I], [rfReplaceAll]);
  1174. end;
  1175. Result := AText;
  1176. end;
  1177. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  1178. var
  1179. SRec: TSearchRec;
  1180. retval: Integer;
  1181. begin
  1182. AFileList.Clear;
  1183. retval := FindFirst(APath + AExtName, faAnyFile, sRec);
  1184. try
  1185. while retval = 0 do
  1186. begin
  1187. if (SRec.Attr and faDirectory) = 0 then
  1188. AFileList.Add(Srec.Name);
  1189. retval := FindNext(SRec);
  1190. end;
  1191. finally
  1192. FindClose(SRec);
  1193. end;
  1194. end;
  1195. function DeleteFolder(const FolderStr: string): Boolean;
  1196. var
  1197. fos: SHFILEOPSTRUCT;
  1198. begin
  1199. ZeroMemory(@fos, SizeOf(fos));
  1200. fos.Wnd := HWND_DESKTOP;
  1201. fos.wFunc := FO_DELETE;
  1202. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  1203. fos.pFrom := PChar(FolderStr + #0);
  1204. Result := SHFileOperation(fos) <> 0;
  1205. end;
  1206. function HasExt(const AFileName: string): Boolean;
  1207. begin
  1208. Result := ExtractFileExt(AFileName) <> '';
  1209. end;
  1210. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  1211. var
  1212. fos: SHFILEOPSTRUCT;
  1213. begin
  1214. ZeroMemory(@fos, SizeOf(fos));
  1215. fos.Wnd := HWND_DESKTOP;
  1216. fos.wFunc := FO_COPY;
  1217. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO;
  1218. fos.pFrom := PChar(SrcFolder + #0);
  1219. fos.pTo := PChar(DstFolder + #0);
  1220. Result := SHFileOperation(fos) <> 0;
  1221. end;
  1222. function FileCount(APath, AExt: string): Integer;
  1223. var
  1224. vSR: TSearchRec;
  1225. iRetval: Integer;
  1226. vSL: TStringList;
  1227. begin
  1228. vSL := TStringList.Create;
  1229. iRetval := FindFirst(APath + '*' + AExt, faAnyFile, vSR);
  1230. try
  1231. while iRetval = 0 do
  1232. begin
  1233. if (vSR.Attr and faDirectory) = 0 then
  1234. vSL.Add(vSR.Name);
  1235. iRetval := FindNext(vSR);
  1236. end;
  1237. Result := vSL.Count;
  1238. finally
  1239. FindClose(vSR);
  1240. vSL.Free;
  1241. end;
  1242. end;
  1243. function ChangeExt(AFile, ANewExt: string): string;
  1244. var i, iPos: Integer;
  1245. begin
  1246. for i := Length(AFile) Downto 1 do
  1247. begin
  1248. if AFile[i] = '.' then
  1249. begin
  1250. iPos := i;
  1251. Break;
  1252. end;
  1253. end;
  1254. Result := Copy(AFile, 1, iPos - 1) + ANewExt;
  1255. end;
  1256. function CheckExt(AName, AExt: string): string;
  1257. var bHasExt: Boolean;
  1258. begin
  1259. bHasExt := ExtractFileExt(AName) <> '';
  1260. if not bHasExt then
  1261. Result := AName + AExt
  1262. else
  1263. begin
  1264. if (UpperCase(ExtractFileExt(AName)) <> AExt) then
  1265. Result := ChangeExt(AName, AExt)
  1266. else
  1267. Result := AName;
  1268. end;
  1269. end;
  1270. function TempName(ALength: Integer): string;
  1271. const
  1272. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  1273. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  1274. 'J', 'K', 'L', 'M', 'N',
  1275. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  1276. 'W', 'X', 'Y', 'Z');
  1277. begin
  1278. Result := '';
  1279. Randomize;
  1280. while Length(Result) < ALength do
  1281. Result := Result + CodedBuf[Random(36)];
  1282. end;
  1283. function URLFileName(AURL: string): string;
  1284. var
  1285. i: integer;
  1286. s: string;
  1287. begin
  1288. s := AURL;
  1289. i := Pos('/', s);
  1290. while i <> 0 do
  1291. begin
  1292. Delete(s, 1, i);
  1293. i := Pos('/', s);
  1294. end;
  1295. Result := s;
  1296. end;
  1297. function ReplaceChars(AStr: string): string;
  1298. var s: string;
  1299. begin
  1300. s := Trim(AStr);
  1301. s := StringReplace(s, '\r\n', #$D#$A, [rfReplaceAll]);
  1302. Result := s;
  1303. end;
  1304. end.