如何设置默认打印机???高手进 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 找的资料 你参考一下在Form上放一个Button和Combobox,在Uses中加入Printers,其余相关代码如下procedure TForm1.FormActivate(Sender: TObject);beginComboBox1.Items := Printer.Printers; {populates ComboBox}ComboBox1.ItemIndex := Printer.PrinterIndex;end;procedure TForm1.Button1Click(Sender: TObject);var Device: array[0..255] of Char; Driver: array[0..255] of char; Port: array[0..255] of char; s : array[0..255] of Char; hDeviceMode: THandle;begin Printer.PrinterIndex:= combobox1.ItemIndex; Printer.GetPrinter (Device, Driver, Port, hDeviceMode); StrCopy (s, Device); StrCat (s, ','); StrCat (s, Driver); StrCat (s, ','); StrCat (s, Port); WriteProfileString ('windows', 'device', s); StrCopy (s, 'windows'); SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));end;可在Win2000下设置默认打印机 //设置默认打印机//参考如下代码uses printers;procedure SetDefaultPrinter(const printerindex:integer);Var FHandle : THandle; HPrt : THandle; PrtInfo5: PPrinterInfo5; FDevice: array[0..79] of char; FDriver: array[0..79] of char; FPort: array[0..79] of char;begin {printerindex为选中打印机的索引,如果使用打印机名称,则此句可忽略} Printer.PrinterIndex := printerindex; Printer.GetPrinter (FDevice, FDriver, FPort, FHandle); OpenPrinter(FDevice, HPrt, nil); if HPrt = 0 then raise(Exception.Create('不能打开打印机')); try PrtInfo5 := GetPrinterInfo5(HPrt); PrtInfo5.Attributes := PrtInfo5.Attributes + PRINTER_ATTRIBUTE_DEFAULT; SetPrinter(HPrt,5,PrtInfo5,PRINTER_CONTROL_SET_STATUS); FreeMem(PrtInfo5); finally ClosePrinter(HPrt); end;end;//设置打印参数是要修改TDeviceMode结构var ADevice, ADriver, APort:array [0..255] of Char; DeviceHandle:THandle; DevMode:PDeviceMode;//TDeviceMode指针begin {首先获取TPrinter的DeviceMode结构的句柄} PrinterGetPrinter(ADevice, ADriver, APort, DeviceHandle); {如果句柄是0, 表示打印机没有装载} if DeviceHandle=0 then begin Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPritner(ADevice, ADriver, APort, DeviceHandle); end; {如果DeviceHandle还是0, 表示有错误发生。否则,就调用} {GlobalLock来获取TDeviceMode结构的指针} if DeviceHandle=0 then Raise Exception.Create('Could Not Initialize TDeviceMode structure') else DevMode:=GlobalLock(DeviceHandle); {下面是设置纸张大小} with DevMode^ do begin dmFields:=dmFields or DM_PAPERSIZE; dmPaperSize:=DMPADER_LETTER;//LETTER,8-1/2 {如果纸张大小由dmPaperWidth和dmPaperLength设置,则dmPaperSize的值可以设为0} //dmFields:=dmFields or DM_PAPERLENGTH or DM_PAPERWIDTH; //dmPaperLength:=somelength; //dmPaperWidth:=somewidth; end; if not DeviceHandle=0 then GlobalUnlock(DeviceHandle);end; //补充//getprinterinfo5函数原型uses Windows, WinSpool; function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;implementationfunction GetPrinterInfo(Printer: THandle; Level: Integer): Pointer;var BytesRequired: DWORD;begin { Find out the required size of the structure } GetPrinter(Printer, Level, nil, 0, @BytesRequired); GetMem(Result, BytesRequired); GetPrinter(Printer, Level, Result, BytesRequired, @BytesRequired);end;function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;begin Result := GetPrinterInfo(Printer,5);end; 请教:数据显示问题 【求救帖】数据库加密问题 讨论一个小问题 关与Tstringgrid的问题 请教高手!Access数据转到SQLServer的类型改变问题! 关于跨表显示的问题!!在这先谢了 tdbgrid控件的选择 怎样在dll里访问asp啊? 急急!請高手們指點,如何用Install Shell打包配置SQL Server Client Network Utility? sql 语句的难题!急 请问在Delphi中如何将数据库中的内容提出来保存成xml文件,请给出demo,谢谢 登陆问题!!
begin
ComboBox1.Items := Printer.Printers; {populates ComboBox}
ComboBox1.ItemIndex := Printer.PrinterIndex;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Device: array[0..255] of Char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
s : array[0..255] of Char;
hDeviceMode: THandle;
begin
Printer.PrinterIndex:= combobox1.ItemIndex;
Printer.GetPrinter (Device, Driver, Port, hDeviceMode);
StrCopy (s, Device);
StrCat (s, ',');
StrCat (s, Driver);
StrCat (s, ',');
StrCat (s, Port);
WriteProfileString ('windows', 'device', s);
StrCopy (s, 'windows');
SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@s));
end;可在Win2000下设置默认打印机
//参考如下代码uses printers;procedure SetDefaultPrinter(const printerindex:integer);
Var
FHandle : THandle;
HPrt : THandle;
PrtInfo5: PPrinterInfo5;
FDevice: array[0..79] of char;
FDriver: array[0..79] of char;
FPort: array[0..79] of char;begin {printerindex为选中打印机的索引,如果使用打印机名称,则此句可忽略}
Printer.PrinterIndex := printerindex; Printer.GetPrinter (FDevice, FDriver, FPort, FHandle);
OpenPrinter(FDevice, HPrt, nil);
if HPrt = 0 then
raise(Exception.Create('不能打开打印机'));
try
PrtInfo5 := GetPrinterInfo5(HPrt);
PrtInfo5.Attributes := PrtInfo5.Attributes +
PRINTER_ATTRIBUTE_DEFAULT;
SetPrinter(HPrt,5,PrtInfo5,PRINTER_CONTROL_SET_STATUS);
FreeMem(PrtInfo5);
finally
ClosePrinter(HPrt);
end;end;//设置打印参数是要修改TDeviceMode结构var
ADevice, ADriver, APort:array [0..255] of Char;
DeviceHandle:THandle;
DevMode:PDeviceMode;//TDeviceMode指针
begin
{首先获取TPrinter的DeviceMode结构的句柄}
PrinterGetPrinter(ADevice, ADriver, APort, DeviceHandle);
{如果句柄是0, 表示打印机没有装载}
if DeviceHandle=0 then
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPritner(ADevice, ADriver, APort, DeviceHandle);
end;
{如果DeviceHandle还是0, 表示有错误发生。否则,就调用}
{GlobalLock来获取TDeviceMode结构的指针}
if DeviceHandle=0 then
Raise Exception.Create('Could Not Initialize TDeviceMode structure')
else
DevMode:=GlobalLock(DeviceHandle);
{下面是设置纸张大小}
with DevMode^ do
begin
dmFields:=dmFields or DM_PAPERSIZE;
dmPaperSize:=DMPADER_LETTER;//LETTER,8-1/2
{如果纸张大小由dmPaperWidth和dmPaperLength设置,则dmPaperSize的值可以设为0}
//dmFields:=dmFields or DM_PAPERLENGTH or DM_PAPERWIDTH;
//dmPaperLength:=somelength;
//dmPaperWidth:=somewidth;
end;
if not DeviceHandle=0 then
GlobalUnlock(DeviceHandle);
end;
//getprinterinfo5函数原型uses
Windows, WinSpool; function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;implementationfunction GetPrinterInfo(Printer: THandle; Level: Integer):
Pointer;
var
BytesRequired: DWORD;
begin
{ Find out the required size of the structure }
GetPrinter(Printer, Level, nil, 0, @BytesRequired);
GetMem(Result, BytesRequired);
GetPrinter(Printer, Level, Result, BytesRequired,
@BytesRequired);
end;function GetPrinterInfo5(Printer: THandle): PPrinterInfo5;
begin
Result := GetPrinterInfo(Printer,5);
end;