如题

解决方案 »

  1.   

    找的资料 你参考一下在Form上放一个Button和Combobox,在Uses中加入Printers,其余相关代码如下procedure TForm1.FormActivate(Sender: TObject);
    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下设置默认打印机
      

  2.   

    //设置默认打印机
    //参考如下代码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;
      

  3.   

    //补充
    //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;