如何选择不同的打印机打印(100分)

  • 主题发起人 主题发起人 vxdwan
  • 开始时间 开始时间
V

vxdwan

Unregistered / Unconfirmed
GUEST, unregistred user!
我的电脑装有两台打印机,如何通过程序控制打印到不同的打印机
PintBox.Items.Assign(Printer.Printers);//下拉列表取得安装的打印机
PintBox.Text:=printer.Printers[printer.printerindex];//显示默认打印机
printer.PrinterIndex:=pintbox.ItemIndex;//选择打印机,不行??
quicprep.preview;//总是机器默认的打印机,请高手指教
 
用printsetupdialog来选择不同的打印机
 
QuickRep1.PrinterSettings.PrinterIndex
QuickRep1.PrinterSettings.ApplySettings
 
我不想通过printsetupdialog来设定,我的意思是:
我有一个程序有一部分功能只能由某一台打印机(只能用针式打印机),
另外一部分功能报表部分用激光打印机打印,
在操作过程中,操作人员不想总是用printsetupdialog来选择打印机,
容易产生误操作(忘记选择打印机),有没有办法自己通过程序来控制,
即printsetupdialog选择打印机的功能是怎么实现的
 
QuickRep1.PrinterSettings.PrinterIndex不行么?
我以前都是這樣做的
 
Printer.PrinterIndex就可以。
我做的一个程序也是一台针打打票证,一台激打打报表
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
MAXPRINTERBUFFER = 8000;
MAXPRINTERNAME = 500;
MAXPRINTERINFO = 50;
type
TPrinterBuffer = array[0..MAXPRINTERBUFFER - 1] of char;
TForm1 = class(TForm)
ListBox1: TListBox;
Button2: TButton;
Button1: TButton;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure GetPrinterNames;
function ParseNames(const namebuffer: TPrinterBuffer;
var startPos: integer): string;
function SetPrinter(const PrinterName : String) : boolean;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
printerNames: TStringList;
defaultPrinter: integer;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
var
x : integer;
begin
try
for x := 0 to printerNames.Count -1do
begin
If ListBox1.Selected[x] then
begin
if (SetPrinter(ListBox1.Items.Strings[x]))
then
label1.Caption := 'Printer set to ' + ListBox1.Items.Strings[x]
else
label1.Caption := 'Printer not set';
end;
end;
except
label1.Caption := 'An error occured while setting the printer';
end;
end;

procedure TForm1.GetPrinterNames;
var
buffer: TPrinterBuffer;
currPos: integer;
printerName: string;
begin
printerNames.Free;
printerNames := TStringList.Create;
if GetProfileString(PChar('PrinterPorts'), nil, '', buffer, MAXPRINTERBUFFER) > 0 then
begin
currPos := 0;
while (true)do
begin
printerName := ParseNames(buffer, currPos);
if printerName <> '' then
printerNames.Add(printerName)
else
break;
end;
end;
end;

function TForm1.ParseNames(const namebuffer: TPrinterBuffer;
var startPos: integer): string;
var
i, j, NameLength: integer;
str: string;
begin
result := '';
if (startPos > High(namebuffer)) or (namebuffer[startPos] = Chr(0))
then
exit;
for i := startPos to High(namebuffer)do
begin
if namebuffer = Chr(0)
then
begin
nameLength := i - startPos;
SetLength(str, nameLength);
for j := 0 to nameLength - 1do
str[j+1] := namebuffer[startPos + j];
result := str;
startPos := i + 1;
break;
end;
end;
end;

function TForm1.SetPrinter(const PrinterName: String): boolean;
var
s2 : string;
dum1 : Pchar;

xx, qq : integer;
const
cs1 : pchar = 'Windows';
cs2 : pchar = 'Device';
cs3 : pchar = 'Devices';
cs4 : pchar = #0;
begin
xx := 254;
GetMem( dum1, xx);
Result := False;
try
qq := GetProfileString( cs3, pchar( printerName ), #0, dum1, xx);
if (qq > 0) and (trim( strpas( dum1 )) <> '')
then
begin
s2 := PrinterName + ',' + strpas( dum1 );
while GetProfileString( cs1, cs2, cs4, dum1, xx) > 0do
WriteProfileString( cs1, cs2, #0);
WriteProfileString( cs1, cs2, pchar( s2 ));
case Win32Platform of
VER_PLATFORM_WIN32_NT :
// SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(cs1));
// VER_PLATFORM_WIN32_WINDOWS :
// SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE, 0, LongInt(cs1));
end;

Result := True;
end;
finally
FreeMem( dum1 );
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetPrinterNames;
Listbox1.Items.AddStrings(PrinterNames);
end;

end.
 
接受答案了.
 
后退
顶部