curtis
17小時前 0756bf12d10cf1b7f78c571de0a9ad69cbaeb7ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
{ ==============================================================================
  方法名稱:PM401Click
  引用相依:FileExists, GetNoNameCase, LoadFromFile, RenameFile, SaveToFile, Str
            2Dir
  方法描述:影像列表右鍵選單功能:從指定頁面分出新案。確認使用者選取的分案起點(不
            能為第一頁)後,取得新的流水案號並建立目錄。將原案件中該頁碼之後的所有
            影像檔案更名並搬移至新案目錄,同步更新原案與新案的 Context.dat 與 Cas
            eIndex.dat。完成後重新載入影像列表並提示完成。
============================================================================== }
procedure TCB_IMGPSScanX.PM401Click(Sender: TObject);
var
  i : Integer;
  FromIndex : Integer;
  CaseID : String;
  NewPath : String;
  OldName,NewName:String;
  S : TStringlist;
begin
  S := TStringlist.Create;
  try
    FromIndex := PageLv.ItemIndex;
    if FromIndex = 0 then
    begin
      Showmessage(_Msg('不能從第1頁分案'));
      Exit;
    end;
 
    If MessageDlg(Format(_Msg('是否確定從%d頁分出新案'),[PageLV.ItemIndex+1]),Mtconfirmation,[mbyes,mbcancel],0) = mrcancel Then Exit;
    ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
    CaseID := GetNoNameCase(ImageSavePath);
    NewPath := ImageSavePath + CaseID+'\';
    Str2Dir(NewPath);
    for i := FromIndex to ContextList.Count - 1 do
    begin
      OldName := ContextList.Strings[i];
      //NewName := Add_Zoo(S.Count+1,3)+Copy(OldName,4,length(OldName)-3);
      NewName := Add_Zoo(S.Count+1,3)+FileName2NoQuene_Filename(OldName);
 
      ReNameFile(DisplayPath+OldName,NewPath+NewName);
      S.Add(NewName);
      S.SaveToFile(NewPath+'Context.dat');
    end;
    for i := ContextList.Count - 1 downto FromIndex do
    begin
      ContextList.Delete(i);
      ContextList.SaveToFile(DisplayPath+'Context.dat');
    end;
    SetCaseList('I',MyTreeNode1.IndexOf(MyTreeNode2)+1,CaseID);
    if FileExists(DisplayPath+'CaseIndex.dat') then  //把原經辦代號取出來再寫入新件裡
    begin
      S.LoadFromFile(DisplayPath+'CaseIndex.dat');
    end;
    DisplayPath := '';
 
    ClearCaseIndex;
    WriteCaseIndex(NewPath);
  finally
  S.Free;
  end;
  LoadImgFile;
  Showmessage(_Msg('分案完成'));
end;
 
 
{ ==============================================================================
  方法名稱:PM402Click
  引用相依:
  方法描述:將影像列表(PageLV)中的所有項目設為選取狀態。
============================================================================== }
procedure TCB_IMGPSScanX.PM402Click(Sender: TObject);
var
  i : Integer;
begin
  for i := 0 to PageLV.Items.Count - 1 do
  begin
    PageLV.Items.Item[i].Selected := True;
  end;
end;
 
 
{ ==============================================================================
  方法名稱:PM403Click
  引用相依:
  方法描述:將影像列表(PageLV)中的所有項目設為取消選取狀態。
============================================================================== }
procedure TCB_IMGPSScanX.PM403Click(Sender: TObject);
var
  i : Integer;
begin
  for i := 0 to PageLV.Items.Count - 1 do
  begin
    PageLV.Items.Item[i].Selected := False;
  end;
end;
 
 
{ ==============================================================================
  方法名稱:PM404Click
  引用相依:
  方法描述:影像列表右鍵選單功能:文件歸類。開啟 TDocListForm 顯示可用的表單清單供
            使用者選擇。確認後根據當前 TreeView 的選取層級(案件級別、文件級別或特
            定表單),呼叫 PageReplaceFormID 將影像重新歸類至選定的表單類型。歸類完
            成後重新繪製樹狀結構並清空檢核記錄,最後回到原先選取的節點。
============================================================================== }
procedure TCB_IMGPSScanX.PM404Click(Sender: TObject);
var
  i : Integer;
  DocListForm : TDocListForm;
  OldName,NewName,Ext : String;
  FormID,FormName : String;
  PreNode2Name : String;
begin
  PreNode2Name := '';
  if TreeView1.Selected.Parent = MyTreeNode1 then
    PreNode2Name:= GetNode2Name(MyTreeNode2);
  ShowText := _Msg('文件歸類中,請稍候');
  DataLoading(True,True);
  DocListForm := TDocListForm.Create(self);
  try
    InitialLanguage(PatchDlg); //載入多國語言
    for i := 1 to FORM_INF_List.Count - 1 do
    begin
      FormID := GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
      FormName := GetSQLData(FORM_INF_List,'T1.FORM_DESC',i);
      if (FormID <> NowFormCode) and FormIDExists(FormID,True,0) then
      begin
        DocListForm.FormIDList.Add(FormID+'#@#'+FormName);
        With DocListForm.DocLV.Items.Add do
        begin
          Caption := FormID;
          SubItems.Add(FormName);
        end;
      end;
    end;
 
    if DocListForm.ShowModal = mrOk then
    begin
      FormID := DocListForm.DocLV.Selected.Caption;
      if (TreeView1.Selected.Level=1) then
      begin
        PageReplaceFormID(DisplayPath,'ALL',FormID);
      end
      Else if (TreeView1.Selected.Level=2) and (NowFormCode = '') then
        PageReplaceFormID(DisplayPath,'',FormID)
      Else
      begin
        PageReplaceFormID(DisplayPath,NowFormCode,FormID);
      end;
      //DrawDocItem(MytreeNode1,FORM_INF_List,NowCaseno);
      //DrawDocItem1(MytreeNode1,Doc_Inf_List,NowCaseno);  //201408280改
      DrawDocItem2(MytreeNode1,NowCaseno);
      ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
      //Showmessage(_Msg('歸類完成'));  //20101103 User要求拿掉
      if PreNode2Name <> '' then  // 回到原本點選的文件節點上
      begin
        for i := 0 to MyTreeNode1.Count - 1 do
        begin
          if GetNode2Name(MyTreeNode1.Item[i]) = PreNode2Name then
          begin
            TreeView1.Selected := MyTreeNode1.Item[i];
            Break;
          end;
        end;
      end;
      TreeView1click(self);
    end;
  finally
  DocListForm.Free;
  DataLoading(False,False);
  end;
 
end;
 
 
{ ==============================================================================
  方法名稱:PM601Click
  引用相依:CopyFile, DeleteImageFile, DirectoryExists, FileExists, LoadFromFile
            , ReSortFileName, RenameFile, SaveToFile
  方法描述:縮圖瀏覽區右鍵選單功能:文件歸類。針對所有被選取(由 Shape 標記)的影像,
            開啟 TDocListForm 選擇目標表單。核心邏輯包含:判斷目標文件是否需要區分
            份數、自動產生新的文件目錄或沿用既有目錄、根據檔案序號產生新檔名、執行
            檔案複製並更新 ContextList。最後刪除原檔案、重新排序原目錄並重新繪製樹
            狀結構。
============================================================================== }
procedure TCB_IMGPSScanX.PM601Click(Sender: TObject);
var
  i : Integer;
  DocListForm : TDocListForm;
  OldName,NewName,Ext : String;
  FormID,FormName,DocNo,DocDir : String;
  PreNode2Name : String;
  iFormID : String;
  iISBName : String;
  iISB : TImageScrollBox;
  GoAtt : Boolean;
  AttLv : Integer;
  ST1 :TStringList;
begin
  PreNode2Name := '';
  ST1:=TStringList.Create;
  if TreeView1.Selected.Parent = MyTreeNode1 then
    PreNode2Name:= GetNode2Name(MyTreeNode2);
  ShowText := _Msg('文件歸類中,請稍候');
LogFile1.LogToFile(logTimeString+'縮圖  歸類開始');
  DataLoading(True,True);
  GoAtt := False;
  if (MytreeNode2 <> nil) and (Pos('Attach',MyTreeNode2.Text)>0) then
  begin
    AttLv := TreeView1.Selected.Level;
    GoAtt := True;
  end;
  DocListForm := TDocListForm.Create(self);
  try
    InitialLanguage(DocListForm); //載入多國語言
    //InitialLanguage(PatchDlg); //載入多國語言
    DocListForm.CheckBox1.Visible:=False;
    for i := 1 to FORM_INF_List.Count - 1 do
    begin
      FormID :=  GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
      FormName := GetSQLData(FORM_INF_List,'T1.FORM_DESC',i);
      DocNo := GetSQLData(FORM_INF_List,'T1.DOC_NO',i)+GetSQLData(FORM_INF_List,'T1.DOC_VERSION',i);
      if not FormIDAppear(FormID) then Continue;   //20170816 先秀全部
 
      //Showmessage(FORM_INF_List.Text);
      //showmessage(inttostr(FORM_INF_List.Count)+#13+inttostr(self.Doc_Inf_List.Count));
      if (FormID <> FileName2FormCode(DisplayISB.FileName)) and FormIDExists(FormID,False,i) then
      begin
        DocListForm.FormIDList.Add(FormID+'#@#'+FormName);
        With DocListForm.DocLV.Items.Add do
        begin
          Caption := FormID;
          SubItems.Add(FormName);
        end;
      end;
    end;
    if DocListForm.ShowModal = mrOk then
    begin
      for i := 0 to ComponentCount -1 do
      begin
        if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
        begin
          iISBName := ShapeName2PreViewISBName(TShape(Components[i]));
          iISB := TImageScrollBox(FindComponent(iISBName));
          OldName := ExtractFileName(iISB.FileName);
          Ext := ExtractFileExt(OldName);
 
          if DocListForm.CheckBox1.Checked then
          begin
            FormID := DocListForm.Edit1.Text;
            DocNo := GetNewCustomDocNo(DisplayPath,FormID);
          end
          else
          begin
            FormID := DocListForm.DocLV.Selected.Caption;
            DocNo := FormCode2DocNo(FormID);
          end;
 
 
          if DocNoDir2DocNo(Path2DocDir(ExtractFilePath(iISB.FileName),NowCaseno)) = DocNo then
            DocDir := Path2DocDir(ExtractFilePath(iISB.FileName),NowCaseNo)
          Else
            DocDir := FindLastestDocDir(NowCaseno,DocNo);
 
