最近用aardio写了一个打印管理工具,写了一个简单的库,实现了简单的打印机和打印任务管理功能,分享给大家一起学习。

库文件:printMGT
Code AardioLine:131复制
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.namespace
printMGT;import
com
.wmi;- getAllPrinter =
function
() { -
-
-
var
getPrinterStatus = function
(statusCode) { -
select
(statusCode) { -
case
1
return
"其他"
-
case
2
return
"未知"
-
case
3
return
"空闲"
-
case
4
return
"正在打印"
-
case
5
return
"预热"
-
case
6
return
"停止打印"
-
case
7
return
"脱机"
-
else
return
"错误"
- }
- }
-
var
prtList = {}; -
-
-
-
for
index, wmiPrinter in
com
.wmi.each("SELECT * FROM Win32_Printer"
) { - ..
table
.push( prtList, {name = wmiPrinter.Name; status = wmiPrinter.WorkOffline?"脱机"
:getPrinterStatus(wmiPrinter.PrinterStatus), portName = wmiPrinter.PortName}); - }
-
return
prtList; - }
- getPrintQueue =
function
(printerName) { -
-
-
var
jobs = {}; -
for
index, wmiJob in
com
.wmi.each("SELECT * FROM Win32_PrintJob"
) { -
-
-
-
-
-
-
-
-
-
var
prtName = ..string
.split(wmiJob.Name, ","
)[1
]; -
if
( -
var
jobInfo = {}; - ..
table
.push(jobInfo, wmiJob.Document, wmiJob.JobStatus?wmiJob.JobStatus:"-"
, wmiJob.Owner
); -
-
if
(wmiJob.PagesPrinted == 0
) - ..
table
.push(jobInfo, wmiJob.TotalPages) -
else
..table
.push(jobInfo, wmiJob.PagesPrinted ++ "/"
++ (wmiJob.PagesPrinted + wmiJob.TotalPages)); -
-
if
(wmiJob.Size >= 1024
*1024
) - ..
table
.push(jobInfo, ..math
.floor(wmiJob.Size/1048576
) ++ " MB"
) -
else
..table
.push(jobInfo, ..math
.floor(wmiJob.Size/1024
) ++ " KB"
); - ..
table
.push(jobInfo, wmiJob.PaperSize); -
var
y,m,d,h,min,s = ..string
.match(wmiJob.TimeSubmitted, "^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})"
); - ..
table
.push(jobInfo, ..string
.format("%d:%d:%d %d/%d/%d"
,h,min,s,y,m,d)); - ..
table
.push(jobInfo, ""
); - ..
table
.push(jobInfo, wmiJob.Priority); - ..
table
.push(jobInfo, wmiJob.JobId); -
-
if
(!jobs[prtName]) jobs[prtName] = {}; - ..
table
.push(jobs[prtName], jobInfo); - }
-
var
prtQueue = {}; -
for
(name, value in
jobs){ - ..
table
.push(prtQueue, {name=name, jobs=value}); - }
-
return
prtQueue; - }
-
- getPrintJob =
function
(jobID){ -
if
(!jobID) return
; -
var
job; -
for
index, wmiJob in
com
.wmi.each("SELECT * FROM Win32_PrintJob WHERE JobId="
++ jobID) { - job = wmiJob;
- }
-
return
job; - }
- cancelPrintJob =
function
(jobID){ -
var
job = getPrintJob(jobID); -
if
(job) job.Delete_(); - }
-
- openPrintersAndFaxesFolder =
function
(){ -
import
process.control; -
if
(!_WIN10_LATER){ -
- process.control(
"ms-settings:printers"
) - }
-
else
{ -
- process.control(
"printers"
) - }
- }
- openPrinterQueue =
function
(prtName){ -
if
(!prtName) return
; -
import
process.rundll; - process.rundll(
"printui.dll"
,"PrintUIEntry"
,'/o /n "' ++ prtName ++ '"
'); - }
- openPrinterPreferences =
function
(prtName){ -
if
(!prtName) return
; -
import
process.rundll; - process.rundll(
"printui.dll"
,"PrintUIEntry"
,'/e /n "' ++ prtName ++ '"
'); - }
- openPrintProperties =
function
(prtName){ -
if
(!prtName) return
; -
import
process.rundll; - process.rundll(
"printui.dll"
,"PrintUIEntry"
,'/p /n "' ++ prtName ++ '"
'); - }
使用示例:
Code AardioLine:125复制
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.import
win
.ui;import
fonts.fontAwesome;- winform =
win
.form(text="打印管理 - by breezee"
;right=1151
;bottom=543
) - winform.add(
- btnLoadPrinter={cls=
"button"
;text="加载所有打印机"
;left=16
;top=16
;right=151
;bottom=56
;z=1
}; - btnPrintersFolder={cls=
"button"
;text="打开设备和打印机"
;left=1008
;top=24
;right=1136
;bottom=56
;color=8421504
;z=3
}; - lvwPrintJob={cls=
"listview"
;left=16
;top=320
;right=1136
;bottom=528
;edge=1
;fullRow=1
;msel=false
;z=5
}; - lvwPrinter={cls=
"listview"
;left=16
;top=72
;right=1136
;bottom=280
;edge=1
;fullRow=1
;msel=false
;z=4
}; - static2={cls=
"static"
;text="打印任务:"
;left=16
;top=296
;right=120
;bottom=320
;color=8421504
;db=1
;dl=1
;font=LOGFONT(h=-15
);transparent=1
;z=6
}; - staticPrinter={cls=
"static"
;left=96
;top=296
;right=600
;bottom=320
;db=1
;dl=1
;font=LOGFONT(h=-15
);transparent=1
;z=2
}; - staticTotal={cls=
"static"
;left=632
;top=296
;right=1136
;bottom=320
;align="right"
;color=8421504
;db=1
;dl=1
;transparent=1
;z=7
} - )
import
console;import
printMGT;- winform.btnLoadPrinter.oncommand =
function
(id,event) { -
- winform.lvwPrinter.clear();
-
var
printerList = printMGT.getAllPrinter(); -
for
(i=1
; - winform.lvwPrinter.addItem({printerList[i].name; printerList[i].portName; printerList[i].status});
- }
- }
- winform.btnPrintersFolder.oncommand =
function
(id,event){ - printMGT.openPrintersAndFaxesFolder();
- }
- winform.lvwPrinter.setColumns({
"打印机名称"
,"端口名称"
,"状态"
},{300
,350
,80
}); - winform.lvwPrinter.onFocusedChanged =
function
(item,subItem,nmListView){ -
var
prtName = owner
.getItemText(item,subItem); - winform.staticPrinter.text = prtName;
- winform.staticTotal.text =
""
; - winform.lvwPrintJob.clear();
-
var
printQueue = printMGT.getPrintQueue(prtName); -
if
( - winform.staticTotal.text =
"队列中有 "
++ -
for
(i=1
; - winform.lvwPrintJob.addItem(printQueue[
1
].jobs[i]); - }
- }
- }
- winform.lvwPrinter.onRightClick =
function
(item,subItem,nmListView){ -
if
(!item) return
; -
import
win
.ui.menu; -
var
popmenu = win
.ui.popmenu(winform); - popmenu.add(
"打印机属性"
,,,1
); - popmenu.add(
"打印首选项"
,,,2
); - popmenu.add(
"打开打印队列"
,,,3
); -
var
ptrName = owner
.getItemText(item,1
); -
if
( -
select
(popmenu.popId()) { -
case
1
printMGT.openPrintProperties(ptrName); -
case
2
printMGT.openPrinterPreferences(ptrName); -
case
3
printMGT.openPrinterQueue(ptrName); - }
- }
- }
- winform.lvwPrintJob.setColumns({
"文档名"
,"状态"
,"所有者"
,"页数"
,"文档大小"
,"纸张大小"
,"提交时间"
,"端口"
,"优先级"
,"任务ID"
},{300
,150
,80
,50
,80
,130
,130
,50
,60
,0
}); - winform.lvwPrintJob.onRightClick =
function
(item,subItem,nmListView){ -
if
(!item) return
; -
var
jobId = owner
.getItemText(item, 10
); -
var
job = printMGT.getPrintJob(jobId); -
if
(!job) { -
import
win
.dlg.message; - winform.msgErr(
"加载该打印任务出错。"
); -
return
; - }
-
var
jobStatus = job.JobStatus : "-"
; -
-
-
import
win
.ui.menu; -
var
popmenu = win
.ui.popmenu(winform); -
if
(string
.find(jobStatus, "暂停"
)) - popmenu.add(
"继续"
,,,1
) -
else
- popmenu.add(
"暂停"
,,,1
); - popmenu.add(
"重新启动"
,,,2
); - popmenu.add();
- popmenu.add(
"取消任务"
,,,3
); - popmenu.add();
- popmenu.add(
"任务属性"
,,,4
); - popmenu.enable(
6
,false
); -
-
select
(popmenu.popId()) { -
case
1
{ -
if
(job){ -
if
(string
.find(jobStatus, "暂停"
)) - job.Resume()
-
else
- job.Pause();
-
- job = printMGT.getPrintJob(jobId);
-
owner
.setItemText(job.JobStatus:"-"
, item, 2
); - }
- }
-
case
2
{ - printMGT.openPrinterProperties(winform.staticPrinter.text);
- }
-
case
3
{ - printMGT.cancelPrintJob(jobId);
-
owner
.delItem(item); - }
-
case
4
{ -
- }
- }
- }
- winform.show();
return
win
.loopMessage();