Delphi 打印条码程序 条码类型CODE 128 B
调用过程:
setOrientation_Landscape;
setpagesize(1000,1600);
//pages:=1;
printer.Copies :=pages;
printer.BeginDoc;
// for i:=1 to pages do
// begin
drawtext('MS Sans Serif',16,100,20,'VENDOR:');
drawtext('MS Sans Serif',16,800,20,'DESCRIPTION:');
ts:= ds2.FieldByName('labelcode').AsString ;
drawtext('MS Sans Serif',16,100,70,ts);
ts:= ds2.FieldByName('fsize').AsString ;
drawtext('MS Sans Serif',16,800,70,ts);
drawline(100,115,1200,115,2);
drawline(790,20,790,115,2);
drawtext('MS Sans Serif',16,100,120,'ORDER NUMBER:');
ts:= ds2.FieldByName('qty').AsString ;
drawtext('Arial',24,350,160,ts);
drawline(100,230,1200,230,2);
drawtext('MS Sans Serif',16,100,240,'SKU:');
ts:= ds2.FieldByName('barcode').AsString ;
drawcode128B1(ts ,200,240,140,5); //打印128B条码
drawtext('Arial',18,420,390,ts);
drawline(100,470,1200,470,2);
drawline(410,470,410,670,2);
drawline(790,470,790,670,2);
drawtext('MS Sans Serif',16,100,480,'CARTON:');
drawtext('MS Sans Serif',16,420,480,'UNITS IN CARTON:');
drawtext('MS Sans Serif',16,800,480,'DESPATCH/SHIPPED');
ts:= ds2.FieldByName('PO').AsString ;
drawtext('MS Sans Serif',16,100,530,ts);
drawtext('MS Sans Serif',16,800,530,'DATE:');
ts:= ds2.FieldByName('style').AsString ;
drawtext('MS Sans Serif',16,100,580,ts);
ts:= ds2.FieldByName('dimpack').AsString ;
drawtext('MS Sans Serif',16,880,580,ts);
ts:= ds2.FieldByName('color').AsString ;
drawcode128B1(ts ,460,530,100,5);
drawtext('MS Sans Serif',16,520,640,ts);
// if i // end; printer.EndDoc; 函数实现: 设置打印方向(横向打印) procedure setOrientation_Landscape; var ADevice,ADriver,APort:array [0..255] of Char; DeviceHandle:THandle; DevMode:PDeviceMode; begin Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle); if DeviceHandle=0 then begin Printer.PrinterIndex:=Printer.PrinterIndex; Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle); end; if DeviceHandle=0 then Raise Exception.Create('Cound Not Initialize TDeviceMode structure') else DevMode:=GlobalLock(DeviceHandle); with DevMOde^ do begin dmFields:=dmFields or DM_ORIENTATION; dmOrientation:=DMORIENT_LANDSCAPE; end; if not DeviceHandle=0 then GlobalUnlock(DeviceHandle); end; 设置打印纸大小 procedure setpagesize(paperwidth,paperlength:integer); var ADevice,ADriver,APort:array [0..255] of Char; DeviceHandle:THandle; DevMode:PDeviceMode; begin Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle); if DeviceHandle=0 then begin Printer.PrinterIndex:=Printer.PrinterIndex; Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle); end; if DeviceHandle=0 then Raise Exception.Create('Cound Not Initialize TDeviceMode structure') else DevMode:=GlobalLock(DeviceHandle); with DevMOde^ do begin dmFields:=dmFields or DM_PAPERSIZE or DM_PAPERLENGTH DM_PAPERWIDTH; or dmPaperSize:=DMPAPER_USER; dmPaperWidth:=paperwidth; dmPaperLength:=paperlength; end; if not DeviceHandle=0 then GlobalUnlock(DeviceHandle); end; 取CODE128逻辑码 function return_code128Blogic(code:char):string; begin case code of '0': result:='bssbbbsbbss'; //code--0 '1': result:= 'bssbbbssbbs'; //code--1 '2': result:= 'bbssbbbssbs'; //code--2 '3': result:= 'bbssbsbbbss'; //code--3 '4': result:= 'bbssbssbbbs'; //code--4 '5': result:= 'bbsbbbssbss'; //code--5 '6': result:= 'bbssbbbsbss'; //code--6 '7': result:= 'bbbsbbsbbbs'; //code--7 '8': result:= 'bbbsbssbbss'; //code--8 '9': result:= 'bbbssbsbbss'; //code--9 '+': result:= 'bbsssbssbss'; //code--+ '-': result:= 'bssbbsbbbss'; //code--- '*': result:= 'bbssbsssbss'; //code--* '/': result:= 'bsbbbssbbss'; //code--/ '%': result:= 'bsssbssbbss'; //code--% '$': result:= 'bssbsssbbss'; //code--$ '.': result:= 'bssbbssbbbs'; //code--. ' ': result:= 'bbsbbssbbss'; //code--空白 'A': result:= 'bsbsssbbsss'; //code--A 'B': result:= 'bsssbsbbsss'; //code--B 'C': result:= 'bsssbsssbbs'; //code--C 'D': result:= 'bsbbsssbsss'; //code--D 'E': result:= 'bsssbbsbsss'; //code--E 'F': result:= 'bsssbbsssbs'; //code--F 'G': result:= 'bbsbsssbsss'; //code--G 'H': result:= 'bbsssbsbsss'; //code--H 'I': result:= 'bbsssbsssbs'; //code--I 'J': result:= 'bsbbsbbbsss'; //code--J 'K': result:= 'bsbbsssbbbs'; //code--K 'L': result:= 'bsssbbsbbbs'; //code--L 'M': result:= 'bsbbbsbbsss'; //code--M 'N': result:= 'bsbbbsssbbs'; //code--N 'O': result:= 'bsssbbbsbbs'; //code--O 'P': result:= 'bbbsbbbsbbs'; //code--P 'Q': result:= 'bbsbsssbbbs'; //code--Q 'R': result:= 'bbsssbsbbbs'; //code--R 'S': result:= 'bbsbbbsbsss'; //code--S 'T': result:= 'bbsbbbsssbs'; //code--T 'U': result:= 'bbsbbbsbbbs'; //code--U 'V': result:= 'bbbsbsbbsss'; //code--V 'W': result:= 'bbbsbsssbbs'; //code--W 'X': result:= 'bbbsssbsbbs'; //code--X 'Y': result:= 'bbbsbbsbsss'; //code--Y 'Z': result:= 'bbbsbbsssbs'; //code--Z '[': result:= 'bbbsssbbsbs'; '\\': result:= 'bbbsbbbbsbs'; ']': result:= 'bbssbssssbs'; '^': result:= 'bbbbsssbsbs'; '_': result:= 'bsbssbbssss'; '`': result:= 'bsbssssbbss'; 'a': result:= 'bssbsbbssss'; 'b': result:= 'bssbssssbbs'; 'c': result:= 'bssssbsbbss'; 'd': result:= 'bssssbssbbs'; 'e': result:= 'bsbbssbssss'; 'f': result:= 'bsbbssssbss'; 'g': result:= 'bssbbsbssss'; 'h': result:= 'bssbbssssbs'; 'i': result:= 'bssssbbsbss'; 'j': result:= 'bssssbbssbs'; 'k': result:= 'bbssssbssbs'; 'l': result:= 'bbssbsbssss'; 'm': result:= 'bbbbsbbbsbs'; 'n': result:= 'bbssssbsbss'; 'o': result:= 'bsssbbbbsbs'; 'p': result:= 'bsbssbbbbss'; 'q': result:= 'bssbsbbbbss'; 'r': result:= 'bssbssbbbbs'; 's': result:= 'bsbbbbssbss'; 't': result:= 'bssbbbbsbss'; 'u': result:= 'bssbbbbssbs'; 'v': result:= 'bbbbsbssbss'; 'w': result:= 'bbbbssbsbss'; 'x': result:= 'bbbbssbssbs'; 'y': result:= 'bbsbbsbbbbs'; 'z': result:= 'bbsbbbbsbbs'; '{': result:= 'bbbbsbbsbbs'; '|': result:= 'bsbsbbbbsss'; '}': result:= 'bsbsssbbbbs'; '~': result:= 'bsssbsbbbbs'; else result:= '0'; end; end; procedure drawtext(fontname:string;fontsize,x,y:integer;text:string); begin printer.Canvas.Brush.Color:=clwhite; printer.Canvas.Font.Name:=fontname; //'MS Sans Serif'; printer.Canvas.Font.size:=fontsize; Printer.Canvas.TextOut(x,y,text); end; 返回给定字符串的CODE128 编码 function returncode128B(s:string):string; var code,c1:char; printcode:string; i,k,a,m:integer; begin k:=104; //code128B for i:=1 to length(s) do begin code:=s[i]; a:=ord(code)-32; k:=k+a*i; end; m:=k mod 103; printcode:='bbsbssbssss'; for i:=1 to length(s) do begin printcode:=printcode+return_code128Blogic(s[i]); end; m:=m+32; c1:=chr(m); printcode:=printcode+return_code128Blogic(c1); printcode:=printcode+'bbsssbbbsbsbb'; result:=printcode; end; 画条码(条码内容,x0,y0: 起始点,height: 条码高度, penwidth: 条码线宽) procedure drawcode128B1(barcode:string;x0,y0,height,penwidth:integer); var i,j,x,y1,y2:integer; code:Char; printcode:string; begin printcode:=returncode128B(barcode); printer.Canvas.pen.Width :=1; printer.Canvas.Pen.Color :=clBlack; // printer.Canvas.Brush.Color:=clBlack; x:=x0; //50 for i:=1 to length(printcode) do begin code:=printcode[i]; if code='b' then begin y1:=y0; y2:=y0+height; //printer.Canvas.Rectangle(x1,y1,x2,y2); for j:=0 to penwidth-1 do begin drawline(x+j,y1,x+j,y2,1); end; end; x:=x+penwidth; end; end; 画线: procedure drawline(x1,y1,x2,y2,penwidth:integer); begin Printer.Canvas.Pen.Width :=penwidth; Printer.Canvas.MoveTo(x1,y1); Printer.Canvas.LineTo(x2,y2); end; 因篇幅问题不能全部显示,请点此查看更多更全内容