请教高手:有关delphi同时控制多台打印机打印的问题?(100分)

  • 主题发起人 主题发起人 cmslj
  • 开始时间 开始时间
C

cmslj

Unregistered / Unconfirmed
GUEST, unregistred user!
帮帮我好吗??高手们,我会急死了!!
请注释:我的打印机有十几台。 我在点击‘开始打印’按钮时,就开始打印 ,但是打印是有条件的,就是根据不同的条件,让不同的打印机去打印,例如:
if adotable1.fieldvalus['菜类']='01' 的 让lenged001 打印,
if adotable1.fieldvalus['菜类']='02' 的 让lenged002 打印,.........
但是这是同时打印的,我不想用索引值控制打印机,我想用打印机的名字控制打印机,
怎么才能控制打印机的名字那??帮帮我好吗???
注释::其中 lenged001,lenged002 都是打印机的名字。
 
呵呵!这里真是高手云集:)
 
这个问题比较实惠,有实际意义,我将继续关注.
 
我这有个别处的资料,你看看吧
Example:
unit multiprn;
interface
{$IFDEF WIN32}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
{$else
}
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
{$ENDIF}

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ListBox2Click(Sender: TObject);
procedure ListBox3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure GetTheListOfPrinters;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
type
PdcArray = ^TdcArray;
TdcArray = array[0..0] of hdc;
procedure TForm1.GetTheListOfPrinters;
var
p : pChar;
p2 : pChar;
i : integer;
sDriver : string;
sPort : string;
begin
GetMem(p, 32767);
p2 := p;
{Get a list of the printer names from the win.ini file.}
{The list will be a buffer filled with strings, each seperated}
{by a null character, with the final string terminated with}
{a two null characters}
if GetProfileString('devices', nil, '',p, 32767) <> 0 then
begin
{loop though the null terminated strings. We know we}
{have reached the end when p2 equals a null character}
while p2^ <> #0do
begin
ListBox1.Items.Add(StrPas(p2));
{increment the pointer past the null to get the next string}
p2 := @p2[lStrLen(p2) + 1];
end;
end;
GetMem(p2, 32767);
{Get the driver and port names for each printer found}
for i := 0 to (ListBox1.Items.Count - 1)do
begin
StrPCopy(p2, ListBox1.Items);
if GetProfileString('devices', p2, '',p, 32767) <> 0 then
begin
sDriver := StrPas(p);
sPort := sDriver;
{The driver is the portion of the string before the comma}
Delete(sDriver, Pos(',', sDriver), Length(sDriver));
{The port is the portion of the string after the comma}
Delete(sPort, 1, Pos(',', sPort));
ListBox2.Items.Add(sDriver);
ListBox3.Items.Add(sPort);
end;
end;
FreeMem(p2, 32767);
FreeMem(p, 32767);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
GetTheListOfPrinters;
{Allow the user to select multiple printers}
ListBox1.MultiSelect := true;
ListBox2.MultiSelect := true;
ListBox3.MultiSelect := true;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
i : integer;
begin
{Select same indexes in ListBoxs 2 and 3 as are selected in Listbox1}
for i := 0 to ListBox1.Items.Count - 1do
begin
ListBox2.Selected := ListBox1.Selected;
ListBox3.Selected := ListBox1.Selected
end;
end;

procedure TForm1.ListBox2Click(Sender: TObject);
var
i : integer;
begin
{Select same indexes in ListBoxs 1 and 3 as are selected in Listbox2}
for i := 0 to ListBox2.Items.Count - 1do
begin
ListBox1.Selected := ListBox2.Selected;
ListBox3.Selected := ListBox2.Selected
end;
end;

