UtilMethods.pas 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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;
  6. type
  7. TBookmarkRefreshEvent = procedure (AExpandFrame: Boolean) of object;
  8. {RoundTo}
  9. function QuantityRoundTo(AValue: Double): Double;
  10. function PriceRoundTo(AValue: Double): Double;
  11. function TotalPriceRoundTo(AValue: Double): Double;
  12. {Interface Control}
  13. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  14. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  15. {Message}
  16. procedure WarningMessage(const AMsg: string);
  17. procedure ErrorMessage(const AMsg: string);
  18. function QuestMessage(const AMsg: string): Boolean;
  19. function QuestMessageYesNo(const AMsg: string): Boolean;
  20. procedure TipMessage(const AMsg: string);
  21. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  22. {Get Common Path}
  23. function GetAppFilePath: string;
  24. function GetMyProjectsFilePath: string;
  25. function GetEmptyDataBaseFileName: string;
  26. function GetTemplateBillsFileName: string;
  27. function GetTemplateXlsFileName: string;
  28. function GetBackupFilePath: string;
  29. function GetReportTemplatePath: string;
  30. {Select & Save File Choose}
  31. function GetFilter(AExt: string): string;
  32. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  33. function SaveFile(var FileName: string; const AExt: string): Boolean;
  34. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  35. AParentHandle: THandle = 0; AHasNewFolderBtn: Boolean = True): Boolean;
  36. function FixPathByAppPath(AFileName: string): string;
  37. function BrowseFolder(var APath: string; const ATitle: string;
  38. AParentHandle: THandle; AHasNewFolderBtn: Boolean = True): Boolean;
  39. {CheckStrings}
  40. function CheckPeg(const AStr: string): Boolean;
  41. function CheckValidPassword(APassword: string): Boolean;
  42. function ValidInteger(var AKey: Char): Boolean;
  43. {MergeStrings}
  44. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  45. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  46. {CodeTransform}
  47. function B_CodeToIndexCode(const AB_Code: string): string;
  48. function ChinessNum(const ADigitNum: Integer): string;
  49. {Compare Code}
  50. //function CompareCodeWithChar(const ACode1, ACode2: string): Integer;
  51. {Tree Relative}
  52. function CreateTree: TZjIDTree;
  53. procedure DisConnectTree(ATree: TZjIDTree);
  54. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  55. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  56. {Generate Name/Directory/Path}
  57. function GetTempFileDir: string;
  58. function GetTempFilePath: string;
  59. function GetTempName(ALength: Integer = 8): string;
  60. function GetTempFileName: string;
  61. function GenerateTempFolder(AGeneratePath: string): string;
  62. function GetNewGUIDFileName(const AGeneratePath: string): string;
  63. {Progress bar Control}
  64. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  65. procedure UpdateProgress(APosition: Integer; const AHint: string);
  66. procedure DisableSysProgress;
  67. procedure EnableSysProgress;
  68. {Interface RePaint Control}
  69. procedure BeginUpdateWindow(AHandle: THandle);
  70. procedure EndUpdateWindow(AHandle: THandle);
  71. {Zip}
  72. procedure ZipFolder(AFileFolder, AFileName: string);
  73. procedure UnZipFile(AFileName, AFileFolder: string);
  74. {Copy By Stream}
  75. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  76. // Add by chenshilong, 2014-04-11
  77. function GetVersion(APartCount: Integer = 4): string;
  78. function ExtractFileNameWithoutExt(const AFileName: string): string;
  79. function ShortText(AText: string; AWidth: Integer): string; // 文本缩略显示
  80. function CustomWidthText(AText: string; AWidth: Integer): string;
  81. function ReplaceCharsForJson(AText: string): string; // 替换Json文本中的特殊字符
  82. function RecoverCharsFromJson(AText: string): string; // 下载得到的Json文本恢复成用户需要的文本
  83. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  84. function DeleteFolder(const FolderStr: string): Boolean;
  85. function HasExt(const AFileName: string): Boolean;
  86. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  87. function FileCount(APath: string): Integer;
  88. implementation
  89. uses
  90. SysUtils, dxBar, MainFrm, ConstUnit, Globals, StdCtrls, ShellAPI,
  91. ScUtils;
  92. var
  93. SysProgressDisabled: Boolean;
  94. {RoundTo}
  95. function QuantityRoundTo(AValue: Double): Double;
  96. begin
  97. if Assigned(OpenProjectManager.CurProjectData) then
  98. Result := ScRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.QuantityDigit)
  99. else
  100. Result := ScRoundTo(AValue, iQuantityDigit);
  101. end;
  102. function PriceRoundTo(AValue: Double): Double;
  103. begin
  104. if Assigned(OpenProjectManager.CurProjectData) then
  105. Result := ScRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.PriceDigit)
  106. else
  107. Result := ScRoundTo(AValue, iPriceDigit);
  108. end;
  109. function TotalPriceRoundTo(AValue: Double): Double;
  110. begin
  111. if Assigned(OpenProjectManager.CurProjectData) then
  112. Result := ScRoundTo(AValue, -OpenProjectManager.CurProjectData.ProjProperties.TotalPriceDigit)
  113. else
  114. Result := ScRoundTo(AValue, iTotalPriceDigit);
  115. end;
  116. {Interface Control}
  117. procedure AlignControl(AControl, AParent: TWinControl; AAlign: TAlign);
  118. begin
  119. if Assigned(AControl) then
  120. begin
  121. if Assigned(AControl.Parent) then
  122. AControl.Parent.RemoveControl(AControl);
  123. AControl.Parent := AParent;
  124. AControl.Align := AAlign;
  125. end;
  126. end;
  127. procedure SetDxBtnAction(AAction: TAction; ADxBtn: TObject);
  128. begin
  129. if Assigned(ADxBtn) then
  130. if ADxBtn is TdxBarButton then
  131. TdxBarButton(ADxBtn).Action := AAction;
  132. end;
  133. {Message}
  134. procedure WarningMessage(const AMsg: string);
  135. begin
  136. MessageBox(Application.Handle, PChar(AMsg), PChar('警告'), MB_OK or MB_ICONWARNING);
  137. end;
  138. procedure ErrorMessage(const AMsg: string);
  139. begin
  140. MessageBox(Application.Handle, PChar(AMsg), PChar('错误'), MB_OK or MB_ICONERROR);
  141. end;
  142. function QuestMessage(const AMsg: string): Boolean;
  143. begin
  144. Result := MessageBox(Application.Handle, PChar(AMsg), PChar('询问'), MB_OKCANCEL or MB_ICONQUESTION) = IDOK;
  145. end;
  146. function QuestMessageYesNo(const AMsg: string): Boolean;
  147. begin
  148. Result := MessageBox(Application.Handle, PChar(AMsg), PChar('询问'), MB_YESNO or MB_ICONQUESTION) = IDYes;
  149. end;
  150. procedure TipMessage(const AMsg: string);
  151. begin
  152. MessageBox(Application.Handle, PChar(AMsg), PChar('提示'), MB_OK or MB_ICONINFORMATION);
  153. end;
  154. procedure DataSetErrorMessage(var Allow: Boolean; const AMsg: string);
  155. begin
  156. Allow := False;
  157. ErrorMessage(AMsg);
  158. end;
  159. {Get Common Path}
  160. function GetAppFilePath: string;
  161. begin
  162. Result := ExtractFilePath(ParamStr(0));
  163. end;
  164. function GetMyProjectsFilePath: string;
  165. begin
  166. Result := GetAppFilePath + '我的项目\';
  167. end;
  168. function GetEmptyDataBaseFileName: string;
  169. begin
  170. Result := GetAppFilePath + 'Data\Base.dat';
  171. end;
  172. function GetTemplateBillsFileName: string;
  173. begin
  174. Result := GetAppFilePath + 'Data\Template.xls';
  175. end;
  176. function GetTemplateXlsFileName: string;
  177. begin
  178. Result := GetAppFilePath + 'Data\Basic.xls';
  179. end;
  180. function GetBackupFilePath: string;
  181. begin
  182. Result := GetAppFilePath + 'TenderBackup\';
  183. end;
  184. function GetReportTemplatePath: string;
  185. begin
  186. Result := GetAppFilePath + 'ReportTemplates\';
  187. end;
  188. {Select & Save File Choose}
  189. function GetFilter(AExt: string): string;
  190. begin
  191. if AExt = '' then
  192. Result := '所有文件(*.*)|*.*'
  193. else
  194. begin
  195. if AExt[1] <> '.' then
  196. AExt := '.' + AExt;
  197. Result := Format('(*%s)|*%s', [AExt, AExt]);
  198. end;
  199. end;
  200. function SelectFile(var AFileName: string; const AExt: string): Boolean;
  201. var
  202. odFile: TOpenDialog;
  203. begin
  204. odFile := TOpenDialog.Create(nil);
  205. try
  206. odFile.Filter := GetFilter(AExt);
  207. if odFile.Execute then
  208. begin
  209. Application.ProcessMessages;
  210. AFileName := odFile.FileName;
  211. Result := True;
  212. end
  213. else
  214. Result := False;
  215. finally
  216. odFile.Free;
  217. end;
  218. end;
  219. function SaveFile(var FileName: string; const AExt: string): Boolean;
  220. var
  221. sdFile: TSaveDialog;
  222. begin
  223. sdFile := TSaveDialog.Create(nil);
  224. try
  225. sdFile.FileName := FileName;
  226. sdFile.DefaultExt := AExt;
  227. sdFile.Filter := GetFilter(AExt);
  228. Result := sdFile.Execute;
  229. if Result then
  230. FileName := sdFile.FileName;
  231. Application.ProcessMessages;
  232. finally
  233. sdFile.Free;
  234. end;
  235. end;
  236. function SelectOutputDirectory(const ATitle: string; var ADirectory: string;
  237. AParentHandle: THandle; AHasNewFolderBtn: Boolean): Boolean;
  238. var
  239. pID: PItemIDList;
  240. bInfo: TBrowseInfo;
  241. AHandle: THandle;
  242. PStr: array[0..1023] of Char;
  243. sPath: string;
  244. function BFCallBack(Hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  245. begin
  246. if uMsg = BFFM_INITIALIZED then
  247. begin
  248. SendMessage(Hwnd, BFFM_SETSELECTION, 1, lpData);
  249. end;
  250. Result := 0;
  251. end;
  252. begin
  253. Result := False;
  254. if AParentHandle = 0 then
  255. AHandle := Screen.ActiveForm.Handle
  256. else
  257. AHandle := AParentHandle;
  258. if ADirectory = '' then
  259. sPath := GetAppFilePath
  260. else
  261. sPath := ADirectory;
  262. with bInfo do
  263. begin
  264. hwndOwner := AParentHandle;
  265. iImage := 0;
  266. lParam := Integer(PChar(sPath));
  267. lpszTitle := PChar(ATitle);
  268. pidlRoot := nil;
  269. pszDisplayName := nil;
  270. if AHasNewFolderBtn then
  271. ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI
  272. else
  273. ulFlags := BIF_RETURNONLYFSDIRS;
  274. lpfn := @BFCallBack;
  275. end;
  276. pID := SHBrowseForFolder(bInfo);
  277. if pID <> nil then
  278. begin
  279. SHGetPathFromIDList(pID, PStr);
  280. ADirectory := StrPas(PStr);
  281. if (ADirectory <> '') and (ADirectory[Length(ADirectory)] <> '\') then
  282. ADirectory := ADirectory + '\';
  283. Result := True;
  284. end;
  285. end;
  286. function FixPathByAppPath(AFileName: string): string;
  287. begin
  288. Result := AFileName;
  289. if AnsiPos(':\', Result) = 0 then
  290. begin
  291. if (Result <> '') and (Result[1] = '\') then
  292. Delete(Result, 1, 1);
  293. Result := ExtractFilePath(Application.ExeName) + Result;
  294. end;
  295. end;
  296. var
  297. PathStr: string;
  298. function BrowseFolder(var APath: string; const ATitle: string; AParentHandle: THandle;
  299. AHasNewFolderBtn: Boolean): Boolean;
  300. function BFCallBackFunc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;
  301. begin
  302. Result := 0;
  303. case uMsg of
  304. BFFM_INITIALIZED : // 初始化设置目录。
  305. begin
  306. SendMessage(Hwnd,BFFM_SETSELECTION,-1,Integer(PathStr));
  307. end;
  308. end;
  309. end;
  310. var
  311. pID: PItemIDList;
  312. bInfo: TBrowseInfo;
  313. PStr: array[0..1023] of Char;
  314. sPath: string;
  315. Hdl: THandle;
  316. begin
  317. Result := False;
  318. PathStr := '';
  319. if AParentHandle = 0 then
  320. Hdl := Screen.ActiveForm.Handle
  321. else
  322. Hdl := AParentHandle;
  323. sPath := FixPathByAppPath(APath);
  324. if DirectoryExists(sPath) then
  325. PathStr := sPath;
  326. bInfo.hwndOwner := Hdl;
  327. bInfo.iImage := 0;
  328. bInfo.lParam := 0;
  329. bInfo.lpszTitle := PChar(ATitle);
  330. bInfo.pidlRoot := nil;
  331. bInfo.pszDisplayName := nil;
  332. if (not AHasNewFolderBtn) then
  333. bInfo.ulFlags := BIF_RETURNONLYFSDIRS
  334. else
  335. bInfo.ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI;
  336. bInfo.lpfn := @BFCallBackFunc;
  337. pID := SHBrowseForFolder(bInfo);
  338. if pID <> nil then
  339. begin
  340. SHGetPathFromIDList(pID,pStr);
  341. APath := StrPas(pStr);
  342. if (APath <> '') and (APath[Length(APath)] <> '\') then
  343. APath := APath + '\';
  344. Result := True;
  345. end;
  346. end;
  347. {CheckStrings}
  348. function CheckPeg(const AStr: string): Boolean;
  349. function GetPosition(const AName, AStr, AStrSpare: string): Integer;
  350. begin
  351. Result := Pos(AStr, AName);
  352. if Result = 0 then
  353. Result := Pos(AStrSpare, AName);
  354. end;
  355. var
  356. iPosK, iPosPlus: Integer;
  357. fNum: Double;
  358. begin
  359. Result := False;
  360. iPosK := GetPosition(AStr, 'K', 'k');
  361. iPosPlus := GetPosition(AStr, '+', '+');
  362. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  363. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum);
  364. end;
  365. function CheckValidPassword(APassword: string): Boolean;
  366. var
  367. iIndex, iLength: Integer;
  368. begin
  369. Result := True;
  370. if APassword = '' then Exit;
  371. iIndex := 1;
  372. iLength := Length(APassword);
  373. Result := (iLength >= 6) and (iLength <= 16);
  374. while Result and (iIndex < iLength) do
  375. begin
  376. Result := APassword[iIndex] in ['A'..'Z', 'a'..'z', '0'..'9'];
  377. Inc(iIndex);
  378. end;
  379. end;
  380. function ValidInteger(var AKey: Char): Boolean;
  381. begin
  382. if AKey in ['+', '-', '0'..'9', #8, #13, #35, #36,
  383. #37, #38, #39, #40, #46] then
  384. begin
  385. Result := True;
  386. end
  387. else
  388. begin
  389. AKey := #0;
  390. Result := False;
  391. end;
  392. end;
  393. {MergeStrings}
  394. function MergeRelaBGL(const ABGLCode1, ABGLCode2: string): string;
  395. var
  396. sgs1, sgs2: TStrings;
  397. i1, i2: Integer;
  398. bExist: Boolean;
  399. begin
  400. Result := ABGLCode1;
  401. sgs1 := TStringList.Create;
  402. sgs2 := TStringList.Create;
  403. try
  404. sgs1.Delimiter := ';';
  405. sgs2.Delimiter := ';';
  406. sgs1.DelimitedText := ABGLCode1;
  407. sgs2.DelimitedText := ABGLCode2;
  408. for i2 := 0 to sgs2.Count - 1 do
  409. begin
  410. bExist := False;
  411. for i1 := 0 to sgs1.Count - 1 do
  412. if SameText(sgs2[i2], sgs1[i1]) then
  413. begin
  414. bExist := True;
  415. Break;
  416. end;
  417. if not bExist then
  418. sgs1.Add(sgs2[i2]);
  419. end;
  420. Result := sgs1.DelimitedText;
  421. finally
  422. sgs1.Free;
  423. sgs2.Free;
  424. end;
  425. end;
  426. procedure MergeRelaBGLAndNum(var ABGLCode1, ABGLNum1: string; const ABGLCode2, ABGLNum2: string);
  427. var
  428. sgsC1, sgsC2, sgsN1, sgsN2: TStrings;
  429. i1, i2: Integer;
  430. bExist: Boolean;
  431. begin
  432. sgsC1 := TStringList.Create;
  433. sgsC2 := TStringList.Create;
  434. sgsN1 := TStringList.Create;
  435. sgsN2 := TStringList.Create;
  436. try
  437. sgsC1.Delimiter := ';';
  438. sgsC2.Delimiter := ';';
  439. sgsC1.DelimitedText := ABGLCode1;
  440. sgsC2.DelimitedText := ABGLCode2;
  441. sgsN1.Delimiter := ';';
  442. sgsN2.Delimiter := ';';
  443. sgsN1.DelimitedText := ABGLNum1;
  444. sgsN2.DelimitedText := ABGLNum2;
  445. for i2 := 0 to sgsC2.Count - 1 do
  446. begin
  447. bExist := False;
  448. for i1 := 0 to sgsC1.Count - 1 do
  449. if SameText(sgsC2[i2], sgsC1[i1]) then
  450. begin
  451. bExist := True;
  452. Break;
  453. end;
  454. if bExist then
  455. begin
  456. sgsN1[i1] := FloatToStr(StrToFloatDef(sgsN1[i1], 0) + StrToFloatDef(sgsN2[i2], 0))
  457. end
  458. else
  459. begin
  460. sgsC1.Add(sgsC2[i2]);
  461. sgsN1.Add(sgsN2[i2]);
  462. end;
  463. end;
  464. ABGLCode1 := sgsC1.DelimitedText;
  465. ABGLNum1 := sgsN1.DelimitedText;
  466. finally
  467. sgsC1.Free;
  468. sgsC2.Free;
  469. sgsN1.Free;
  470. sgsN2.Free;
  471. end;
  472. end;
  473. function B_CodeToIndexCode(const AB_Code: string): string;
  474. var
  475. sgs: TStrings;
  476. i, iNum, iError: Integer;
  477. sError: string;
  478. begin
  479. sgs := TStringList.Create;
  480. try
  481. Result := '';
  482. sgs.Delimiter := '-';
  483. sgs.DelimitedText := AB_Code;
  484. for i := 0 to sgs.Count - 1 do
  485. begin
  486. Val(sgs[i], iNum, iError);
  487. if iError = 0 then
  488. sError := ''
  489. else
  490. sError := Copy(sgs[i], iError, Length(sgs[i])-iError+1);
  491. if iError = 0 then
  492. Result := Result + Format('%4d', [iNum]) + Format('%-3s', [sError])
  493. else if iNum = 0 then
  494. Result := Result + '9999' + Format('%3s', [sError])
  495. else
  496. Result := Result + Format('%4d', [iNum]) + Format('%-3s', [sError]);
  497. end;
  498. finally
  499. sgs.Free;
  500. end;
  501. end;
  502. function ChinessNum(const ADigitNum: Integer): string;
  503. function TransChineseNum(const ANum, AZeroNum: Integer): string;
  504. begin
  505. Result := '';
  506. case ANum of
  507. 1: if AZeroNum <> 1 then Result := '一';
  508. 2: Result := '二';
  509. 3: Result := '三';
  510. 4: Result := '四';
  511. 5: Result := '五';
  512. 6: Result := '六';
  513. 7: Result := '七';
  514. 8: Result := '八';
  515. 9: Result := '九';
  516. end;
  517. if (Result = '') and ((AZeroNum <> 1) or (ANum = 0)) then Exit;
  518. case AZeroNum of
  519. 0: Result := Result;
  520. 1: Result := Result + '十';
  521. 2: Result := Result + '百';
  522. 3: Result := Result + '千';
  523. 4: Result := Result + '万';
  524. end;
  525. end;
  526. var
  527. iBai, iShi, iGe: Integer;
  528. begin
  529. Result := '';
  530. if (ADigitNum < 0) and (ADigitNum > 10000) then Exit;
  531. iBai := ADigitNum div 100;
  532. iShi := (ADigitNum mod 100) div 10;
  533. iGe := (ADigitNum mod 100) mod 10;
  534. Result := TransChineseNum(iBai, 2) + TransChineseNum(iShi, 1) + TransChineseNum(iGe, 0);
  535. end;
  536. {Tree Relative}
  537. function CreateTree: TZjIDTree;
  538. begin
  539. Result := TZjIDTree.Create;
  540. Result.KeyFieldName := 'ID';
  541. Result.ParentFieldName := 'ParentID';
  542. Result.NextSiblingFieldName := 'NextSiblingID';
  543. Result.AutoCreateKeyID := True;
  544. Result.AutoExpand := True;
  545. end;
  546. procedure DisConnectTree(ATree: TZjIDTree);
  547. begin
  548. ATree.DataSet := nil;
  549. ATree.Active := False;
  550. end;
  551. procedure ConnectTree(ATree: TZjIDTree; ADataset: TDataSet);
  552. begin
  553. ATree.DataSet := ADataset;
  554. ATree.Active := True;
  555. end;
  556. procedure InitGridHead(AGridDBA: TZjGridDBA; AGrid: TZJGrid);
  557. var
  558. I: Integer;
  559. begin
  560. for I := 0 to AGridDBA.Columns.Count - 1 do
  561. AGrid.Cells[I + 1, 0].Text := AGridDBA.Columns[I].Title.Caption;
  562. end;
  563. {Generate Name/Path}
  564. function GetTempFileDir: string;
  565. var
  566. TempPath: string;
  567. begin
  568. TempPath := GetEnvironmentVariable('TMP');
  569. if TempPath = '' then
  570. TempPath := GetEnvironmentVariable('TEMP');
  571. if TempPath = '' then
  572. begin
  573. if not DirectoryExists(ExtractFileDir(ParamStr(0)) + '\Temp') then
  574. CreateDir(ExtractFileDir(ParamStr(0)) + '\Temp');
  575. TempPath := ExtractFileDir(ParamStr(0)) + '\Temp';
  576. end;
  577. Result := TempPath;
  578. end;
  579. function GetTempFilePath: string;
  580. begin
  581. Result := GetTempFileDir + '\';
  582. end;
  583. function GetTempName(ALength: Integer): string;
  584. const
  585. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  586. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  587. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  588. 'W', 'X', 'Y', 'Z');
  589. var
  590. Temp: string;
  591. Num: Integer;
  592. begin
  593. Randomize;
  594. Temp := '';
  595. while Length(Temp) < ALength do
  596. begin
  597. Num := Random(37);
  598. If Num <> 36 Then
  599. Temp := Temp + CodedBuf[Num]; // 0..35
  600. end;
  601. Result := Temp;
  602. end;
  603. function GetTempFileName: string;
  604. var
  605. TempExt: string;
  606. begin
  607. Result := GetTempFilePath + GetTempName;
  608. TempExt := '.' + GetTempName(3);
  609. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  610. SameText(TempExt, '.ldb') do
  611. TempExt := '.' + GetTempName(3);
  612. Result := Result + TempExt;
  613. while FileExists(Result) do
  614. begin
  615. Result := GetTempFilePath + GetTempName;
  616. TempExt := '.' + GetTempName(3);
  617. while SameText(TempExt, '.mdb') or SameText(TempExt, '.db') or
  618. SameText(TempExt, '.ldb') do
  619. TempExt := '.' + GetTempName(3);
  620. Result := Result + TempExt;
  621. end;
  622. end;
  623. function GenerateTempFolder(AGeneratePath: string): string;
  624. begin
  625. Result := AGeneratePath + GetTempName;
  626. while DirectoryExists(Result) do
  627. Result := AGeneratePath + GetTempName;
  628. CreateDirectoryInDeep(Result);
  629. end;
  630. function GetNewGUIDFileName(const AGeneratePath: string): string;
  631. var
  632. gFile: TGUID;
  633. begin
  634. repeat
  635. CreateGUID(gFile);
  636. Result := AGeneratePath + GUIDToString(gFile);
  637. until not FileExists(Result);
  638. end;
  639. {Progress bar Control}
  640. procedure UpdateSysProgress(APosition: Integer; const AHint: string);
  641. begin
  642. if not SysProgressDisabled then
  643. UpdateProgress(APosition, AHint);
  644. end;
  645. procedure UpdateProgress(APosition: Integer; const AHint: string);
  646. begin
  647. MainForm.UpdateProgress(APosition, AHint);
  648. end;
  649. procedure DisableSysProgress;
  650. begin
  651. SysProgressDisabled := True;
  652. end;
  653. procedure EnableSysProgress;
  654. begin
  655. SysProgressDisabled := False;
  656. end;
  657. {Interface RePaint Control}
  658. procedure BeginUpdateWindow(AHandle: THandle);
  659. begin
  660. SendMessage(AHandle, WM_SETREDRAW, 0, 0);
  661. end;
  662. procedure EndUpdateWindow(AHandle: THandle);
  663. begin
  664. SendMessage(AHandle, WM_SETREDRAW, 1, 0);
  665. end;
  666. {Zip}
  667. procedure ZipFolder(AFileFolder, AFileName: string);
  668. var
  669. vZip: TVCLZip;
  670. begin
  671. vZip := TVCLZip.Create(nil);
  672. try
  673. vZip.FilesList.Clear;
  674. vZip.ZipName := AFileName;
  675. vzip.RootDir := AFileFolder;
  676. vZip.OverwriteMode := Always;
  677. //vZip.AddDirEntriesOnRecurse:=True;
  678. vZip.RelativePaths := True;
  679. //vZip.RecreateDirs := True;
  680. vZip.FilesList.Add(AFileFolder + '\*.*');
  681. vZip.Zip;
  682. finally
  683. vZip.Free;
  684. end;
  685. end;
  686. procedure UnZipFile(AFileName, AFileFolder: string);
  687. var
  688. vUnZip: TVCLZip;
  689. begin
  690. vUnZip := TVCLZip.Create(nil);
  691. try
  692. vUnZip.FilesList.Clear;
  693. vUnZip.ZipName := AFileName;
  694. vUnZip.ReadZip;
  695. vUnZip.DestDir := AFileFolder;
  696. vUnZip.OverwriteMode := Always;
  697. vUnZip.RecreateDirs := True;
  698. vUnZip.RelativePaths := True;
  699. vUnZip.DoAll := True;
  700. vUnZip.FilesList.Add('*.*');
  701. vUnZip.UnZip;
  702. finally
  703. vUnZip.Free;
  704. end;
  705. end;
  706. {Copy By Stream}
  707. procedure CopyFileByStream(const ASourceFile, ADestFile: string);
  708. var
  709. MS: TMemoryStream;
  710. begin
  711. MS := TMemoryStream.Create;
  712. try
  713. MS.LoadFromFile(ASourceFile);
  714. if FileExists(ADestFile) then
  715. DeleteFile(ADestFile);
  716. MS.SaveToFile(ADestFile);
  717. finally
  718. MS.Free;
  719. end;
  720. end;
  721. function GetVersion(APartCount: Integer = 4): string;
  722. var
  723. V1, V2, V3, V4: Word;
  724. FInfoSize, FF: Cardinal;
  725. FInfo: Pointer;
  726. FFixed: PVSFIXEDFILEINFO;
  727. begin
  728. FInfoSize := GetFileVersionInfoSize(PChar(Application.ExeName), FF);
  729. FInfo := AllocMem(FInfoSize);
  730. try
  731. GetFileVersionInfo(PChar(Application.ExeName), FF, FInfoSize, FInfo);
  732. VerQueryValue(FInfo, '\', Pointer(FFixed), FInfoSize);
  733. V1 := FFixed.dwFileVersionMS shr 16;
  734. V2 := FFixed.dwFileVersionMS and $FFFF;
  735. V3 := FFixed.dwFileVersionLS shr 16;
  736. V4 := FFixed.dwFileVersionLS and $FFFF;
  737. finally
  738. Dispose(FInfo);
  739. end;
  740. Result := '';
  741. case APartCount of
  742. 1:
  743. Result := Format('%d', [V1]);
  744. 2:
  745. Result := Format('%d.%d', [V1, V2]);
  746. 3:
  747. Result := Format('%d.%d.%d', [V1, V2, V3]);
  748. 4:
  749. Result := Format('%d.%d.%d.%d', [V1, V2, V3, V4]);
  750. end;
  751. end;
  752. function ExtractFileNameWithoutExt(const AFileName: string): string;
  753. var
  754. sFileName, Ext: string;
  755. begin
  756. Result := '';
  757. if AFileName = '' then
  758. Exit;
  759. sFileName := AFileName;
  760. if sFileName[Length(sFileName)] = '\' then
  761. Delete(sFileName, Length(sFileName), 1);
  762. Result := ExtractFileName(sFileName);
  763. Ext := ExtractFileExt(sFileName);
  764. Delete(Result, Length(Result) - Length(Ext) + 1, Length(Ext));
  765. end;
  766. function ShortText(AText: string; AWidth: Integer): string;
  767. var vMM: TMemo;
  768. begin
  769. if Trim(AText) = '' then
  770. begin
  771. Result := '';
  772. Exit;
  773. end;
  774. vMM := TMemo.Create(nil);
  775. try
  776. vMM.Visible := False;
  777. vMM.WordWrap := True;
  778. vMM.parent := Application.MainForm;
  779. vMM.Width := AWidth - 10; // 10像素留给3个小点
  780. vMM.Text := AText;
  781. if vMM.Lines.Count > 1 then
  782. Result := vMM.Lines[0] + '...'
  783. else
  784. Result := AText;
  785. finally
  786. vMM.Free;
  787. end;
  788. end;
  789. function CustomWidthText(AText: string; AWidth: Integer): string;
  790. var vMM: TMemo;
  791. i: Integer;
  792. begin
  793. if Trim(AText) = '' then
  794. begin
  795. Result := '';
  796. Exit;
  797. end;
  798. vMM := TMemo.Create(nil);
  799. try
  800. vMM.Visible := False;
  801. vMM.WordWrap := True;
  802. vMM.parent := Application.MainForm;
  803. vMM.Width := AWidth;
  804. vMM.Text := AText;
  805. for i := 0 to vMM.Lines.Count - 1 do
  806. begin
  807. if i = 0 then
  808. Result := vMM.Lines[i]
  809. else
  810. Result := Result + #10#13 + vMM.Lines[i];
  811. end;
  812. finally
  813. vMM.Free;
  814. end;
  815. end;
  816. function ReplaceCharsForJson(AText: string): string;
  817. const
  818. BefChar: array [0..7] of Char = ('{', '}', ',', ':', '"', '[', ']', '%');
  819. AftChar: array [0..7] of string = ('{', '}', ',', ':', '"', '【', '】', '♂');
  820. var I: Integer;
  821. begin
  822. AText := Trim(AText);
  823. Result := AText;
  824. if AText = '' then Exit;
  825. for I := low(BefChar) to High(BefChar) do
  826. begin
  827. if Pos(BefChar[I], AText) > 0 then
  828. AText := StringReplace(AText, BefChar[I], AftChar[I], [rfReplaceAll]);
  829. end;
  830. Result := AText;
  831. end;
  832. function RecoverCharsFromJson(AText: string): string;
  833. const
  834. BefStr: array [0..1] of string = ('♂', '\r\n');
  835. AftStr: array [0..1] of string = ('%', '');
  836. var I: Integer;
  837. begin
  838. AText := Trim(AText);
  839. Result := AText;
  840. if AText = '' then Exit;
  841. for I := low(BefStr) to High(BefStr) do
  842. begin
  843. if Pos(BefStr[I], AText) > 0 then
  844. AText := StringReplace(AText, BefStr[I], AftStr[I], [rfReplaceAll]);
  845. end;
  846. Result := AText;
  847. end;
  848. procedure FindFiles(APath, AExtName: string; AFileList: TStrings);
  849. var
  850. SRec: TSearchRec;
  851. retval: Integer;
  852. begin
  853. AFileList.Clear;
  854. retval := FindFirst(APath + AExtName, faAnyFile, sRec);
  855. try
  856. while retval = 0 do
  857. begin
  858. if (SRec.Attr and faDirectory) = 0 then
  859. AFileList.Add(Srec.Name);
  860. retval := FindNext(SRec);
  861. end;
  862. finally
  863. FindClose(SRec);
  864. end;
  865. end;
  866. function DeleteFolder(const FolderStr: string): Boolean;
  867. var
  868. fos: SHFILEOPSTRUCT;
  869. begin
  870. ZeroMemory(@fos, SizeOf(fos));
  871. fos.Wnd := HWND_DESKTOP;
  872. fos.wFunc := FO_DELETE;
  873. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO OR FOF_NOCONFIRMATION;
  874. fos.pFrom := PChar(FolderStr + #0);
  875. Result := SHFileOperation(fos) <> 0;
  876. end;
  877. function HasExt(const AFileName: string): Boolean;
  878. begin
  879. Result := ExtractFileExt(AFileName) <> '';
  880. end;
  881. function CopyFolder(const SrcFolder, DstFolder: string): Boolean;
  882. var
  883. fos: SHFILEOPSTRUCT;
  884. begin
  885. ZeroMemory(@fos, SizeOf(fos));
  886. fos.Wnd := HWND_DESKTOP;
  887. fos.wFunc := FO_COPY;
  888. fos.fFlags := FOF_SILENT OR FOF_ALLOWUNDO;
  889. fos.pFrom := PChar(SrcFolder + #0);
  890. fos.pTo := PChar(DstFolder + #0);
  891. Result := SHFileOperation(fos) <> 0;
  892. end;
  893. function FileCount(APath: string): Integer;
  894. var
  895. vSR: TSearchRec;
  896. iRetval: Integer;
  897. vSL: TStringList;
  898. begin
  899. vSL := TStringList.Create;
  900. iRetval := FindFirst(APath + '*.*', faAnyFile, vSR);
  901. try
  902. while iRetval = 0 do
  903. begin
  904. if (vSR.Attr and faDirectory) = 0 then
  905. vSL.Add(vSR.Name);
  906. iRetval := FindNext(vSR);
  907. end;
  908. Result := vSL.Count;
  909. finally
  910. FindClose(vSR);
  911. vSL.Free;
  912. end;
  913. end;
  914. end.