UtilMethods.pas 27 KB

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