//ShowMessage('DocNoNeedDiv(DocNo)='+BoolToStr(DocNoNeedDiv(DocNo),true));
//ShowMessage('DocDir='+DocDir);
 
          if DocNoNeedDiv(DocNo) then   //要分份數
          begin
            if ((FormCode2Page(FormID) = '01') and (GetDocDir_Page(NowCaseno,DocDir)>0)) or (DocDir = '') then
            begin
              DocDir := DocNo2DocNoDir(ImageSavePath + NowCaseno+'\',DocNo);
            end
            else
            begin //20171016  真對補件影響 所加的判斷
              ST1.Clear;
              if FileExists(ImageSavePath + NowCaseno+'\'+DocDir+'\Context.dat') then
              begin
                ST1.LoadFromFile(ImageSavePath + NowCaseno+'\'+DocDir+'\Context.dat');
                if (ST1.Count > 0) and ISExistImg(ImageSavePath + NowCaseno+'\'+DocDir+'\'+ST1.Strings[0]) then   //20181210 多增加判斷ST1>0 否則會有機會出現List out of bound  Hong
                begin
                  DocDir := DocNo2DocNoDir(ImageSavePath + NowCaseno+'\',DocNo);
                end;
              end;
            end;
          end
          Else        //不分份數
          begin
            if DocNo <> '' then
              DocDir := DocNo
            else      //Attach 附件
              DocDir := DocNo2DocNoDir(ImageSavePath + NowCaseno+'\',DocNo);
          end;
          if (not DirectoryExists(ImageSavePath + NowCaseno+'\'+DocDir+'\')) and (DocDir <> AttName) then
            SetDocNoList('A',-1,NowCaseno,DocDir,'1');
 
          {if DocDir = '' then
          begin
            DocDir := DocNo;
            if DocNoNeedDiv(DocNo) then
              DocDir:=DocNo2DocNoDir(ImageSavePath+NowCaseno+'\',DocNo);
            SetDocNoList('A',-1,NowCaseno,DocDir);
          end; }
LogFile1.LogToFile(logTimeString+'縮圖  FormID='+FormID);
          if Not DirectoryExists(ImageSavePath+NowCaseno+'\'+DocDir) then
            Mkdir(ImageSavePath+NowCaseno+'\'+DocDir);
          ContextList.Clear;
          if FileExists(ImageSavePath+NowCaseno+'\'+DocDir+'\Context.dat') then
            ContextList.LoadFromFile(ImageSavePath+NowCaseno+'\'+DocDir+'\Context.dat');
          NewName := Add_Zoo(ContextList.Count+1,3)+'_'+FormID+Ext;
          CopyFile(PWideChar(iISB.FileName),PwideChar(ImageSavePath+NowCaseno+'\'+DocDir+'\'+NewName),False);
          {ContextList.Add(NewName);
          ContextList.SaveToFile(ImageSavePath+NowCaseno+'\'+DocDir+'\Context.dat'); }
          SetContextList('A',-1,NowCaseNo,DocDir,NewName);
          DeleteImageFile(ExtractFilePath(iISB.FileName),ExtractFileName(iISB.FileName),NowCaseNo);
          //RenameFile(iISB.FileName,ImageSavePath+NowCaseno+'\'+DocDir+'\'+NewName);
          //ReNameContext(iISB.FileName,OldName,NewName);
        end;
      end;
//ShowMessage('KKKK');
      ReSortFileName(ExtractFilePath(iISB.FileName));
      DrawDocItem2(MytreeNode1,NowCaseno);
      ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
 
      if GoAtt then
      begin
        GotoAttach(AttLv);
      end;
      TreeView1click(self);
 
    end;
  finally
  DataLoading(False,False);
  DocListForm.Free;
  ST1.Free;
  end;
 
end;
 
 
{ ==============================================================================
  方法名稱:PM602Click
  引用相依:CopyFile, DeleteImageFile, FileExists, LoadFromFile, ReSortFileName,
             SaveToFile, Str2Dir
  方法描述:縮圖瀏覽區右鍵選單功能:歸類至自定義文件。彈出對話框要求使用者輸入新文
            件名稱,檢核名稱是否重複後產生新的自定義文件編號。接著將所有選取的影像
            複製到新建立的文件目錄下,更新 ContextList並刪除原檔案。最後重新排序並
            刷新樹狀顯示。
============================================================================== }
procedure TCB_IMGPSScanX.PM602Click(Sender: TObject);
var
  FileList:TStringlist;
  SavePath : String;
  DocDir : String;
  CustomDocName : String;
  CustomDocNo : String;
  i : Integer;
  OldName,NewName,Ext : String;
  FormID,FormName,DocNo : String;
  PreNode2Name : String;
  iFormID : String;
  iISBName : String;
  iISB : TImageScrollBox;
  GoAtt : Boolean;
  AttLv : Integer;
begin
  GoAtt := False;
  if (MytreeNode2 <> nil) and (Pos('Attach',MyTreeNode2.Text)>0) then
  begin
    AttLv := TreeView1.Selected.Level;
    GoAtt := True;
  end;
 
  if InputQuery(_Msg('輸入其他文件名稱'),_Msg('文件名稱'),CustomDocName) then
  begin
    if FindCustomDocName(DisplayPath,CustomDocName) then
    begin
      Showmessage(Format(_Msg('文件名稱:"%s"己存在'),[CustomDocName]));
      Exit;
    end;
    CustomDocNo := GetNewCustomDocNo(DisplayPath,CustomDocName);
  end;
  if CustomDocNo = '' then Exit;
  DocDir := CustomDocNo;
LogFile1.LogToFile(logTimeString+'縮圖 歸類自訂文件 DocDir='+DocDir);
  SavePath := ImageSavePath+NowCaseNo+'\'+DocDir+'\';
  Str2Dir(SavePath);
  SetDocNoList('A',-1,NowCaseNo,DocDir,'1');
  FileList := TStringlist.Create;
  try
  FileList.Clear;
  if FileExists(SavePath+'Context.dat') then
    FileList.LoadFromFile(SavePath+'Context.dat');
 
    for i := 0 to ComponentCount -1 do
    begin
      if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
      begin
        iISBName := ShapeName2PreViewISBName(TShape(Components[i]));
        iISB := TImageScrollBox(FindComponent(iISBName));
        OldName := ExtractFileName(iISB.FileName);
        Ext := ExtractFileExt(OldName);
        NewName := Add_Zoo(FileList.Count+1,3)+'_'+GetCustomFormID(ImageSavePath+NowCaseNo+'\',CustomDocNo)+ext;
 
        //Showmessage(iISB.FileName+#13+ImageSavePath+NowCaseno+'\'+DocDir+'\'+NewName);
        CopyFile(PWideChar(iISB.FileName),PwideChar(ImageSavePath+NowCaseno+'\'+DocDir+'\'+NewName),False);
        SetContextList('A',-1,NowCaseno,DocDir,NewName);
        FileList.Add(NewName);
        {FileList.Add(NewName);
        FileList.SaveToFile(ImageSavePath+NowCaseno+'\'+DocDir+'\Context.dat');}
        DeleteImageFile(ExtractFilePath(iISB.FileName),ExtractFileName(iISB.FileName),NowCaseNo);
      end;
    end;
  finally
  FileList.Free;
  end;
  ReSortFileName(ExtractFilePath(iISB.FileName));
  DrawDocItem2(MytreeNode1,NowCaseno);
  ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
 
  if GoAtt then
  begin
    GotoAttach(AttLv);
  end;
  TreeView1click(self);
  MyTreeNode1.Expand(True);
end;
 
 
{ ==============================================================================
  方法名稱:PM604Click
  引用相依:DeskewImg, LoadFromFile, SaveToFile
  方法描述:縮圖瀏覽區右鍵選單功能:自動去偏斜(Deskew)。遍歷所有選取的影像元件,對
            其 Graphic 執行 DeskewImg 操作,重新繪製並將結果存回原檔案。
============================================================================== }
procedure TCB_IMGPSScanX.PM604Click(Sender: TObject);
var
  i : Integer;
  iISBName : String;
  iISB : TImageScrollBox;
begin
  //Showmessage(inttostr(ComponentCount));
  for i := 0 to ComponentCount -1 do
  begin
    if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
    begin
      //Showmessage(Components[i].Name);
      iISBName := ShapeName2PreViewISBName(TShape(Components[i]));
      iISB := TImageScrollBox(FindComponent(iISBName));
      DeskewImg(iISB.Graphic);
      iISB.Redraw(True);
      iISB.SaveToFile(iISB.FileName);
      DisplayISB.LoadFromFile(DisplayISB.FileName,1);
    end;
  end;
  //TreeView1Click(nil);
end;
 
 
{ ==============================================================================
  方法名稱:PM605Click
  引用相依:DeleteImageFile, ReSortFileName
  方法描述:縮圖瀏覽區右鍵選單功能:刪除影像。在使用者確認後,遍歷所有選取的影像元
            件,呼叫 DeleteImageFile 刪除實際檔案。刪除完成後,執行檔案重新排序(ReS
            ortFileName),更新樹狀結構上的頁數統計文字,並刷新顯示。
============================================================================== }
procedure TCB_IMGPSScanX.PM605Click(Sender: TObject);
var
  i : Integer;
  iISBName,OldName : String;
  iISB : TImageScrollBox;
begin
  if MessageDlg(_Msg('是否確定刪除??'),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
 
  for i := 0 to ComponentCount -1 do
  begin
    if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
    begin
      //Showmessage(Components[i].Name);
      iISBName := ShapeName2PreViewISBName(TShape(Components[i]));
      iISB := TImageScrollBox(FindComponent(iISBName));
//ShowMessage('iISB.FileName='+iISB.FileName);
//ShowMessage(ExtractFilePath(iISB.FileName)+','+ExtractFileName(iISB.FileName)+','+NowCaseNo);
//      if (FMode = 'ESCAN') and (FModeName<>'異動件') then
//      begin
//        if ISExistImg(iISB.FileName) then
//        begin
//          ShowMessage(_Msg('此圖為非當次掃瞄,不可刪除'));
//          Exit;
//        end;
//      end;
LogFile1.LogToFile(logTimeString+'縮圖刪除 iISB.FileName='+iISB.FileName);
      DeleteImageFile(ExtractFilePath(iISB.FileName),ExtractFileName(iISB.FileName),NowCaseNo);
    end;
  end;
//ShowMessage('iISB.FileName='+iISB.FileName);
  ReSortFileName(ExtractFilePath(iISB.FileName));
  DrawDocItem2(MytreeNode1,NowCaseno);
  MyTreeNode1.Text := Format(_Msg('%s-%d頁'),[NowCaseno,GetCasePage(ImageSavePath,NowCaseNo)]);
  NewTreeNodeRefresh;
  ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
  TreeView1click(self);
end;
 
 
{ ==============================================================================
  方法名稱:PM101Click
  引用相依:DeleteDocNoFile, DirectoryExists, _DelTree
  方法描述:處理樹狀結構(TreeView)的右鍵刪除選單。根據選取的節點類型(新掃瞄件、案
            件、文件或表單)執行不同範圍的刪除:包含刪除實體目錄、清空影像清單、更新
            案件索引及檢核記錄。針對異動模式(ESCAN),若刪除後無影像則會重建空案件
            以維持結構。
============================================================================== }
procedure TCB_IMGPSScanX.PM101Click(Sender: TObject);
var
  P,v,v1,v2,ln,i : Integer;
  iDocDir,iDocNo : String;
begin
LogFile1.LogToFile(logTimeString+'Tree 按下刪除');
  if TreeView1.Selected = NewTreeNode then  //全刪  //新掃描件
  begin
LogFile1.LogToFile(logTimeString+'Tree 全部刪除');
    If Messagedlg(_Msg('是否刪除所有案件?'),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
    clearView(1);
    Application.ProcessMessages;
    _DelTree(ImageSavePath);
    if (FMode = 'ESCAN') then
    begin
      MkDir(ImageSavePath+FCaseID);
      CreateEmptyCase(ImageSavePath,FCaseID);
    end;
    LoadImgFile;
    Showmessage(_Msg('刪除完成'));
  end
  Else if TreeView1.Selected = MyTreeNode1 then       //案件編號
  begin
LogFile1.LogToFile(logTimeString+'Tree 案件編號刪除 NowCaseno='+NowCaseno);
    If Messagedlg(Format(_Msg('編號(%s)是否刪除?'),[NowCaseno]),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
    clearView(1);
    Application.ProcessMessages;
    if (FMode = 'ESCAN') then
    begin
      for i := 0 to MyTreeNode1.Count - 1 do
      begin
        MyTreenode2 := MyTreeNode1.Item[i];
        v := Posend('{',MyTreenode2.Text);
        v1 := Posend('}',MyTreenode2.Text);
        v2 := posend('-',MyTreenode2.Text);
        ln := length(MyTreenode2.Text);
        iDocDir := Copy(MyTreeNode2.Text,v+1,v1-v-1);
        iDocNo := DocNoDir2DocNo(iDocDir);
        _DelTree(ImageSavePath+NowCaseno+'\'+iDocDir);
        SetUseCase('D',ImageSavePath+NowCaseno+'\',iDocDir,'','');
        SetDocNoList('D',-1,NowCaseNo,iDocDir,'');
        if (Copy(iDocNo,1,5)='ZZZZZ') then   //20140703 刪除自定文件時要刪ini檔資料
          DeleteCustomDocDir(ImageSavePath+NowCaseno+'\',iDocDir);
 
      end
    end
    Else
    begin
      _DelTree(DisplayPath);
      SetCaseList('D',NewTreeNode.IndexOf(MyTreeNode1),'');
    end;
    ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
 
    if (FMode = 'ESCAN') then
    begin
      if not DirectoryExists(ImageSavePath+FCaseID) then
      begin
        MkDir(ImageSavePath+FCaseID);
        CreateEmptyCase(ImageSavePath,FCaseID);
      end;
    end;
    LoadImgFile;
  end
  Else if TreeView1.Selected = MyTreeNode2 then       //文件層
  begin
    If Messagedlg(Format(_Msg('文件(%s)是否刪除?'),[DocNo2DocName(NowCaseno,NowDocNo)]),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
    ClearView(1);
    Application.ProcessMessages;
//ShowMessage(NowDocDir);
LogFile1.LogToFile(logTimeString+'Tree 文件層號刪除 NowDocDir='+NowDocDir);
    if (Length(NowDocDir)=8) or (NowDocDir=AttName) then
    begin
//ShowMessage('DeleteDocNoFileForESCAN');
      DeleteDocNoFileForESCAN(ImageSavePath+NowCaseno+'\'+NowDocDir,NowDocDir);
    end
    else
    begin
      _DelTree(ImageSavePath+NowCaseno+'\'+NowDocDir);
      SetDocNoList('D',-1,NowCaseNo,NowDocDir,'');
    end;
    SetUseCase('D',ImageSavePath+NowCaseno+'\',NowDocDir,'','');
 
    if (Copy(NowDocNo,1,5)='ZZZZZ') then   //20140703 刪除自定文件時要刪ini檔資料
      DeleteCustomDocDir(ImageSavePath+NowCaseno+'\',NowDocDir);
 
    DrawDocItem2(MytreeNode1,NowCaseno);
    MytreeNode1.Text := Format(_Msg('%s-%d頁'),[NowCaseno,GetCasePage(ImageSavePath,NowCaseNo)]);
    ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
    NewTreeNodeRefresh;
 
  end
  Else if TreeView1.Selected = MyTreeNode3 then       //FormID層
  begin
    If Messagedlg(Format(_Msg('文件(%s)是否刪除?'),[NowFormName]),mtconfirmation,[mbyes,mbcancel],0) = mrcancel then Exit;
LogFile1.LogToFile(logTimeString+'Tree FormID層號刪除 NowFormCode='+NowFormCode);
    DeleteFormCodeFile(NowCaseNo,NowDocDir,NowFormCode);
    SetRecordEditedDocDir('A',NowCaseNo,NowDocDir);
    DrawDocItem2(MytreeNode1,NowCaseno);
    MytreeNode1.Text := Format(_Msg('%s-%d頁'),[NowCaseno,GetCasePage(ImageSavePath,NowCaseNo)]);
    ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
    NewTreeNodeRefresh;
 
  end;
end;
 
 
{ ==============================================================================
  方法名稱:PM102Click
  引用相依:DirectoryExists, RenameFile
  方法描述:處理「修改案件編號」右鍵選單。彈出輸入盒要求輸入新編號並驗證長度與是否
            重複。確認修改後,先清空當前影像顯示,接著執行磁碟目錄更名並更新案件清
            單文字。最後重新繪製該案件的文件樹狀結構並提示完成。
============================================================================== }
procedure TCB_IMGPSScanX.PM102Click(Sender: TObject);
var
  NewCaseID,ShowNewCaseID,ShowNowCaseID : String;
  i,P,v : Integer;
  InputOk : Boolean;
begin
  VMode := 0;
  GoViewMode;
  ISB1.ZoomMode := zmFitWidth;
 
  NewCaseID := InputBox(_Msg('修改案件編號'),_Msg('新案件編號'),'');
  ShowNewCaseID := NewCaseID;
  ShowNowCaseID := NowCaseno;
 
  if NewCaseID = '' then Exit;
 
  IF Length(NewCaseID)<>CaseIDLength Then
  begin
    Showmessage(_Msg('輸入格式錯誤'));
    Exit;
  end;
 
  if DirectoryExists(ImageSavePath+NewCaseID) then
  begin
    Showmessage(NewCaseID+_Msg('己存在,無法修改'));
    Exit;
  end;
 
  if Messagedlg(Format(_Msg('是否將%s改為%s'),[ShowNowCaseID,ShowNewCaseID]),Mtconfirmation,[mbyes,mbcancel],0) = mrcancel then
    Exit;
  ClearView(1);
  RenameFile(ImageSavePath+NowCaseno,ImageSavePath+NewCaseID);
  SetCaseList('E',NewTreeNode.IndexOf(MyTreeNode1),NewCaseID);
 
  //P := ContextList.Count;
  MytreeNode1.Text := Format(_Msg('%s-%d頁'),[NewCaseID,GetCasePage(ImageSavePath,NewCaseID)]);
  //DrawDocItem(MyTreeNode1,FORM_INF_List,NewCaseID);
  DrawDocItem2(MytreeNode1,NewCaseID);
  Showmessage(_Msg('修改完成'));
end;
 
 
{ ==============================================================================
  方法名稱:PM103Click
  引用相依:
  方法描述:處理樹狀結構的「掃瞄/追加掃瞄」右鍵選單。根據選取的節點層級,決定是啟動
            全新的案件掃瞄程序(NewScanBtnClick)或是針對現有案件進行追加掃瞄(AddS
            canBtnclick),並在日誌中記錄操作行為。
============================================================================== }
procedure TCB_IMGPSScanX.PM103Click(Sender: TObject);
begin
  if TreeView1.Selected = nil then Exit;
 
  {if Treeview1.Selected = NewTreeNode then
  begin
    ShowMessage('AAAAA');
  end;
  if Treeview1.Selected = MyTreeNode1 then
  begin
    ShowMessage('BBBBB');
  end;
 
  if Treeview1.Selected = MyTreeNode2 then
  begin
    ShowMessage('CCCCC');
  end;
 
  if Treeview1.Selected = MyTreeNode3 then
  begin
    ShowMessage('DDDDD');
  end;
  }
  if (Treeview1.Selected = NewTreeNode) {or (Treeview1.Selected = MyTreeNode1)} then
  begin
//ShowMessage('NewScanBtnClick');
LogFile1.LogToFile(logTimeString+'Tree NewScanBtnClick');
    NewScanBtnClick(self)
  end
  Else
  begin
//ShowMessage('AddScanBtnclick');
LogFile1.LogToFile(logTimeString+'Tree AddScanBtnclick');
    AddScanBtnclick(self);
  end;
end;
 
 
 
{ ==============================================================================
  方法名稱:PM104Click
  引用相依:CheckNeedCrop, ConvertToBW, ConvertToGray, CropImg, DeskewImg, Direc
            toryExists, FJpgCompression, FileExists, FindFirst, GetNoNameCase, L
            oadFromFile, MpsGetBarcode, Rotate, SaveQuality, SaveToFile, Str2Dir
            , TJpegGraphic, TTiffGraphic, _DelTree, ifBlackWhite, ifColor25, ifG
            ray256, ifTrueColor, tcGroup4, tcJpeg
  方法描述:處理「匯入影像檔案」右鍵選單。開啟檔案對話框選取 TIF/JPG/PNG 檔,並檢查
            檔案大小是否超過限制。核心流程包含:計算總頁數、逐頁載入、執行自動去偏斜
            (Deskew)、條碼辨識以判斷 FormID、處理 A3 切圖(左右分割)、將影像轉換為對
            應格式(黑白轉 TIF G4,彩色/灰階轉 JPG)並寫入磁碟,最後更新索引、樹狀結
            構與頁數統計。
============================================================================== }
Procedure TCB_IMGPSScanX.PM104Click(Sender: TObject);
Var
  i, n, m, ii, P, v, v1, page, imageCount: Integer;
  FName: String;
  CaseID, DocNo, FormID: String;
  DocDir: String;
  SavePath, SaveFilename: String;
  ISB: TImageScrollBox;
  FileRec: TSearchrec;
 
  iGraphic, iGraphic_First, iGraphic_sec: TTiffGraphic;
  iRect : TRect;
  JpgGr : TJpegGraphic;
  SaveStream     : TFileStream;
  SaveStreamA:TFileStream;
  SaveStreamB:TFileStream;
  cooom:integer;
Begin
  OpenDialog1.Filter := 'Image files|*.TIF;*.JPG;*.PNG';
  If OpenDialog1.Execute Then
  Begin
    ISB := TImageScrollBox.Create(self);
try
    ShowText := _Msg('檔案加入中,請稍候');
LogFile1.LogToFile(logTimeString+'檔案加入中開始');
    DataLoading(True, True);
    If TreeView1.Selected = Nil Then
      Exit;
    FName := OpenDialog1.FileName;
 
    FindFirst(FName, faAnyfile, FileRec);
 
    If FFileSizeLimit = 0 Then
    Begin
      FFileSizeLimit := 5 * 1024;
    End;
//FFileSizeLimit:=20*5*1024;
//ShowMessage(IntToStr(FileRec.Size)+','+IntToStr(FFileSizeLimit * 1024));
    If FileRec.Size > FFileSizeLimit * 1024 Then // 檢查檔案大小
    Begin
      ShowMessage(Format(_Msg('目前檔案大小為 %.3f MB,已超過單一檔案匯入限制%1.f MB'),[FileRec.Size / (1024*1024),FFileSizeLimit/1024]));
      {ShowMessage(Format('目前檔案大小為 %.3f MB', [FileRec.Size / (1024*1024)]) +
        ',已超過單一檔案匯入限制'+Format('%.1f',[FFileSizeLimit/1024])+'MB');}
      FindClose(FileRec);
      DataLoading(false, false);
      Exit;
    End;
    //MessageDlg()
    //cooom:=StrToInt(InputBox('輸入百分比','輸入百分比',''));
    cooom:=FJpgCompression;//20171211彩色tif採jpg壓縮的比例
 
    FindClose(FileRec);
    CaseID := NowCaseno;
    imageCount := 0;
    P := ISB.ImageCountFromFile(OpenDialog1.FileName);
    For i := 1 To P Do
    Begin
      ShowText := Format(_Msg('檔案加入中,請稍候(%d/%d)'),[i,p]);
      //ShowText := _Msg('檔案加入中,請稍候')+'(' + inttostr(i) + '/' + inttostr(P) + ')';
      DataLoading(True, True);
      ISB.LoadFromFile(FName, i);
      DeskewImg(ISB.Graphic);
 
 
      ISB_BW.Graphic.Assign(ISB.Graphic); //20180104
      If ISB.Graphic.ImageFormat <> ifBlackWhite Then   //20180104
      begin
        ConvertToBW(ISB_BW.Graphic);
      end;
      ///ISB_BW.SaveToFile('KKKKKKKK.tif');
 
      iGraphic_First := TTiffGraphic.Create;
      iGraphic_sec := TTiffGraphic.Create;
 
 
      //ShowMessage(IntToStr(iGraphic_First.Palette.palNumEntries)); //彩色 會為0  黑白 為2
 
      MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf); //判斷A3 有用FormID 所以要先辨條碼
      For n := 1 To MpsBarcodeinf.Count Do
      Begin
        If (MpsBarcodeinf.r180[n] <> 0) and (Length(MpsBarcodeinf.Text[n])=FormIDLength) Then // 依條碼角度轉影像
        Begin
          Rotate(ISB.Graphic, MpsBarcodeinf.r180[n]);
          //MpsGetBarcode(iGraphic_First, MpsBarcodeinf);
          Break;
        End;
      End;
 
      iGraphic_First.Assign(ISB.Graphic);
      //有必要的話先把影像轉正 再開始切圖
      If CheckNeedCrop(iGraphic_First) Then
      Begin
        // 先取右邊的影像
        iRect.Left := ISB.Graphic.Width Div 2;
        iRect.Right := ISB.Graphic.Width;
        iRect.Top := 0;
        iRect.Bottom := ISB.Graphic.Height;
        CropImg(iGraphic_First, iRect);
 
        iGraphic_sec.Assign(ISB.Graphic);
        // 再取左邊的影像
        iRect.Left := 0;
        iRect.Right := ISB.Graphic.Width Div 2;
        iRect.Top := 0;
        iRect.Bottom := ISB.Graphic.Height;
        CropImg(iGraphic_sec, iRect);
      End;
      ISB.Graphic.Clear;  //20220711  Hong 覺得ISB後面沒有到了,先清掉減少記憶體使用
      iGraphic := iGraphic_First;
 
      While Not iGraphic.IsEmpty Do
      Begin
        If (TreeView1.Selected = NewTreeNode) Or
          (TreeView1.Selected = MyTreeNode1) Then
        Begin
          SaveFilename := '';
          ISB_BW.Graphic.Assign(iGraphic); //20180104
          If iGraphic.ImageFormat <> ifBlackWhite Then   //20180104
          begin
            ConvertToBW(ISB_BW.Graphic);
          end;
          MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
//ShowMessage(IntToStr(MpsBarcodeinf.Count));
          For n := 1 To MpsBarcodeinf.Count Do
          Begin
            If (MpsBarcodeinf.r180[n] <> 0) and (Length(MpsBarcodeinf.Text[n])=FormIDLength) Then // 依條碼角度轉影像
            Begin
              Rotate(iGraphic, MpsBarcodeinf.r180[n]);
              MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
              Break;
            End;
          End;
//ShowMessage('XXX '+IntToStr(MpsBarcodeinf.Count));
          FormID := BarCode2FormID;
//ShowMessage('FormID='+FormID);
          // 取出FormID
          SaveFilename := FormID;
          If (TreeView1.Selected = NewTreeNode) Then
          Begin
            If FindDivFormCode(FormID) Then // 只找分案頁上的案件條碼
            Begin
              imageCount := 0;
              ClearView(1);
              ContextList.Clear;
              CaseID := BarCode2CaseID;
              If DirectoryExists(ImageSavePath + CaseID + '\') Then
              Begin
                _DelTree(ImageSavePath + CaseID + '\');
                SetCaseList('D', -1, CaseID);
              End;
            End;
            If CaseID = '' Then
            Begin
              CaseID := GetNoNameCase(ImageSavePath);
              ContextList.Clear;
            End;
          End;
          SavePath := ImageSavePath + CaseID + '\';
          Str2Dir(SavePath);
 
          DocNo := FormCode2DocNo(FormID);
          DocDir := FindLastestDocDir(CaseID, DocNo);
          if (FMode='ESCAN') and (FModeName=_Msg('補件掃描')) then
          begin
            DocDir := FindLastestDocDirForPage(CaseID, DocNo,FormID);
//ShowMessage('DocDir='+DocDir);
          end;
 
          If DocNoNeedDiv(DocNo) Then // 要分份數
          Begin
            If ((FormCode2Page(FormID) = '01') And
              (GetDocDir_Page(CaseID, DocDir) > 0)) Or (DocDir = '') Then
              begin
                DocDir := DocNo2DocNoDir(ImageSavePath + CaseID + '\', DocNo);
              end;
          End
          Else // 不分份數
          Begin
            If DocNo <> '' Then
              DocDir := DocNo
            Else // Attach 附件
              DocDir := DocNo2DocNoDir(ImageSavePath + CaseID + '\', DocNo);
          End;
 
          If (Not DirectoryExists(ImageSavePath + CaseID + '\' + DocDir + '\'))
            And (DocDir <> AttName) Then
            SetDocNoList('A', -1, CaseID, DocDir, '1');
          SavePath := ImageSavePath + CaseID + '\' + DocDir + '\';
          Str2Dir(SavePath);
          ContextList.Clear;
          If FileExists(SavePath + 'Context.dat') Then
            ContextList.LoadFromFile(SavePath + 'Context.dat');
 
          WriteCaseIndex(ImageSavePath + CaseID + '\');
          // 寫入案件索引
          If SaveFilename = '' Then // 附件
            SaveFilename := Add_Zoo(ContextList.Count + 1, 3) + ext
          Else
            SaveFilename := Add_Zoo(ContextList.Count + 1, 3) + '_' +
              SaveFilename + ext;
 
          For n := 1 To MpsBarcodeinf.Count Do
          Begin
            If (MpsBarcodeinf.r180[n] <> 0) and (Length(MpsBarcodeinf.Text[n])=FormIDLength) Then // 依條碼角度轉影像
            Begin
              Rotate(iGraphic, MpsBarcodeinf.r180[n]);
              MpsGetBarcode(ISB_BW.Graphic, MpsBarcodeinf);
              Break;
            End;
          End;
//ShowMessage(IntToStr(iGraphic.Palette.palNumEntries));
          if iGraphic.ImageFormat = ifBlackWhite then   //20200806 出現無法匯入,是因color256無法壓JPEG,待報會後再開啟
          begin
            SaveFilename := changefileext(SaveFilename,'.tif');    //20240320 Hong 調整黑白存tif
            iGraphic.Compression:=tcGroup4;
          end
          else if iGraphic.ImageFormat= ifColor256 then
          begin
            SaveFilename := changefileext(SaveFilename,'.jpg');    //20240320 Hong 調整Color256存jpg
            ConverttoGray(iGraphic);
            iGraphic.Compression:=tcJPEG;
            iGraphic.JpegQuality:=cooom;
          end
          else if (iGraphic.ImageFormat = ifTrueColor) or (iGraphic.ImageFormat = ifGray256) then
          begin
            SaveFilename := changefileext(SaveFilename,'.jpg');    //20240320 Hong 調整彩色灰階存jpg
            iGraphic.Compression:=tcJPEG;
            iGraphic.JpegQuality:=cooom;
          end
          else
          begin
            iGraphic.Compression:=tcLZW;
          end;
 
          {if (iGraphic.Palette.palNumEntries = 0) or (iGraphic.Palette.palNumEntries = 256) then  //20171130 彩色 會為0  黑白 為2  灰階256     //20200806拿掉
          begin
            iGraphic.Compression:=tcJPEG;
            iGraphic.JpegQuality:=cooom;
          end;}
 
          If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.tif' Then
          Begin
            If FileExists(SavePath + SaveFilename) Then
              SaveStream := TFileStream.Create(SavePath + SaveFilename, fmOpenReadWrite)
            Else
              SaveStream := TFileStream.Create(SavePath + SaveFilename, fmCreate);
            Try
              SaveStream.Seek(0, soFromBeginning);
              iGraphic.AppendToStream(SaveStream);
            Finally
              SaveStream.Free;
            End;
          End
          Else If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.jpg' Then
          Begin
            If FileExists(SavePath + SaveFilename) Then
              DeleteFile(SavePath + SaveFilename);
            // SaveStream := TFileStream.Create( PEFileName ,fmCreate );
            JpgGr := TJpegGraphic.Create;
            Try
              JpgGr.Assign(iGraphic);
              JpgGr.SaveQuality := 30;
              // JpgGr.AppendToStream(SaveStream);
              JpgGr.SaveToFile(SavePath + SaveFilename);
            Finally
              JpgGr.Free;
              // SaveStream.Free;
            End;
          End;
 
          SetContextList('A', -1, CaseID, DocDir, SaveFilename);
          If (TreeView1.Selected = NewTreeNode) Then
          Begin
            If imageCount = 0 Then
            Begin
              SetCaseList('A', -1, CaseID);
              MyTreeNode1 := TreeView1.Items.AddChild(NewTreeNode, CaseID);
              MyTreeNode1.ImageIndex := 2;
              MyTreeNode1.SelectedIndex := 2;
              Application.ProcessMessages;
            End;
          End;
          inc(imageCount);
          // DrawDocItem1(MytreeNode1,Doc_Inf_List,CaseID);
          // DrawDocItem(MyTreeNode1,FORM_INF_List,CaseID);
        End
        Else If TreeView1.Selected = MyTreeNode3 Then
        Begin
          SavePath := ImageSavePath + CaseID + '\' + NowDocDir + '\';
          ContextList.Clear;
          If FileExists(ImageSavePath + CaseID + '\' + NowDocDir +
            '\Context.dat') Then
            ContextList.LoadFromFile(ImageSavePath + CaseID + '\' + NowDocDir +
              '\Context.dat');
          If NowFormCode <> '' Then
            SaveFilename := Add_Zoo(ContextList.Count + 1, 3) + '_' +
              NowFormCode + ext
          Else
            SaveFilename := Add_Zoo(ContextList.Count + 1, 3) + ext;
 
          For n := 1 To MpsBarcodeinf.Count Do
          Begin
            If MpsBarcodeinf.r180[n] <> 0 Then // 依條碼角度轉影像
            Begin
              Rotate(iGraphic, MpsBarcodeinf.r180[n]);
              MpsGetBarcode(iGraphic, MpsBarcodeinf);
              Break;
            End;
          End;
 
          if (iGraphic.Palette.palNumEntries = 0) or (iGraphic.Palette.palNumEntries = 256) then  //20171130 彩色 會為0  黑白 為2
          begin
            iGraphic.Compression:=tcJPEG;
            iGraphic.JpegQuality:=cooom;
          end;
 
          If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.tif' Then
          Begin
            If FileExists(SavePath + SaveFilename) Then
              SaveStream := TFileStream.Create(SavePath + SaveFilename,
                fmOpenReadWrite)
            Else
              SaveStream := TFileStream.Create(SavePath + SaveFilename,
                fmCreate);
            Try
              SaveStream.Seek(0, soFromBeginning);
              iGraphic.AppendToStream(SaveStream);
            Finally
              SaveStream.Free;
            End;
          End
 
          Else
          Begin
            If LowerCase(ExtractFileExt(SavePath + SaveFilename)) = '.jpg' Then
            Begin
              If FileExists(SavePath + SaveFilename) Then
                DeleteFile(SavePath + SaveFilename);
              // SaveStream := TFileStream.Create( PEFileName ,fmCreate );
              JpgGr := TJpegGraphic.Create;
              Try
                JpgGr.Assign(iGraphic);
                JpgGr.SaveQuality := cooom;
                // JpgGr.AppendToStream(SaveStream);
                JpgGr.SaveToFile(SavePath + SaveFilename);
              Finally
                JpgGr.Free;
                // SaveStream.Free;
              End;
            End;
 
          End;
          // ISB.SaveToFile(SavePath+SaveFilename);
          ContextList.Add(SaveFilename);
          ContextList.SaveToFile(SavePath + 'Context.dat');
        End;
 
        if iGraphic = iGraphic_First then
          iGraphic := iGraphic_Sec
        else
          iGraphic.Assign(nil);
 
      End //While 結束
 
    End;
    ClearErrini(CaseID, MyTreeNode1);
    // 清掉檢核記錄
    If (TreeView1.Selected = MyTreeNode1) Or
      (TreeView1.Selected = NewTreeNode) Then
    Begin
      LoadImgFile;
    End
    Else
    Begin
      DrawDocItem2(MyTreeNode1, CaseID);
      // 長出文件名稱的樹並傳回是否有申請書的影像
      page := GetCasePage(ImageSavePath, CaseID);
      // ShowMessage('page='+IntToStr(page));
      MyTreeNode1.Text := Format(_Msg('%s-%d頁'), [CaseID, page]);
    End;
    // ShowMessage('AAAA');
    NewTreeNodeRefresh;
    Application.ProcessMessages;
    DataLoading(false, false);
 
 
finally
ISB.Free;
iGraphic_First.Free;
iGraphic_sec.Free;
end;
 
 
  End;
End;
 
 
 
 
{ ==============================================================================
  方法名稱:PM106Click
  引用相依:CopyFile, LoadFromFile, SaveToFile
  方法描述:處理「複製文件」右鍵選單。開啟對話框供使用者選擇來源文件與目標案件。使用
            者確認後,遍歷目標案件並將符合條件的來源影像複製到目標目錄下,同步產生
            新的序號檔名、更新目標案件的 Context.dat 並清空其檢核記錄。
============================================================================== }
procedure TCB_IMGPSScanX.PM106Click(Sender: TObject);
var
  i,n,x,v,v1 : Integer;
  CopyFormID,Copy2Caseno,CopyFileName : String;
  S : TStringlist;
begin
  ShowText := _Msg('複製文件中,請稍候');
  DataLoading(True,True);
  DocCopyForm := TDocCopyForm.Create(Self);
  S := TStringlist.Create;
  try
    InitialLanguage(DocCopyForm); //載入多國語言
    DocCopyForm.CopyFromGB.Caption := NowCaseno+DocCopyForm.CopyFromGB.Caption;
    IF NewTreenode.Count = 1 Then
    begin
      Showmessage(_Msg('沒有其他可複製的文件'));
      Exit;
    end;
    For i := 0 to MyTreeNode1.Count -1 do
    begin
      v := Pos('-',MyTreeNode1.Item[i].Text);
      v1 := pos('{',MyTreeNode1.Item[i].Text);
      if V1 > 0 then
      begin
        CopyFormID := Copy(MyTreeNode1.Item[i].Text,1,v-1);
        DocCopyForm.CheckListBox1.Items.Add(CopyFormID);
      end;
    end;
    For i := 0 to NewTreenode.Count -1 do
    begin
      v := Posend('-',NewTreeNode.Item[i].Text);
      Copy2Caseno := Copy(NewTreeNode.Item[i].Text,1,v-1);
      IF Copy2Caseno <> NowCaseno Then
      begin
        DocCopyForm.CheckListBox2.Items.Add(Copy2Caseno);
      end;
    end;
 
    if DocCopyForm.ShowModal = mrok then
    begin
      If MessageDlg(_Msg('是否確定要將勾選的文件複製到勾選的編號裡?'),MtConfirmation,[Mbyes,mbcancel],0) = mrCancel Then
        Exit;
      ShowText := _Msg('複製中,請稍候');
      DataLoading(True,True);
      For i := 0 to DocCopyForm.CheckListBox2.Count -1 do
      begin
        IF DocCopyForm.CheckListBox2.Checked[i] Then
        begin
          S.Clear;
          Copy2Caseno := DocCopyForm.CheckListBox2.Items.Strings[i];
          ClearErrini(Copy2Caseno,MyTreeNode1);  //清掉檢核記錄
          S.LoadFromFile(ImageSavePath + Copy2Caseno +'\Context.dat');
          For n := 0 to DocCopyForm.CheckListBox1.Count -1 do  //文件
          begin
            If DocCopyForm.CheckListBox1.Checked[n] Then
            begin
              //v:= Posend('{',DocCopyForm.CheckContextList.Strings[n]);
              //v1 := Posend('}',DocCopyForm.CheckContextList.Strings[n]);
              //CopyFormID := Copy(DocCopyForm.CheckContextList.Strings[n],1,v-1);
              CopyFormID := DocCopyForm.CheckListBox1.Items.Strings[n];
              IF v = 0 Then
                CopyFormID := '';
              For x := 0 to ContextList.Count -1 do
              begin
                //Showmessage(CopyFormCode);
                //IF CopyFormID <> '' then   //有文件代號
               // begin
                  if FileName2FormCode(ContextList.Strings[x])=CopyFormID  then
                  begin
                    //CopyFileName := Add_Zoo(S.Count+1,3)+ Copy(ContextList.Strings[x],4,length(ContextList.Strings[x])-3);
                    CopyFileName := Add_Zoo(S.Count+1,3)+ FileName2NoQuene_Filename(ContextList.Strings[x]);
                    CopyFile(PWideChar(DisplayPath+ContextList.Strings[x]),PWidechar(ImageSavePath + Copy2Caseno+'\'+CopyFileName),False);
                    S.Add(CopyFileName);
                    S.SaveToFile(ImageSavePath + Copy2Caseno +'\Context.dat');
                  end;
               // end
 
              end;
            end;
          end;
 
        end;
      end;
      DataLoading(False,False);
      Showmessage(_Msg('複製完成!!'));
      LoadImgFile;
    end;
  finally
  DocCopyForm.Free;
  DataLoading(False,False);
  S.Free;
  end;
end;
 
 
{ ==============================================================================
  方法名稱:PM107Click
  引用相依:
  方法描述:觸發右鍵選單中的備註點擊事件。
============================================================================== }
procedure TCB_IMGPSScanX.PM107Click(Sender: TObject);
begin
  WNoteBtnClick(nil);
end;
 
 
{ ==============================================================================
  方法名稱:PM108Click
  引用相依:
  方法描述:處理樹狀結構的「文件歸類」右鍵選單。開啟對話框顯示表單清單,使用者可選擇
            歸類至既有文件或新增自定義文件。若為自定義,則驗證名稱後取得新編號。隨
            後執行 FormIDReplace 搬移影像檔案、更新索引、清空檢核記錄並刷新樹狀結
            構顯示。
============================================================================== }
procedure TCB_IMGPSScanX.PM108Click(Sender: TObject);
var
  i : Integer;
  DocListForm : TDocListForm;
  OldName,NewName,Ext : String;
  NewFormID,NewFormName,CustomDocNo : String;
begin
  DocListForm := TDocListForm.Create(self);
  try
LogFile1.LogToFile(logTimeString+'Tree 歸類開始');
    InitialLanguage(DocListForm); //載入多國語言
    DocListForm.CheckBox1.Visible:=True;
    for i := 1 to FORM_INF_List.Count - 1 do
    begin
      NewFormID := GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
      NewFormName := GetSQLData(FORM_INF_List,'T1.FORM_DESC',i);
      if not FormIDAppear(NewFormID) then Continue;   //20170816 先秀全部的
      if (NewFormID <> FileName2FormCode(DisplayISB.FileName)) and FormIDExists(NewFormID,False,i) then
      begin
        DocListForm.FormIDList.Add(NewFormID+'#@#'+NewFormName);
        With DocListForm.DocLV.Items.Add do
        begin
          Caption := NewFormID;
          SubItems.Add(GetSQLData(FORM_INF_List,'T1.FORM_DESC',i));
        end;
      end;
    end;
    if DocListForm.ShowModal = mrOk then
    begin
      if not (DocListForm.DocLV.Selected=nil) then
         NewFormID := DocListForm.DocLV.Selected.Caption;
      if DocListForm.CheckBox1.Checked then
      begin
        //歸類到自訂文件
        NewFormID:=DocListForm.Edit1.Text;
 
        if Trim(DocListForm.Edit1.Text)='' then
        begin
          Showmessage(_Msg('未輸入文件名稱'));
          Exit;
        end;
        if FindCustomDocName(DisplayPath,NewFormID) then
        begin
          Showmessage(Format(_Msg('文件名稱:"%s"己存在'),[NewFormID]));
          Exit;
        end;
        if NowFormCode <> AttName then
        begin
          If Messagedlg(Format(_Msg('是否將"%s"的所有影像歸類成"%s"'),[FormCode2FormName(NowCaseNo,NowFormCode),NewFormID]),MtConfirmation,[mbyes,mbcancel],0) = mrcancel Then
            Exit;
        end
        Else
        begin
          If Messagedlg(Format(_Msg('是否將"%s"的所有影像歸類成"%s"'),[_Msg('附件')+MyTreeNode3.Text,FormCode2FormName(NowCaseNo,NewFormID)]),MtConfirmation,[mbyes,mbcancel],0) = mrcancel Then
            Exit;
        end;
        CustomDocNo := GetNewCustomDocNo(DisplayPath,NewFormID);
//ShowMessage('CustomDocNo='+CustomDocNo);
LogFile1.LogToFile(logTimeString+'Tree 歸類到自訂文件 CustomDocNo='+CustomDocNo);
        ShowText := _Msg('歸類中,請稍侯');
        DataLoading(True,True);
        FormIDReplace(NowCaseNo,NowDocDir,NowFormCode,CustomDocNo+'010101A');
      end
      else
      begin
        //歸類到既有文件
        if NowFormCode <> AttName then
        begin
          If Messagedlg(Format(_Msg('是否將"%s"的所有影像歸類成"%s"'),[FormCode2FormName(NowCaseNo,NowFormCode),FormCode2FormName(NowCaseNo,NewFormID)]),MtConfirmation,[mbyes,mbcancel],0) = mrcancel Then
            Exit;
        end
        Else
        begin
          If Messagedlg(Format(_Msg('是否將"%s"的所有影像歸類成"%s"'),[_Msg('附件')+MyTreeNode3.Text,FormCode2FormName(NowCaseNo,NewFormID)]),MtConfirmation,[mbyes,mbcancel],0) = mrcancel Then
            Exit;
        end;
        ShowText := _Msg('歸類中,請稍侯');
        DataLoading(True,True);
//ShowMessage('NowDocDir='+NowDocDir);
LogFile1.LogToFile(logTimeString+'Tree 歸類到既有文件 NewFormID='+NewFormID);
        FormIDReplace(NowCaseNo,NowDocDir,NowFormCode,NewFormID);
 
      end;
      ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
      DrawDocItem2(MytreeNode1,NowCaseno);
      DataLoading(False,False);
      TreeView1.Selected := MyTreeNode1;
      TreeView1Click(self);
    end;
 
  finally
  DocListForm.Free;
  end;
 
end;
 
 
{ ==============================================================================
  方法名稱:PM109Click
  引用相依:SaveToFile
  方法描述:處理「案件 OMR 檢核」右鍵選單。清空當前顯示並開啟檢核進度,呼叫 OMRCheck
            Case 對案件執行光學劃記辨識檢核。若檢核成功則建立 OMRCheckOk.dat 標記
            檔,最後重新載入影像、刷新樹狀結構並提示檢核完成。
============================================================================== }
procedure TCB_IMGPSScanX.PM109Click(Sender: TObject);
var
  S : TStringlist;
  CaseID : String;
begin
  //if TreeView1.Selected = nil then Exit;
  //if TreeView1.Selected = NewTreeNode then Exit;
  CaseID := NowCaseno;
  S := TStringlist.Create;
  try
    ClearView(1);
    ShowText := CaseID+_Msg('檢核中,請稍候');
    DataLoading(True,True);
    ShowText := CaseID+_Msg('檢核中,請稍候');
    DataLoading(True,True);
    If OMRCheckCase(CaseID) then  //有成功
    begin
      S.Add('Y');
      S.SaveToFile(ImageSavePath+CaseID+'\OMRCheckOk.dat');
    end;
      //MyTreeNode2ReFresh(CaseID);
    LoadImgFile;
    TreeView1Click(nil);
    DataLoading(False,False);
  finally
  S.Free;
  end;
  Showmessage(_Msg('檢核完成'));
end;
 
 
{ ==============================================================================
  方法名稱:PM110Click
  引用相依:Str2Dir
  方法描述:處理「新增其他文件」右鍵選單。彈出對話框要求輸入自定義名稱,驗證無誤後產
            生新的文件編號並在案件目錄下建立實體子目錄。隨後將新目錄加入清單並重
            新繪製樹狀結構,最後自動展開新建立的節點。
============================================================================== }
procedure TCB_IMGPSScanX.PM110Click(Sender: TObject);
var
  CustomDocName : String;
  CustomDocNo : String;
  DocDir : String;
  SavePath : String;
  ST1:TStringList;
begin
  if InputQuery(_Msg('輸入其他文件名稱'),_Msg('文件名稱'),CustomDocName) then
  begin
    if CustomDocName <> '' then
    begin
      if FindCustomDocName(DisplayPath,CustomDocName) then
      begin
        Showmessage(Format('文件名稱:"%s"己存在',[CustomDocName]));
        Exit;
      end;
      ST1:=TStringList.Create;
      CustomDocNo := GetNewCustomDocNo(DisplayPath,CustomDocName);
      DocDir := CustomDocNo;
      SavePath := ImageSavePath+NowCaseNo+'\'+DocDir+'\';
      Str2Dir(SavePath);
      SetDocNoList('A',-1,NowCaseNo,DocDir,'1');
      DrawDocItem2(MytreeNode1,NowCaseno);
      MyTreeNode1.Expand(True);
    end;
  end;
 
end;
 
 
{ ==============================================================================
  方法名稱:PM111Click
  引用相依:
  方法描述:處理「修改文件份數」右鍵選單。取得當前份數並供使用者修改(範圍 1-9999)。
            若份數有變動則執行驗證(如分份文件限制),確認後更新 SetDocDirCopies 設
            定並標記文件已編輯,最後刷新樹狀結構統計。
============================================================================== }
procedure TCB_IMGPSScanX.PM111Click(Sender: TObject);
var
  oldCopies,NewCopies : Integer;
  copies : String;
begin
  oldCopies := GetDocDirCopies(NowCaseno,NowDocDir);
  try
    NewCopies := Strtoint(inputBox(_Msg('修改份數'),_Msg('請輸入修改後的份數'),inttostr(oldCopies)));
  except
    Showmessage(_Msg('輸入錯誤'));
    Exit;
  end;
  if (NewCopies <= 0) and (NewCopies >= 10000) then
  begin
    Showmessage(_Msg('輸入範圍錯誤'));
    Exit;
  end;
 
  if (oldCopies <> NewCopies) and (NewCopies > 0) and (NewCopies < 10000) then
  begin
    if DocNoNeedDiv(NowDocNo) and (NewCopies = 1) and (MessageDlg(_Msg('修改至1份後此文件將無法再進行份數修改,是否確定??'),mtConfirmation,[mbyes,mbcancel],0)= mrcancel) then
      Exit;
 
    SetDocDirCopies(NowCaseno,NowDocDir,NewCopies);
    SetRecordEditedDocDir('A',NowCaseNo,NowDocDir);
    DrawDocItem2(MytreeNode1,NowCaseno);
    Showmessage(_Msg('修改完成'));
  end;
 
end;
 
 
{ ==============================================================================
  方法名稱:PM301Click
  引用相依:ifBlackWhite
  方法描述:將掃瞄模式設定為黑白(ifBlackWhite)並指定副檔名為 .tif。
============================================================================== }
procedure TCB_IMGPSScanX.PM301Click(Sender: TObject);
begin
  ScanColor := ifBlackWhite;
  ScanDpi := Def_ScanDpi;
  Ext := '.tif';
  PM301.Checked := True;
end;
 
 
{ ==============================================================================
  方法名稱:PM302Click
  引用相依:ifGray256
  方法描述:將掃瞄模式設定為灰階(ifGray256)並指定副檔名為 .jpg。
============================================================================== }
procedure TCB_IMGPSScanX.PM302Click(Sender: TObject);
begin
  ScanColor := ifGray256;
  Ext := '.jpg';
  ScanDpi := 200;
  //Ext := '.tif';
  PM302.Checked := True;
end;
 
 
{ ==============================================================================
  方法名稱:PM303Click
  引用相依:ifTrueColor
  方法描述:將掃瞄模式設定為全彩(ifTrueColor)並指定副檔名為 .jpg。
============================================================================== }
procedure TCB_IMGPSScanX.PM303Click(Sender: TObject);
begin
  ScanColor := ifTrueColor;
  Ext := '.jpg';     //20130326 yuu說理賠改存jpg
  ScanDpi := 200;
  //Ext := '.tif';
  PM303.Checked := True;
end;
 
 
{ ==============================================================================
  方法名稱:PM501Click
  引用相依:
  方法描述:將影像縮放模式設為「符合寬度」並開啟反鋸齒。
============================================================================== }
procedure TCB_IMGPSScanX.PM501Click(Sender: TObject);
begin
  DisplayISB.ZoomMode := zmFitWidth;
  DisplayISB.AntiAliased := True;
  SetScrollData(DisplayISB,DisplayISB.HorzScrollBar.Position,DisplayISB.VertScrollBar.Position,DisplayISB.ZoomPercent);
end;
 
 
{ ==============================================================================
  方法名稱:PM502Click
  引用相依:
  方法描述:將影像縮放模式設為「符合高度」並開啟反鋸齒。
============================================================================== }
procedure TCB_IMGPSScanX.PM502Click(Sender: TObject);
begin
  DisplayISB.ZoomMode := zmFitHeight;
  DisplayISB.AntiAliased := True;
  SetScrollData(DisplayISB,DisplayISB.HorzScrollBar.Position,DisplayISB.VertScrollBar.Position,DisplayISB.ZoomPercent);
end;
 
 
{ ==============================================================================
  方法名稱:PM503Click
  引用相依:
  方法描述:將影像縮放模式設為「符合頁面」並開啟反鋸齒。
============================================================================== }
procedure TCB_IMGPSScanX.PM503Click(Sender: TObject);
begin
  DisplayISB.ZoomMode := zmFittoPage;
  DisplayISB.AntiAliased := True;
  SetScrollData(DisplayISB,DisplayISB.HorzScrollBar.Position,DisplayISB.VertScrollBar.Position,DisplayISB.ZoomPercent);
end;
 
 
{ ==============================================================================
  方法名稱:PM504Click
  引用相依:
  方法描述:將影像縮放模式設為「原始大小」並開啟反鋸齒。
============================================================================== }
procedure TCB_IMGPSScanX.PM504Click(Sender: TObject);
begin
  DisplayISB.ZoomMode := zmOriginalSize;
  DisplayISB.AntiAliased := True;
  SetScrollData(DisplayISB,DisplayISB.HorzScrollBar.Position,DisplayISB.VertScrollBar.Position,DisplayISB.ZoomPercent);
end;
 
 
{ ==============================================================================
  方法名稱:PM505Click
  引用相依:StatrTwainScan
  方法描述:處理「掃瞄替換此頁」右鍵選單。檢查當前影像後,設定掃瞄模式為 smReplace 
            並指定儲存路徑與檔名為當前顯示之影像。啟動 StatrTwainScan 掃瞄以覆蓋
            原檔案,完成後清空該案件的檢核記錄。
============================================================================== }
procedure TCB_IMGPSScanX.PM505Click(Sender: TObject);
begin
  if DisplayISB.FileName = '' then Exit;
  Panel1.Enabled := False;
  Panel2.Enabled := False;
  ScanMode := smReplace;
  ScanInfo.ImageCount := 0;
  ScanPath := DisplayPath;
  ScanCaseno := '';
  ScanSaveFilename := ExtractFileName(DisplayISB.FileName);
  Try
    StatrTwainScan;
  Except
    Panel1.Enabled := True;
    Panel2.Enabled := True;
  end;
  Panel1.Enabled := True;
  Panel2.Enabled := True;
  ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
end;
 
 
{ ==============================================================================
  方法名稱:PM507Click
  引用相依:RenameFile
  方法描述:處理影像區域的「文件歸類」右鍵選單。彈出對話框供使用者選取目標表單,隨後
            根據選取的 FormID 產生新的序號檔名。執行 RenameFile 進行實際更名並同
            步更新 Context.dat。最後重新整理樹狀結構、清空檢核記錄並使焦點回到原文
            件節點。
============================================================================== }
procedure TCB_IMGPSScanX.PM507Click(Sender: TObject);
var
  i : Integer;
  DocListForm : TDocListForm;
  OldName,NewName,Ext : String;
  FormID,FormName,DocNo : String;
  PreNode2Name : String;
  iFormID : String;
begin
  PreNode2Name := '';
  if TreeView1.Selected.Parent = MyTreeNode1 then
    PreNode2Name:= GetNode2Name(MyTreeNode2);
  ShowText := _Msg('文件歸類中,請稍候');
  DataLoading(True,True);
  DocListForm := TDocListForm.Create(self);
  try
    InitialLanguage(PatchDlg); //載入多國語言
    for i := 1 to FORM_INF_List.Count - 1 do
    begin
      FormID :=  GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
      FormName := GetSQLData(FORM_INF_List,'T1.FORM_DESC',i);
      DocNo := GetSQLData(FORM_INF_List,'T1.DOC_NO',i)+GetSQLData(FORM_INF_List,'T1.DOC_VERSION',i);
      //Showmessage(FORM_INF_List.Text);
      //showmessage(inttostr(FORM_INF_List.Count)+#13+inttostr(self.Doc_Inf_List.Count));
      if (FormID <> FileName2FormCode(DisplayISB.FileName)) and FormIDExists(FormID,False,i) then
      begin
        DocListForm.FormIDList.Add(FormID+'#@#'+FormName);
        With DocListForm.DocLV.Items.Add do
        begin
          Caption := FormID;
          SubItems.Add(FormName);
        end;
      end;
    end;
    if DocListForm.ShowModal = mrOk then
    begin
      OldName := ExtractFileName(DisplayISB.FileName);
      Ext := ExtractFileExt(OldName);
      //NewName := Copy(OldName,1,3)+'_'+TransRealFormID(DocListForm.DocLV.Selected.Caption)+Ext;
      NewName := Add_Zoo(FileName2ScanPage(OldName),3)+'_'+DocListForm.DocLV.Selected.Caption+Ext;
      RenameFile(DisplayPath+OldName,DisplayPath+NewName);
      ReNameContext(DisplayPath,OldName,NewName);
      //DrawDocItem1(MytreeNode1,Doc_Inf_List,NowCaseno);  //201408280改
      DrawDocItem2(MytreeNode1,NowCaseno);
      //DrawDocItem(MytreeNode1,FORM_INF_List,NowCaseno);
      ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
      if PreNode2Name <> '' then  // 回到原本點選的文件節點上
      begin
        for i := 0 to MyTreeNode1.Count - 1 do
        begin
          if GetNode2Name(MyTreeNode1.Item[i]) = PreNode2Name then
          begin
            TreeView1.Selected := MyTreeNode1.Item[i];
            Break;
          end;
        end;
      end;
      TreeView1click(self);
      //Showmessage(_Msg('歸類完成')); //20101103 User要求拿掉
    end;
  finally
  DataLoading(False,False);
  DocListForm.Free;
  end;
 
end;
 
 
 
{ ==============================================================================
  方法名稱:PM508Click
  引用相依:FileExists, LoadFromFile, ReSortFileName, SaveToFile, _DelTree
  方法描述:處理影像區域的「刪除影像」右鍵選單。若案件僅剩一張影像則詢問是否刪除整
            個案件目錄。否則,在確認後從影像清單中移除該項目、刪除實體檔案並呼叫 Re
            SortFileName 重新排序。最後刷新樹狀統計文字、清空檢核記錄並更新顯示。
============================================================================== }
procedure TCB_IMGPSScanX.PM508Click(Sender: TObject);
var
  P : Integer;
  inx:Integer;
begin
  if DisplayISB.FileName = '' then Exit;
  if (ContextList.Count = 1) and ((FMode = 'NSCAN') or (FMode = 'ASCAN') or (FMode = 'DSCAN') or (FMode = 'ISCAN') or (FMode = 'SSCAN') or (FMode = 'MSCAN') or (FMode = 'RI_SCAN')) then
  begin
    if Messagedlg(Format(_Msg('刪除後(%s)案件無影像,將刪除此案件,是否確定刪除?'),[NowCaseno]),mtconfirmation,[mbyes,mbcancel],0) = mrCancel then Exit;
    _DelTree(DisplayPath);
    SetCaseList('D',NewTreeNode.IndexOf(MyTreeNode1),'');
    LoadImgFile;
  end
  Else
  begin
    if Messagedlg(_Msg('是否確定刪除?'),mtconfirmation,[mbyes,mbcancel],0) = mrCancel then Exit;
    inx := ContextList.IndexOf(ExtractFileName(DisplayISB.FileName));
    ContextList.Delete(inx);
    ContextList.SaveToFile(ImageSavePath + NowCaseno+'\Context.dat');
    Context_DocnoList.Delete(inx);
    Context_DocnoList.SaveToFile(ImageSavePath + NowCaseno+'\Context_DocNo.dat');
    DeleteFile(DisplayISB.FileName);
    ReSortFileName(DisplayPath);
    ContextList.LoadFromFile(ImageSavePath + NowCaseno+'\Context.dat');
    Context_DocnoList.LoadFromFile(ImageSavePath + NowCaseno+'\Context_DocNo.dat');
    if FileExists(ImageSavePath + NowCaseno+'\CustomDocNo.dat') then
      Cust_DocNoList.LoadFromFile(ImageSavePath + NowCaseno+'\CustomDocNo.dat');
    //DrawDocItem1(MytreeNode1,Doc_Inf_List,NowCaseno);  //201408280改
    DrawDocItem2(MytreeNode1,NowCaseno);
 
    //DrawDocItem(MytreeNode1,FORM_INF_List,NowCaseno);
    P := ContextList.Count;
    MytreeNode1.Text := Format(_Msg('%s-%d頁'),[NowCaseno,p]);
    ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
    NewTreeNodeRefresh;
    TreeView1Click(self);
 
  end;
  //Showmessage(_Msg('刪除完成'));  //20101101 User要求拿掉
end;
 
 
{ ==============================================================================
  方法名稱:PM509Click
  引用相依:
  方法描述:觸發「從此分案」功能,內部轉呼叫 PM401Click。
============================================================================== }
procedure TCB_IMGPSScanX.PM509Click(Sender: TObject);
begin
  PM401Click(nil);
end;
 
 
{ ==============================================================================
  方法名稱:PM510Click
  引用相依:DeskewImg, SaveToFile
  方法描述:對當前 Graphic 執行去偏斜處理後存回原檔案。
============================================================================== }
procedure TCB_IMGPSScanX.PM510Click(Sender: TObject);
begin
  DeskewImg(DisplayISB.Graphic);
  DisplayISB.SaveToFile(DisplayISB.FileName);
  ClearErrini(NowCaseno,MyTreeNode1);  //清掉檢核記錄
end;
 
 
{ ==============================================================================
  方法名稱:PopupMenu1Popup
  引用相依:
  方法描述:處理樹狀結構右鍵選單彈出時的動態項目控制。根據選取節點的層級(根、案件、
            文件、表單)與系統模式(如 FMode、FImgDelete 權限等)動態設定各項選單(刪
            除、修改、掃瞄加入、歸類、檢核等)的可見度與可用性。例如在文件層會受影像引
            用狀態限制刪除功能。
============================================================================== }
procedure TCB_IMGPSScanX.PopupMenu1Popup(Sender: TObject);
begin
  PM101.Visible := False;   //刪除
  PM102.Visible := False;   //修改案件編號
  PM103.Visible := False;   //掃瞄器加入影像
  PM104.Visible := False;   //檔案加入影像
  PM106.Visible := False;   //複製文件至其他編號
  PM107.Visible := False;   //寫備註
  PM108.Visible := False;   //歸類
  PM109.Visible := False;   //檢核此筆
  PM110.Visible := False;   //新增自訂文件
  PM111.Visible := False;   //修改份數
 
  if (FMode = 'SAMPLESCAN') then Exit;
 
  if TreeView1.Selected = nil then Exit;
  if TreeView1.Selected = NewTreeNode  then     //新掃瞄件
  begin
    if (FMode = 'NSCAN') then
    begin
//ShowMessage('AAAA');
      PM101.Visible := True;   //刪除
      PM103.Visible := True;   //掃瞄器加入影像
      PM104.Visible := True;   //檔案加入影像
    end;
 
    if FModeName=_Msg('異動件') then
    begin
      PM101.Visible := True;
    end;
//    if FMode='ESCAN' then
//    begin
//      PM101.Visible := True;
//    end;
  end
  Else if TreeView1.Selected = MyTreeNode1 then    //案件層
  begin
    PM101.Visible := True;   //刪除
 
    if FImgDelete='Y' then
    begin
      PM101.Visible:=True;
    end;
    if FImgDelete='N' then
    begin
      PM101.Visible:=false;
    end;
 
    if FMode='ESCAN' then
      PM101.Visible:=false;
 
    if FModeName=_Msg('異動件') then
    begin
      PM101.Visible := True;
    end;
 
    if not CaseDelete_Enable(NowCaseno) then //
      PM101.Enabled := False
    else
      PM101.Enabled := True;
    PM103.Visible := True;   //掃瞄器加入影像
    //PM107.Visible := True;   //寫備註
    //PM109.Visible := True;   //檢核此筆
//    if FCustDocYN <> 'N' Then
//      PM110.Visible := True;   //新增自訂文件  20170914 先不在tree 中做自訂文件  讓user在縮圖做
    PM104.Visible := True;   //檔案加入影像
    if (FMode = 'NSCAN') then
    begin
      PM102.Visible := True;   //修改案件編號
    end;
//    if FMode='ESCAN' then
//    begin
//      PM101.Visible := True;
//    end;
  end
  Else if TreeView1.Selected = MyTreeNode2 then    //文件層
  begin
    PM101.Visible := True;   //刪除
    //PM107.Visible := True;   //寫備註
    //PM109.Visible := True;   //檢核此筆
//    if FCustDocYN <> 'N' Then
//      PM110.Visible := True;   //新增自訂文件
    if FImgDelete='Y' then
    begin
      PM101.Visible:=True;
    end;
    if FImgDelete='N' then
    begin
      PM101.Visible:=false;
    end;
 
    if FModeName=_Msg('異動件') then
    begin
      PM101.Visible := True;
    end;
 
    if GetUseCase('T',DisplayPath,NowDocDir) <> '' then   //沒有被引用走的
      PM101.Enabled := False    //刪除
    Else
      PM101.Enabled := True;   //刪除
    if ((GetDocDirCopies(NowCaseno,NowDocDir) > 1) or (not DocNoNeedDiv(NowDocNo)) or (Copy(NowDocNo,1,5)='ZZZZZ')) and (NowDocNo<> 'Attach') and (NowDocNo<> 'S_Attach') then
      PM111.Visible := True;   //修改份數
    if (FMode = 'NSCAN') then
      PM102.Visible := True;   //修改案件編號
//    if FMode='ESCAN' then
//    begin
//      PM101.Visible := True;
//    end;
  end
  Else if TreeView1.Selected = MyTreeNode3 then    //表單層
  begin
    PM101.Visible := True;   //刪除
    PM104.Visible := True;   //檔案加入影像
    PM108.Visible := True;   //歸類
    PM103.Visible := True;   //掃瞄器加入影像
//    if FCustDocYN <> 'N' Then
//      PM110.Visible := True;   //新增自訂文件
    if GetFormIDPage(ContextList,NowFormCode) < 1 Then
    begin
      PM108.Visible := False;   //歸類
    end;
    if FImgDelete='Y' then
    begin
      PM101.Visible:=True;
    end;
    if FImgDelete='N' then
    begin
      PM101.Visible:=false;
    end;
 
    if FModeName=_Msg('異動件') then
    begin
      PM101.Visible := True;
    end;
 
    if GetUseCase('T',DisplayPath,NowDocDir) <> '' then   //被引用走的
    begin
      PM101.Enabled := False;   //刪除
      PM104.Enabled := False;   //檔案加入影像
      PM108.Enabled := False;   //歸類
    end
    Else
    begin
      PM101.Enabled := True;   //刪除
      PM104.Enabled := True;   //檔案加入影像
      PM108.Enabled := True;   //歸類
    end;
 
    if (FMode = 'NSCAN') then
    begin
      PM102.Visible := True;   //修改案件編號
    end;
  end;
 
 
 
end;
 
 
{ ==============================================================================
  方法名稱:PopupMenu4Popup
  引用相依:
  方法描述:處理影像列表右鍵選單彈出邏輯。僅在樹狀結構選取文件或表單層級時顯示全
            選、取消全選及歸類功能,並特別判斷「分出新案」在非重掃模式下才可見。
============================================================================== }
procedure TCB_IMGPSScanX.PopupMenu4Popup(Sender: TObject);
begin
  PM401.Visible := False;
  PM402.Visible := False;
  PM403.Visible := False;
  PM404.Visible := False;
  if FMode = 'SAMPLESCAN' then Exit;
  if (TreeView1.Selected.Level =2) or (TreeView1.Selected.Level =3) then
  begin
    PM402.Visible := True;
    PM403.Visible := True;
    PM404.Visible := True;
  end;
  PM401.Visible := True;
  if (TreeView1.Selected <> MyTreeNode2) or (FMode = 'RSCAN') then
    PM401.Visible := False;
end;
 
 
{ ==============================================================================
  方法名稱:PopupMenu5Popup
  引用相依:
  方法描述:處理影像顯示區域右鍵選單彈出邏輯。當有載入影像時啟用各類縮放選單項目,
            並根據樹狀選取層級與掃瞄模式決定是否顯示「分出新案」功能。
============================================================================== }
procedure TCB_IMGPSScanX.PopupMenu5Popup(Sender: TObject);
begin
  PM501.Visible := False;
  PM502.Visible := False;
  PM503.Visible := False;
  PM504.Visible := False;
  PM505.Visible := False;
  PM506.Visible := False;
  PM507.Visible := False;
  PM508.Visible := False;
  PM509.Visible := False;
  PM510.Visible := False;
  if FMode = 'SAMPLESCAN' then Exit;
  if (DisplayISB.FileName <> '') then
  begin
    PM501.Visible := True;
    PM502.Visible := True;
    PM503.Visible := True;
    PM504.Visible := True;
    //PM505.Visible := True;
    //PM506.Visible := True;
    //PM507.Visible := True;
    //PM508.Visible := True;
    //PM509.Visible := True;
    //PM510.Visible := True;
  end;
  if (TreeView1.Selected <> MyTreeNode2) or (FMode = 'RSCAN') or (FMode = 'ESCAN') then
    PM509.Visible := False;
end;
 
 
{ ==============================================================================
  方法名稱:PopupMenu6Popup
  引用相依:
  方法描述:處理縮圖區右鍵選單彈出邏輯。動態設定歸類、自定義文件及刪除項目的可見度
            。若選取附件目錄則開啟特殊功能。同時根據權限設定與影像引用狀態(UseCase
            )限制歸類與刪除功能的可用性。
============================================================================== }
procedure TCB_IMGPSScanX.PopupMenu6Popup(Sender: TObject);
begin
  PM601.Visible := True;  //歸類
  PM602.Visible := True;  //自行定義文件名稱
  PM603.Visible := False;  //掃描替換此頁
  PM604.Visible := False;  //歪斜矯正
  PM605.Visible := True;  //刪除
 
//  if FMode='ESCAN' then
//  begin
//    PM601.Visible := False;  //歸類
//    PM602.Visible := False;  //自行定義文件名稱
//    PM603.Visible := False;  //掃描替換此頁
//    PM604.Visible := False;  //歪斜矯正
//    PM605.Visible := False;  //刪除
//  end;
 
  if ((NowDocNo = 'Attach') or (NowDocNo = 'S_Attach')) and (FCustDocYN <> 'N') then
  begin
    PM602.Visible := True;  //自行定義文件名稱
    //PM603.Visible := True;  //掃描替換此頁
    PM604.Visible := True;  //歪斜矯正
    PM601.Visible := True;  //歸類
    PM605.Visible := True;  //刪除
  end;
 
  if  FModeName<>_Msg('異動件') then
  begin
    if (FImgDelete='Y') then
    begin
      PM605.Enabled:=True;
    end;
    if FImgDelete='N' then
    begin
      PM605.Enabled:=false;
    end;
  end;
 
 
 
 
  if CheckSelectImg_UseCase(DisplayPath,NowCaseNo) then //選擇的影像不可有引用的
  begin
    PM601.Enabled := False;  //歸類
    PM605.Enabled := False;  //刪除
  end;
 
 
end;