ChinaCock打印控件介绍-TCCFujitsuPrinter实现蓝牙针式打印
项目中遇到,要蓝牙针式打印机,用手机打印表单。感谢专家,对厂家提供的SDK进行了封装,实现利用Delphi开发出这一功能。
现在来看看,如何利用这一控件实现打印过程:
procedure startScanDevices; procedure stopScanDevices; function getScanedDevices: TList<TCCFujitsuPrinterDevice>; function openConnection(ADevice: TCCFujitsuPrinterDevice): Integer; procedure printPDFPaper(pdfPath: String; sX: Integer; mode: Integer);
TCCFujitsuPrinter提供了上面几个方法,利用startScanDevices扫描蓝牙打印机,
property Devices: TList<TCCFujitsuPrinterDevice> read FDevices;
然后我们用stopScanDevices停止扫描,调用getScanDevices,将扫描到的设备存到Devices。
接下来就是利用openConnection联接打印机,成功返回1,然后调用printPDFPaper进行打印。
下面,是实际项目中的查询打印机的代码,利用kbmMW Scheduler实现。
procedure TPrintFrame.ScanPrinter;
begin
if IsConnectPrinter=1 then
Exit;
AniIndicator1.Visible:=True;
AniIndicator1.Enabled:=True;
Printer.startScanDevices;//查找打印机
Scheduler.Schedule(
procedure (const AScheduledEvent:IkbmMWScheduledEvent)
var
i:Integer;
begin
CCButton1.Text:='正在查找打印机...';
Printer.getScanedDevices;//取得扫到的设备
for I := 0 to Printer.Devices.Count-1 do
begin
if (Printer.Devices[i].DeviceName='蓝牙打印机') and (Printer.Devices[i].Paired) then//如果找到指定的打印机并已经配对,中止扫描并返回设备号
begin
DeviceIndex:=i;
printer.stopScanDevices;
IsConnectPrinter:=printer.openConnection(Printer.Devices[i]);
sleep(500);
AniIndicator1.Visible:=False;
AniIndicator1.Enabled:=False;
CCButton1.Text:='开始打印';
AScheduledEvent.Activate(False);//中止线程
Break;
end;
end;
end)
.Synchronized//主线程中执行
.NamedAs('ScanPrinter')
.EverySecond(2)//每两秒检查一次扫到的设备
.Activate;
end;
找到了打印机,就可以实现打印了:
procedure TPrintFrame.CCButton1Click(Sender: TObject); begin if DeviceIndex=-1 then//没找到打印机直接返回 Exit; if IsConnectPrinter<>1 then//没联接打印机则执行联接打印机 IsConnectPrinter:=Printer.openConnection(Printer.Devices[DeviceIndex]); Scheduler.Run(procedure begin Printer.printPDFPaper(FInfo.Data['FileName'].AsString,0,2); end) .Activate; end;
上面代码,同样利用kbmMW Scheduler实现在线程中打印。
最后,代码实现完成,发布时要带几个SO文件:

这个控件在即将发布的新版本中集成,如果项目中有需要,可以联系作者购买。
开发的过程中遇到一个问题,就是必须在Release下才能正常打印!
浙公网安备 33010602011771号