装了好几台打印机,怎样得到默认打印机的名称?以及默认打印机所选用的纸装名称?
解决方案 »
- 怎樣得到控件相對屏幕的位置???????????
- 如何删除ListBox中的指定项?
- 使用Twebbrowser控件直接载入字符串,如果内容太大,会直接显示网页源码.而不是html格式
- 大家帮帮忙!一个关于打印的问题!急啊!!!!!!!!
- Delphi如何实现类似vb InstrRev函数的功能返回字符串最后一次出现的位置!
- 怎样用拨号网络连接到WINXP的机子上啊?我怎么找不到它的拨号网络服务器在哪儿...
- 谁能告诉我,如何在代码里对treeview中的node进行加亮
- DBgrideh控件用过的人请进来一下
- 如何获取资源管理器传给自己程序的参数?
- 超难sql表abd 字段 name(名称) oldcount(原先数量) nowcount(现在数量) gcount(放声数量) why(发生原因) id(流水编码)
- 为一个表,增加一个布尔类型的虚拟字段,用来判定某条记录是否被选择?
- 如何使MDIChild中的一个窗口设成激活状态???
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, QPrinters, Printers, FR_Class;type
TForm1 = class(TForm)
PrinterBox: TComboBox;
Btn_SetDefaultPrint: TButton;
Btn_Report: TButton;
frReport1: TfrReport;
Label1: TLabel;
procedure FormShow(Sender: TObject);
procedure Btn_ReportClick(Sender: TObject);
private
{ Private declarations }
procedure GetPrinterList;
procedure SetDefaultPrinter;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.GetPrinterList;
begin
PrinterBox.Items:=Printer.Printers;
PrinterBox.ItemIndex:=Printer.PrinterIndex
end;
procedure TForm1.SetDefaultPrinter;
var
ADevice,ADriver,APort,s:array[0..255] of Char;
DeviceHandle:THandle;
DevMode:PDeviceMode;
begin
Printer.PrinterIndex:=PrinterBox.ItemIndex;
Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle);
StrCopy(s,ADevice);
StrCat(s,',');
StrCat(s,ADriver);
StrCat(s,',');
StrCat(s,APort);
WriteProfileString('windows','device',s);
StrCopy(s,'windows');
SendMessage(HWND_BROADCAST,WM_WININICHANGE,0,longint(@s));
Printer.GetPrinter(ADevice,ADriver,APort,DeviceHandle);
Printer.Refresh;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
GetPrinterList;
end;procedure TForm1.Btn_ReportClick(Sender: TObject);
begin
Printer.Refresh;
frReport1.LoadFromFile('C:\test1.frf');
frReport1.ShowReport;
end;end.
窗体文件
object Form1: TForm1
Left = 187
Top = 117
Width = 427
Height = 136
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 80
Top = 32
Width = 60
Height = 13
Caption = '打印机列表'
end
object PrinterBox: TComboBox
Left = 152
Top = 24
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 0
Text = 'PrinterBox'
end
object Btn_SetDefaultPrint: TButton
Left = 88
Top = 56
Width = 105
Height = 25
Caption = '设置默认打印机'
TabOrder = 1
end
object Btn_Report: TButton
Left = 208
Top = 56
Width = 75
Height = 25
Caption = '打印报表'
TabOrder = 2
OnClick = Btn_ReportClick
end
object frReport1: TfrReport
InitialZoom = pzDefault
PreviewButtons = [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbHelp, pbExit]
Left = 24
Top = 16
ReportForm = {18000000}
end
end
uses
Printers, Messages; function GetDefaultPrinter: string;
var
ResStr: array[0..255] of Char;
begin
GetProfileString('Windows', 'device', '', ResStr, 255);
Result := StrPas(ResStr);
end; procedure SetDefaultPrinter1(NewDefPrinter: string);
var
ResStr: array[0..255] of Char;
begin
StrPCopy(ResStr, NewdefPrinter);
WriteProfileString('windows', 'device', ResStr);
StrCopy(ResStr, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr));
end; procedure SetDefaultPrinter2(PrinterName: string);
var
I: Integer;
Device: PChar;
Driver: PChar;
Port: PChar;
HdeviceMode: THandle;
aPrinter: TPrinter;
begin
Printer.PrinterIndex := -1;
GetMem(Device, 255);
GetMem(Driver, 255);
GetMem(Port, 255);
aPrinter := TPrinter.Create;
try
for I := 0 to Printer.Printers.Count - 1 do
begin
if Printer.Printers = PrinterName then
begin
aprinter.PrinterIndex := i;
aPrinter.getprinter(device, driver, port, HdeviceMode);
StrCat(Device, ',');
StrCat(Device, Driver);
StrCat(Device, Port);
WriteProfileString('windows', 'device', Device);
StrCopy(Device, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE,
0, Longint(@Device));
end;
end;
finally
aPrinter.Free;
end;
FreeMem(Device, 255);
FreeMem(Driver, 255);
FreeMem(Port, 255);
end; procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := GetDefaultPrinter2;
end; //Fill the combobox with all available printers
procedure TForm1.FormCreate(Sender: TObject);
begin
Combobox1.Items.Clear;
Combobox1.Items.AddStrings(Printer.Printers);
end; //Set the selected printer in the combobox as default printer
procedure TForm1.Button2Click(Sender: TObject);
begin
SetDefaultPrinter(Combobox1.Text);
end;