procedure TForm1.ListBox3Click(Sender: TObject);
var
i : integer;
begin
{Select same indexes in ListBoxs 1 and 2 as are selected in Listbox3}
for i := 0 to ListBox3.Items.Count - 1do
begin
ListBox1.Selected := ListBox3.Selected;
ListBox2.Selected := ListBox3.Selected
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
dcs : PdcArray;
{a pointer to an array of hdc's}
dcsCount : integer;
{count of valad hdc's}
dc : hdc;
{test hdc}
i : integer;
{couning variable}
pPrinter : pChar;
{pointer to a printer name}
pDriver : pChar;
{pointer to a printer driver filename}
pPort : pChar;
{pointer to a port}
do
cInfo: TDocInfo;
{document information for the spooler}
{$IFDEF WIN32}
osv : TOSVERSIONINFO;
{to test for Windows NT}
{$ENDIF}
begin
{If there are no printers selected then
exit}
if ListBox1.SelCount = 0 then
exit;
{If Range Checking is on then
rember the setting and turn it off}
{This will allow use to access the dcs array past element zero}
{without a compiler or runtime error}
{$IFOPT R+}
{$DEFINE CKRANGE}
{$R-}
{$ENDIF}
{Allocate the array of dcs}
GetMem(dcs, sizeof(hdc) * ListBox1.SelCount);
dcsCount := 0;
{Loop though the printers that are selected and create dc's for each}
for i := 0 to (ListBox1.Items.Count - 1)do
begin
{Loop through the list box to find the selected printers}
if ListBox1.Selected then
begin
{Allocate and get the Printer, Driver and port from the listboxs}
{in the form of a null terminated string}
GetMem(pPrinter, Length(ListBox1.Items) + 1);
GetMem(pDriver, Length(ListBox2.Items) + 1);
GetMem(pPort, Length(ListBox3.Items) + 1);
StrPCopy(pPrinter, ListBox1.Items);
StrPCopy(pDriver, ListBox2.Items);
StrPCopy(pPort, ListBox3.Items);
{Attempt to create a dc - notes 1) Not all printers can}
{support multiple dc's at one time. 2) CreateDc requires}
{different parameters if Win32 andor Win32 under NT.}
{Sixteen bit win apps get standard handling}
{$IFDEF WIN32}
GetVersionEx(osv);
if osv.dwPlatformId = VER_PLATFORM_WIN32_NT then
dc := CreateDc(pDriver, pPrinter, nil, nil) else
dc := CreateDc(nil, pPrinter, nil, nil);
{$else
}
dc := CreateDc(pDriver, pPrinter, pPort, nil);
{$ENDIF}
FreeMem(pPrinter, Length(ListBox1.Items));
FreeMem(pDriver, Length(ListBox2.Items));
FreeMem(pPort, Length(ListBox3.Items));
{If the dc is valid, then
lets save it to our array}
{and increment our count}
if dc <> 0 then
begin
dcs^[dcsCount] := dc;
inc(dcsCOunt);
end;
end;
end;
{If we ended up with a valid array of dc's then
let's print}
if dcsCount > 0 then
begin

{Fill Out thedo
cInfo structure for the spooler}
{and start ado
cument for each printer}
GetMem(DocInfo.lpszDocName, 32);
for i := 0 to (dcsCount - 1)do
begin
FillChar(DocInfo, sizeof(DocInfo), #0);
do
cInfo.cbSize := SizeOf(DocInfo);
StrPCopy(DocInfo.lpszDocName, 'Testdo
c' + IntToStr(i));
StartDoc(dcs^,do
cInfo);
end;
FreeMem(DocInfo.lpszDocName, 32);
{Start a page for each printer}
for i := 0 to (dcsCount - 1)do
StartPage(dcs^);
{Print something}
for i := 0 to (dcsCount - 1)do
TextOut(dcs^, 200, 200, 'Test', 4);
{End a page for each printer}
for i := 0 to (dcsCount - 1)do
EndPage(dcs^);
{End thedo
cument for each printer}
for i := 0 to (dcsCount - 1)do
EndDoc(dcs^);
{Delete the dc for each printer}
for i := 0 to (dcsCount - 1)do
DeleteDc(dcs^);
end;
{dcsCount}
{Free our array of printers}
FreeMem(dcs, sizeof(hdc) * ListBox1.SelCount);
{If range checking was on when we started then
turn it back on}
{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
end;

end.

 
你的回答对我没有帮助
 
function TTranPrn.Print(Var JobId:integer) :boolean;
var
pFileName:PChar;
pDocName:PChar;
pPrnName:PAnsiChar;
hPrinter:THandle;
DocInfo:TDocInfo1A;
tmp:array[0..7] of Char;
wcnt:DWORD;
s:string;
f:file;
i:integer;
begin
// event, before printe
if Assigned(f_OnBeforePrint) then
f_OnBeforePrint(self);
// set value.
pFileName := PChar(f_FileName);
// file name.
pDocName := PChar(f_DocumentName);
//do
cument name.
pPrnName := PAnsiChar(f_PrinterName);
// printer name.
// checkdo
cument name.
if pDocName^ = #0 then
pDocName := pFileName;
// get printer handle.
if OpenPrinterA( pPrnName,hPrinter,nil ) = False then
begin
ErrorEvent(F_OnOpenPrinterError, 'Error:OpenPrinter!' );
Result := False;
exit;
end;

// initdo
cInfo.
do
cInfo.pDocName := pDocName;
//do
cument name.
do
cInfo.pOutputFile := NIL;
// output filename.
do
cInfo.pDatatype := NIL;
// data type.
// open file.
try
AssignFile( f, pFileName );
Reset( f, 1 );
except
ErrorEvent(F_OnOpenFileError, 'Error:OpenFile!' );
Result := False;
exit;
end;

// start print spool.do
cInfo structure 1 type. NT3.51 only type 1.
JObID:= StartDocPrinterA( hPrinter,1,@DocInfo);
if JObID= 0 then
begin
ErrorEvent( F_OnStartDocPrinterError, 'Error:StartDocPrinter!');
Result := False;
exit;
end;

// loop of reading file and spooling file.
while not eof(f)do
begin

// process.
application.ProcessMessages;
// read byte.
i := 1;
BlockRead( f, tmp, i );
// event, After printe
if Assigned(f_OnAfterReadByte) then
f_OnAfterReadByte(self);
// set printer spooler
if WritePrinter( hPrinter, @tmp, i, wcnt ) = False then
begin
ErrorEvent( F_OnWritePrinterError, 'Error:WritePrinter!');
AbortPrinter( hPrinter );
EndDocPrinter( hPrinter);
ClosePrinter( hPrinter );
Result := False;
exit;
end;

end;

// close file
CloseFile(f);
// end spool.
// If the function fails, the return value should be FALSE.
// To get extended error information, call GetLastError.
Result := EndDocPrinter(hPrinter);
if not Result then
ErrorEvent( F_OnWritePrinterError, 'Error:EndDocPrinter!');
// close printer handle.
ClosePrinter( hPrinter );
// event, After print
if Result and Assigned(f_OnAfterPrint) then
f_OnAfterPrint(self);
end;
 
如果用打印机名称,可以动态获取索引号吧
uses Printers;
Printers.Printer.Printers.IndexOf('PrinterName');
 
后退
顶部