UtilMethods.pas 26 KB

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