装了好几台打印机,怎样得到默认打印机的名称?以及默认打印机所选用的纸装名称?

解决方案 »

  1.   

    procedure TForm1.FormCreate(Sender: TObject);begincb1.items.assign(printer.printers);end;procedure TForm1.Button1Click(Sender: TObject);var LPrinter:string;PrinIniFile:Tinifile;LStr:string;beginLStr:=printer.Printers[cb1.itemindex];delete(Lstr,pos(' on ',Lstr),Length(LStr));PrinIniFile:=TIniFile.Create('WIN.ini');tryLPrinter:=PrinIniFile.ReadString('Devices',LStr,'');PrinIniFile.writestring('windows','device',LStr+','+LPrinter);finallyPrinIniFile.free;end;end;***********************uses printers; procedure SetDefaultPrinter(const printerindex:integer);VarFHandle : 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 thenraise(Exception.Create('不能打开打印机'));tryPrtInfo5 := GetPrinterInfo5(HPrt);PrtInfo5.Attributes := PrtInfo5.Attributes +PRINTER_ATTRIBUTE_DEFAULT;SetPrinter(HPrt,5,PrtInfo5,PRINTER_CONTROL_SET_STATUS);FreeMem(PrtInfo5);finallyClosePrinter(HPrt);end;end; 
      

  2.   

    给段代码参考一下。
    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
      

  3.   

    ...get / set the default printer? 
     
     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; 
      

  4.   

    printer.GetPrinter(adevice,adriver,aport,printdev);