UtilMethods.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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 <= -6) 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. function CheckPlusPeg(const AStr: string): Boolean;
  571. var
  572. iPosK, iPosPlus: Integer;
  573. fNum: Double;
  574. begin
  575. Result := False;
  576. iPosK := GetPosition(AStr, 'K', 'k');
  577. iPosPlus := GetPosition(AStr, '+', '+');
  578. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  579. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum) and (fNum > 0);
  580. end;
  581. // K0-134.5 pass; k-2-134.5 fail;
  582. function CheckMinusPeg(const AStr: string): Boolean;
  583. var
  584. iPosK, iPosMinus: Integer;
  585. fNum: Double;
  586. begin
  587. Result := False;
  588. iPosK := GetPosition(AStr, 'K', 'k');
  589. iPosMinus := GetPosition(AStr, '-', '-');
  590. if (iPosK = 0) or (iPosMinus = 0) or (iPosMinus < iPosK) then Exit;
  591. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosMinus - iPosK - 1), fNum) and (fNum < 0);
  592. end;
  593. begin
  594. Result := CheckPlusPeg(AStr) or CheckMinusPeg(AStr);
  595. end;
  596. function CheckValidPassword(APassword: string): Boolean;
  597. var
  598. iIndex, iLength: Integer;
  599. begin
  600. Result := True;
  601. if APassword = '' then Exit;
  602. iIndex := 1;
  603. iLength := Length(APassword);
  604. Result := (iLength >= 6) and (iLength <= 16);
  605. while Result and (iIndex < iLength) do
  606. begin
  607. Result := APassword[iIndex] in ['A'..'Z', 'a'..'z', '0'..'9'];
  608. Inc(iIndex);
  609. end;
  610. end;
  611. function ValidInteger(var AKey: Char): Boolean;
  612. begin
  613. if AKey in ['+', '-', '0'..'9', #8, #13, #35, #36,
  614. #37, #38, #39, #40, #46] then
  615. begin
  616. Result := True;
  617. end
  618. else
  619. begin
  620. AKey := #0;
  621. Result := False;
  622. end;
  623. end;
  624. function TrimInvalidChar(const AText: string): string;
  625. var
  626. i, iLength: Integer;
  627. begin
  628. Result := '';
  629. iLength := Length(AText);
  630. for i := 1 to iLength do
  631. begin
  632. if not (AText[i] in [#0]) then
  633. Result := Result + AText[i];
  634. end;
  635. end;
  636. {MergeStrings}
  637. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  638. var
  639. sgs1, sgs2: TStrings;
  640. i1, i2: Integer;
  641. bExist: Boolean;
  642. begin
  643. Result := ABGLCode1;
  644. sgs1 := TStringList.Create;
  645. sgs2 := TStringList.Create;
  646. try
  647. sgs1.Delimiter := ';';
  648. sgs2.Delimiter := ';';
  649. sgs1.DelimitedText := ABGLCode1;
  650. sgs2.DelimitedText := ABGLCode2;
  651. for i2 := 0 to sgs2.Count - 1 do
  652. begin
  653. bExist := False;
  654. for i1 := 0 to sgs1.Count - 1 do
  655. if SameText(sgs2[i2], sgs1[i1]) then
  656. begin
  657. bExist := True;
  658. Break;
  659. end;
  660. if not bExist then
  661. sgs1.Add(sgs2[i2]);
  662. end;
  663. Result := sgs1.DelimitedText;
  664. finally
  665. sgs1.Free;
  666. sgs2.Free;
  667. end;
  668. end;
  669. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  670. var
  671. sgsC1, sgsC2, sgsN1, sgsN2: TStrings;
  672. i1, i2: Integer;
  673. bExist: Boolean;
  674. begin
  675. sgsC1 := TStringList.Create;
  676. sgsC2 := TStringList.Create;
  677. sgsN1 := TStringList.Create;
  678. sgsN2 := TStringList.Create;
  679. try
  680. sgsC1.Delimiter := ';';
  681. sgsC2.Delimiter := ';';
  682. sgsC1.DelimitedText := ABGLCode1;
  683. sgsC2.DelimitedText := ABGLCode2;
  684. sgsN1.Delimiter := ';';
  685. sgsN2.Delimiter := ';';
  686. sgsN1.DelimitedText := ABGLNum1;
  687. sgsN2.DelimitedText := ABGLNum2;
  688. for i2 := 0 to sgsC2.Count - 1 do
  689. begin
  690. bExist := False;
  691. for i1 := 0 to sgsC1.Count - 1 do
  692. if SameText(sgsC2[i2], sgsC1[i1]) then
  693. begin
  694. bExist := True;
  695. Break;
  696. end;
  697. if bExist then
  698. begin
  699. sgsN1[i1] := FloatToStr(StrToFloatDef(sgsN1[i1], 0) + StrToFloatDef(sgsN2[i2], 0))
  700. end
  701. else
  702. begin
  703. sgsC1.Add(sgsC2[i2]);
  704. sgsN1.Add(sgsN2[i2]);
  705. end;
  706. end;
  707. ABGLCode1 := sgsC1.DelimitedText;
  708. ABGLNum1 := sgsN1.DelimitedText;
  709. finally
  710. sgsC1.Free;
  711. sgsC2.Free;
  712. sgsN1.Free;
  713. sgsN2.Free;
  714. end;
  715. end;
  716. function B_CodeToIndexCode(const AB_Code: string): string;
  717. var
  718. sgs: TStrings;
  719. i, iNum, iError: Integer;
  720. sError: string;
  721. begin
  722. sgs := TStringList.Create;
  723. try
  724. Result := '';
  725. sgs.Delimiter := '-';
  726. sgs.DelimitedText := AB_Code;
  727. for i := 0 to sgs.Count - 1 do
  728. begin
  729. Val(sgs[i], iNum, iError);
  730. if iError = 0 then
  731. sError := ''
  732. else
  733. sError := Copy(sgs[i], iError, Length(sgs[i])-iError+1);
  734. if iError = 0 then
  735. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError])
  736. else if iNum = 0 then
  737. Result := Result + '9999' + Format('%-4s', [sError])
  738. else
  739. Result := Result + Format('%4d', [iNum]) + Format('%-4s', [sError]);
  740. end;
  741. finally
  742. sgs.Free;
  743. end;
  744. end;
  745. function ChinessNum(const ADigitNum: Integer): string;
  746. function TransChineseNum(const ANum, AZeroNum: Integer): string;
  747. begin
  748. Result := '';
  749. case ANum of
  750. 1: if AZeroNum <> 1 then Result := '一';
  751. 2: Result := '二';
  752. 3: Result := '三';
  753. 4: Result := '四';
  754. 5: Result := '五';
  755. 6: Result := '六';
  756. 7: Result := '七';
  757. 8: Result := '八';
  758. 9: Result := '九';
  759. end;
  760. if (Result = '') and ((AZeroNum <> 1) or (ANum = 0)) then Exit;
  761. case AZeroNum of
  762. 0: Result := Result;
  763. 1: Result := Result + '十';
  764. 2: Result := Result + '百';
  765. 3: Result := Result + '千';
  766. 4: Result := Result + '万';
  767. end;
  768. end;
  769. var
  770. iBai, iShi, iGe: Integer;
  771. begin
  772. Result := '';
  773. if (ADigitNum < 0) and (ADigitNum > 10000) then Exit;
  774. iBai := ADigitNum div 100;
  775. iShi := (ADigitNum mod 100) div 10;
  776. iGe := (ADigitNum mod 100) mod 10;
  777. Result := TransChineseNum(iBai, 2) + TransChineseNum(iShi, 1) + TransChineseNum(iGe, 0);
  778. end;
  779. function CheckDigit(ANum: Double): Integer;
  780. begin
  781. if abs(ANum - advRoundTo(ANum, -2)) > 0.0001 then
  782. Result := 3
  783. else if abs(ANum - advRoundTo(ANum, -1)) > 0.001 then
  784. Result := 2
  785. else if abs(ANum - AdvRoundTo(ANum, 0)) > 0.01 then
  786. Result := 1
  787. else
  788. Result := 0;
  789. end;
  790. function Num2Peg(ANum: Double): string;
  791. var
  792. fMod: Double;
  793. iK, iDigit: Integer;
  794. sDigit: string;
  795. begin
  796. iK := Trunc(ANum/1000);
  797. fMod := Frac(ANum/1000)*1000;
  798. iDigit := CheckDigit(fMod);
  799. case iDigit of
  800. 3: sDigit := FormatFloat('000.000', AdvRoundTo(fMod, -3));
  801. 2: sDigit := FormatFloat('000.00', AdvRoundTo(fMod, -2));
  802. 1: sDigit := FormatFloat('000.0', AdvRoundTo(fMod, -1));
  803. 0: sDigit := FormatFloat('000', Trunc(AdvRoundTo(fMod, 0)));
  804. end;
  805. { case iDigit of
  806. 3: Result := Format('K%d+%3.3f', [iK, ]);
  807. 2: Result := Format('K%d+%3.2f', [iK, AdvRoundTo(fMod, -2)]);
  808. 1: Result := Format('K%d+%3.1f', [iK, AdvRoundTo(fMod, -1)]);
  809. 0: Result := Format('K%d+%s', [iK, FormatFloat('000', [])]);
  810. end; }
  811. Result := Format('K%d+%s', [iK, sDigit]);
  812. end;
  813. {Tree Relative}
  814. function CreateTree: TZjIDTree;
  815. begin
  816. Result := TZjIDTree.Create;
  817. Result.KeyFieldName := 'ID';
  818. Result.ParentFieldName := 'ParentID';
  819. Result.NextSiblingFieldName := 'NextSiblingID';
  820. Result.AutoCreateKeyID := True;
  821. Result.AutoExpand := True;
  822. end;
  823. procedure DisConnectTree(ATree: TZjIDTree);
  824. begin
  825. ATree.DataSet := nil;
  826. ATree.Active := False;
  827. end;
  828. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  829. begin
  830. ATree.DataSet := ADataset;
  831. ATree.Active := True;
  832. end;
  833. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  834. var
  835. I: Integer;
  836. begin
  837. for I := 0 to AGridDBA.Columns.Count - 1 do
  838. AGrid.Cells[I + 1, 0].Text := AGridDBA.Columns[I].Title.Caption;
  839. end;
  840. {Generate Name/Path}
  841. function GetTempFileDir: string;
  842. var
  843. TempPath: string;
  844. begin
  845. TempPath := GetEnvironmentVariable('TMP');
  846. if TempPath = '' then
  847. TempPath := GetEnvironmentVariable('TEMP');
  848. if TempPath = '' then
  849. begin
  850. if not DirectoryExists(ExtractFileDir(ParamStr(0)) + '\Temp') then
  851. CreateDir(ExtractFileDir(ParamStr(0)) + '\Temp');
  852. TempPath := ExtractFileDir(ParamStr(0)) + '\Temp';
  853. end;
  854. Result := TempPath;
  855. end;
  856. function GetTempFilePath: string;
  857. begin
  858. //Result := GetTempFileDir + '\';
  859. Result := GetAppTempPath;
  860. end;
  861. function GetTempName(ALength: Integer): string;
  862. const
  863. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  864. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  865. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  866. 'W', 'X', 'Y', 'Z');
  867. var
  868. Temp: string;
  869. Num: Integer;
  870. begin
  871. Randomize;
  872. Temp := '';
  873. while Length(Temp) < ALength do
  874. begin
  875. Num := Random(37);
  876. If Num <> 36 Then
  877. Temp := Temp + CodedBuf[Num]; // 0..35
  878. end;
  879. Result := Temp;
  880. end;
  881. function GetTempFileName: string;
  882. var
  883. TempExt: string;
  884. begin
  885. Result := GetTempFilePath + GetTempName;
  886. TempExt := '.' + GetTempName(3);
  887. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  888. SameText(TempExt, '.ldb') do
  889. TempExt := '.' + GetTempName(3);
  890. Result := Result + TempExt;
  891. while FileExists(Result) do
  892. begin
  893. Result := GetTempFilePath + GetTempName;
  894. TempExt := '.' + GetTempName(3);
  895. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  896. SameText(TempExt, '.ldb') do
  897. TempExt := '.' + GetTempName(3);
  898. Result := Result + TempExt;
  899. end;
  900. end;
  901. function GetTempFileName(const APath, AExt: string): string;
  902. begin
  903. Result := APath + GetTempName + AExt;
  904. while FileExists(Result) do
  905. Result := APath + GetTempName + AExt;
  906. end;
  907. function GenerateTempFolder(AGeneratePath: string): string;
  908. begin
  909. Result := AGeneratePath + GetTempName;
  910. while DirectoryExists(Result) do
  911. Result := AGeneratePath + GetTempName;
  912. CreateDirectoryInDeep(Result);
  913. end;
  914. function GetNewGUIDFileName(const AGeneratePath: string): string;
  915. var
  916. gFile: TGUID;
  917. begin
  918. repeat
  919. CreateGUID(gFile);
  920. Result := AGeneratePath + GUIDToString(gFile);
  921. until not FileExists(Result);
  922. end;
  923. {Progress bar Control}
  924. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  925. begin
  926. if not SysProgressDisabled then
  927. UpdateProgress(APosition, AHint);
  928. end;
  929. procedure UpdateProgress(APosition: Integer; const AHint: string);
  930. begin
  931. MainForm.UpdateProgress(APosition, AHint);
  932. end;
  933. procedure DisableSysProgress;
  934. begin
  935. SysProgressDisabled := True;
  936. end;
  937. procedure EnableSysProgress;
  938. begin
  939. SysProgressDisabled := False;
  940. end;
  941. {Interface RePaint Control}
  942. procedure BeginUpdateWindow(AHandle: THandle);
  943. begin
  944. SendMessage(AHandle, WM_SETREDRAW, 0, 0);
  945. end;
  946. procedure EndUpdateWindow(AHandle: THandle);
  947. begin
  948. SendMessage(AHandle, WM_SETREDRAW, 1, 0);
  949. end;
  950. {Zip}
  951. function ZipComplete(const AFileName: string; ACheckMode: Integer): Boolean;
  952. const
  953. i4k = 4096;
  954. var
  955. iSize1, iSize2: Integer;
  956. begin
  957. iSize1 := FileSizeByName(AFileName);
  958. Sleep(300);
  959. iSize2 := FileSizeByName(AFileName);
  960. Result := (iSize1 = iSize2);
  961. if ACheckMode = 0 then
  962. Result := Result and (iSize1 > i4k);
  963. end;
  964. procedure ZipFolder(AFileFolder, AFileName: string; ACheckMode: Integer = 0);
  965. procedure AppendLog(const ALog: string);
  966. begin
  967. if SupportManager.ConfigInfo.IsLog then
  968. MeasureLog.AppendLogTo(ALog);
  969. end;
  970. var
  971. sTempFile: string;
  972. vZip: TVCLZip;
  973. iCount: Integer;
  974. begin
  975. AppendLog('Zip');
  976. sTempFile := GetTempFileName;
  977. vZip := TVCLZip.Create(nil);
  978. iCount := 0;
  979. try
  980. vZip.FilesList.Clear;
  981. vZip.ZipName := sTempFile;
  982. vzip.RootDir := AFileFolder;
  983. vZip.OverwriteMode := Always;
  984. //vZip.AddDirEntriesOnRecurse:=True;
  985. vZip.RelativePaths := True;
  986. //vZip.RecreateDirs := True;
  987. vZip.FilesList.Add(AFileFolder + '\*.*');
  988. vZip.Zip;
  989. AppendLog(Format('Check Zip File, FileSize - %d', [FileSizeByName(sTempFile)]));
  990. while not ZipComplete(sTempFile, ACheckMode) and (iCount < 10) do
  991. begin
  992. Sleep(200);
  993. Inc(iCount);
  994. AppendLog(Format('Check Zip File - %d, FileSize - %d', [iCount + 1, FileSizeByName(sTempFile)]));
  995. end;
  996. AppendLog('Check Zip File --> Pass');
  997. CopyFileOrFolder(sTempFile, AFileName);
  998. AppendLog('Zip --> Pass');
  999. finally
  1000. vZip.Free;
  1001. if FileExists(sTempFile) then
  1002. DeleteFile(sTempFile);
  1003. end;
  1004. end;
  1005. function UnZipFile(AFileName, AFileFolder: string): Boolean;
  1006. var
  1007. vUnZip: TVCLZip;
  1008. begin
  1009. Result := True;
  1010. vUnZip := TVCLZip.Create(nil);
  1011. try
  1012. vUnZip.FilesList.Clear;
  1013. vUnZip.ZipName := AFileName;
  1014. vUnZip.ReadZip;
  1015. vUnZip.DestDir := AFileFolder;
  1016. vUnZip.OverwriteMode := Always;
  1017. vUnZip.RecreateDirs := True;
  1018. vUnZip.RelativePaths := True;
  1019. vUnZip.DoAll := True;
  1020. vUnZip.FilesList.Add('*.*');
  1021. try
  1022. vUnZip.UnZip;
  1023. except
  1024. Result := False;
  1025. end;
  1026. finally
  1027. vUnZip.Free;
  1028. end;
  1029. end;
  1030. {Copy By Stream}
  1031. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  1032. var
  1033. MS: TMemoryStream;
  1034. begin
  1035. MS := TMemoryStream.Create;
  1036. try
  1037. MS.LoadFromFile(ASourceFile);
  1038. if FileExists(ADestFile) then
  1039. DeleteFile(ADestFile);
  1040. MS.SaveToFile(ADestFile);
  1041. finally
  1042. MS.Free;
  1043. end;
  1044. end;
  1045. procedure AppendTestLog(const AFileName, ALog: string);
  1046. var
  1047. f: TextFile;
  1048. begin
  1049. try
  1050. if FileExists(AFileName) then
  1051. begin
  1052. AssignFile(f, AFileName);
  1053. Append(f);
  1054. Writeln(f, ALog);
  1055. end
  1056. else
  1057. begin
  1058. AssignFile(f, AFileName);
  1059. Rewrite(f);
  1060. Writeln(f, ALog);
  1061. end;
  1062. finally
  1063. CloseFile(f);
  1064. end;
  1065. end;
  1066. function GetVersion(APartCount: Integer = 4): string;
  1067. var
  1068. V1, V2, V3, V4: Word;
  1069. FInfoSize, FF: Cardinal;
  1070. FInfo: Pointer;
  1071. FFixed: PVSFIXEDFILEINFO;
  1072. begin
  1073. FInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), FF);
  1074. FInfo := AllocMem(FInfoSize);
  1075. try
  1076. GetFileVersionInfo(PChar(Application.ExeName), FF, FInfoSize, FInfo);
  1077. VerQueryValue(FInfo, '\', Pointer(FFixed), FInfoSize);
  1078. V1 := FFixed.dwFileVersionMS shr 16;
  1079. V2 := FFixed.dwFileVersionMS and $FFFF;
  1080. V3 := FFixed.dwFileVersionLS shr 16;
  1081. V4 := FFixed.dwFileVersionLS and $FFFF;
  1082. finally
  1083. Dispose(FInfo);
  1084. end;
  1085. Result := '';
  1086. case APartCount of
  1087. 1:
  1088. Result := Format('%d', [V1]);
  1089. 2:
  1090. Result := Format('%d.%d', [V1, V2]);
  1091. 3:
  1092. Result := Format('%d.%d.%d', [V1, V2, V3]);
  1093. 4:
  1094. Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4]);
  1095. end;
  1096. end;
  1097. function ExtractFileNameWithoutExt(const AFileName: string): string;
  1098. var
  1099. sFileName, Ext: string;
  1100. begin
  1101. Result := '';
  1102. if AFileName = '' then
  1103. Exit;
  1104. sFileName := AFileName;
  1105. if sFileName[Length(sFileName)] = '\' then
  1106. Delete(sFileName, Length(sFileName), 1);
  1107. Result := ExtractFileName(sFileName);
  1108. Ext := ExtractFileExt(sFileName);
  1109. Delete(Result, Length(Result) - Length(Ext) + 1, Length(Ext));
  1110. end;
  1111. function ShortText(AText: string; AWidth: Integer): string;
  1112. var vMM: TMemo;
  1113. begin
  1114. if Trim(AText) = '' then
  1115. begin
  1116. Result := '';
  1117. Exit;
  1118. end;
  1119. vMM := TMemo.Create(nil);
  1120. try
  1121. vMM.Visible := False;
  1122. vMM.WordWrap := True;
  1123. vMM.parent := Application.MainForm;
  1124. vMM.Width := AWidth - 10; // 10像素留给3个小点
  1125. vMM.Text := AText;
  1126. if vMM.Lines.Count > 1 then
  1127. Result := vMM.Lines[0] + '...'
  1128. else
  1129. Result := AText;
  1130. finally
  1131. vMM.Free;
  1132. end;
  1133. end;
  1134. function CustomWidthText(AText: string; AWidth: Integer): string;
  1135. var vMM: TMemo;
  1136. i: Integer;
  1137. begin
  1138. if Trim(AText) = '' then
  1139. begin
  1140. Result := '';
  1141. Exit;
  1142. end;
  1143. vMM := TMemo.Create(nil);
  1144. try
  1145. vMM.Visible := False;
  1146. vMM.WordWrap := True;
  1147. vMM.parent := Application.MainForm;
  1148. vMM.Width := AWidth;
  1149. vMM.Text := AText;
  1150. for i := 0 to vMM.Lines.Count - 1 do
  1151. begin
  1152. if i = 0 then
  1153. Result := vMM.Lines[i]
  1154. else
  1155. Result := Result + #10#13 + vMM.Lines[i];
  1156. end;
  1157. finally
  1158. vMM.Free;
  1159. end;
  1160. end;
  1161. function ReplaceCharsForJson(AText: string): string;
  1162. const
  1163. BefChar: array [0..7] of Char = ('{', '}', ',', ':', '"', '[', ']', '%');
  1164. AftChar: array [0..7] of string = ('{', '}', ',', ':', '"', '【', '】', '♂');
  1165. var I: Integer;
  1166. begin
  1167. AText := Trim(AText);
  1168. Result := AText;
  1169. if AText = '' then Exit;
  1170. for I := low(BefChar) to High(BefChar) do
  1171. begin
  1172. if Pos(BefChar[I], AText) > 0 then
  1173. AText := StringReplace(AText, BefChar[I], AftChar[I], [rfReplaceAll]);
  1174. end;
  1175. Result := AText;
  1176. end;
  1177. function RecoverCharsFromJson(AText: string): string;
  1178. const
  1179. BefStr: array [0..3] of string = ('♂', '\r\n', '\r', '\n');
  1180. AftStr: array [0..3] of string = ('%', '', '', '');
  1181. var I: Integer;
  1182. begin
  1183. AText := Trim(AText);
  1184. Result := AText;
  1185. if AText = '' then Exit;
  1186. for I := low(BefStr) to High(BefStr) do
  1187. begin
  1188. if Pos(BefStr[I], AText) > 0 then
  1189. AText := StringReplace(AText, BefStr[I], AftStr[I], [rfReplaceAll]);
  1190. end;
  1191. Result := AText;
  1192. end;
  1193. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  1194. var
  1195. SRec: TSearchRec;
  1196. retval: Integer;
  1197. begin
  1198. AFileList.Clear;
  1199. retval := FindFirst(APath + AExtName, faAnyFile, sRec);
  1200. try
  1201. while retval = 0 do
  1202. begin
  1203. if (SRec.Attr and faDirectory) = 0 then
  1204. AFileList.Add(Srec.Name);
  1205. retval := FindNext(SRec);
  1206. end;
  1207. finally
  1208. FindClose(SRec);
  1209. end;
  1210. end;
  1211. function DeleteFolder(const FolderStr: string): Boolean;
  1212. var
  1213. fos: SHFILEOPSTRUCT;
  1214. begin
  1215. ZeroMemory(@fos, SizeOf(fos));
  1216. fos.Wnd := HWND_DESKTOP;
  1217. fos.wFunc := FO_DELETE;
  1218. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  1219. fos.pFrom := PChar(FolderStr + #0);
  1220. Result := SHFileOperation(fos) <> 0;
  1221. end;
  1222. function HasExt(const AFileName: string): Boolean;
  1223. begin
  1224. Result := ExtractFileExt(AFileName) <> '';
  1225. end;
  1226. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  1227. var
  1228. fos: SHFILEOPSTRUCT;
  1229. begin
  1230. ZeroMemory(@fos, SizeOf(fos));
  1231. fos.Wnd := HWND_DESKTOP;
  1232. fos.wFunc := FO_COPY;
  1233. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO;
  1234. fos.pFrom := PChar(SrcFolder + #0);
  1235. fos.pTo := PChar(DstFolder + #0);
  1236. Result := SHFileOperation(fos) <> 0;
  1237. end;
  1238. function FileCount(APath, AExt: string): Integer;
  1239. var
  1240. vSR: TSearchRec;
  1241. iRetval: Integer;
  1242. vSL: TStringList;
  1243. begin
  1244. vSL := TStringList.Create;
  1245. iRetval := FindFirst(APath + '*' + AExt, faAnyFile, vSR);
  1246. try
  1247. while iRetval = 0 do
  1248. begin
  1249. if (vSR.Attr and faDirectory) = 0 then
  1250. vSL.Add(vSR.Name);
  1251. iRetval := FindNext(vSR);
  1252. end;
  1253. Result := vSL.Count;
  1254. finally
  1255. FindClose(vSR);
  1256. vSL.Free;
  1257. end;
  1258. end;
  1259. function ChangeExt(AFile, ANewExt: string): string;
  1260. var i, iPos: Integer;
  1261. begin
  1262. for i := Length(AFile) Downto 1 do
  1263. begin
  1264. if AFile[i] = '.' then
  1265. begin
  1266. iPos := i;
  1267. Break;
  1268. end;
  1269. end;
  1270. Result := Copy(AFile, 1, iPos - 1) + ANewExt;
  1271. end;
  1272. function CheckExt(AName, AExt: string): string;
  1273. var bHasExt: Boolean;
  1274. begin
  1275. bHasExt := ExtractFileExt(AName) <> '';
  1276. if not bHasExt then
  1277. Result := AName + AExt
  1278. else
  1279. begin
  1280. if (UpperCase(ExtractFileExt(AName)) <> AExt) then
  1281. Result := ChangeExt(AName, AExt)
  1282. else
  1283. Result := AName;
  1284. end;
  1285. end;
  1286. function TempName(ALength: Integer): string;
  1287. const
  1288. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  1289. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  1290. 'J', 'K', 'L', 'M', 'N',
  1291. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  1292. 'W', 'X', 'Y', 'Z');
  1293. begin
  1294. Result := '';
  1295. Randomize;
  1296. while Length(Result) < ALength do
  1297. Result := Result + CodedBuf[Random(36)];
  1298. end;
  1299. function URLFileName(AURL: string): string;
  1300. var
  1301. i: integer;
  1302. s: string;
  1303. begin
  1304. s := AURL;
  1305. i := Pos('/', s);
  1306. while i <> 0 do
  1307. begin
  1308. Delete(s, 1, i);
  1309. i := Pos('/', s);
  1310. end;
  1311. Result := s;
  1312. end;
  1313. function ReplaceChars(AStr: string): string;
  1314. var s: string;
  1315. begin
  1316. s := Trim(AStr);
  1317. s := StringReplace(s, '\r\n', #$D#$A, [rfReplaceAll]);
  1318. Result := s;
  1319. end;
  1320. end.