UtilMethods.pas 37 KB

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