我的系统是xp有两个虚拟打印机:pdf(默认)和Microsoft Office Document Image Writer为了改变默认打印机我是这么写的procedure TForm1.BitBtn1Click(Sender: TObject);
var
  pDevice : pChar;
  pDriver : pChar;
  pPort   : pChar;
  hDMode : THandle;
  winini : TiniFile;
  WinIniFileName : array[0..Max_PATH] of char;
  s : array[0..64] of char;
begin
  if PrintDialog1.Execute then begin
    GetMem(pDevice, cchDeviceName);
    GetMem(pDriver, MAX_PATH);
    GetMem(pPort, MAX_PATH);
    Printer.GetPrinter(pDevice, pDriver, pPort, hDMode);
    if lStrLen(pDriver) = 0 then begin
      GetProfileString('Devices', pDevice, '', pDriver, MAX_PATH);
      pDriver[pos(',', pDriver) - 1] := #0;
    end;
    if lStrLen(pPort) = 0 then begin
      GetProfileString('Devices', pDevice, '', pPort, MAX_PATH);
      lStrCpy(pPort,  @pPort[lStrLen(pPort)+2]);
    end;
    Memo1.Lines.Add('Device := ' + StrPas(pDevice));
    Memo1.Lines.Add('Driver := ' + StrPas(pDriver));
    Memo1.Lines.Add('Port := ' + StrPas(pPort));
    //以上取得欲更改的打印机名    GetWindowsDirectory(WinIniFileName,Sizeof(WinIniFilename));
    StrCat(WinIniFileName,'\win.ini');//找到设置打印机的路径
    WinIni := TiniFile.create(WinIniFileName);
    try
    WinIni.WriteString('Windows',
                       'Device',
                       StrPas(pDevice)
                       );//更改默认打印机
    finally
    WinIni.free;
    end;    StrCOpy(s,'Windows');
    SendMessage(HWND_BROADCAST,WM_WININICHANGE,0,LongInt(@S));
    //通知系统刷新
    FreeMem(pDevice, cchDeviceName);
    FreeMem(pDriver, MAX_PATH);   //发生错误
    FreeMem(pPort, MAX_PATH);
    //释放内存
  end;
end;可是最后发生了错误1.运行完毕之后并没有真正改变默认打印机,反而使系统没有了默认打印机(即取消了打印机默认)
2.每次运行到FreeMem(pDriver, MAX_PATH); 就发生错误,报错是写内存错误,然而如果系统默认打印机是Microsoft Office Document Image Writer就不会有这个问题

解决方案 »

  1.   

    //给你段代码,希望你帮你
    设置默认打印机    
        
    窗体上有个TComBox,命名为cb1窗体名为Form1procedure 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.   

    在给你个例子
    前人写的
    具体见:http://dear12399.51.net/free_resource/other_1.htm
    ==========================================================
    改变默认的打印机: 如:var  winini:tinifile;  wininifilename:array[0..max_path] of char;  s:array[0..64] of char;begin  getwindowsdirectory(wininifilename,sizeof(wininifilename));  strcat(wininifilename,'win.ini');  winini:=tinifile.create(wininifilename);  try    winini.writestring('windows',device',hp laserjet series II,hppcl,LPT1:');  finally    winini.free;  end;  strcopy(s,'windows');  sendmessage(hwnd_broadcast,wm_wininichange,0,longint(@s));end;
      

  3.   

    Kshape兄:
       谢谢你的解答,可是第一个程序需要uses那些东东呢?
      

  4.   

    Kshape兄:
      
       谢谢你的解答,可是我发现还是不能解决我的问题   我用rave做的报表,用你的方法改了默认打印机之后(好像还是没有成功,因为显示的是没有了默认打印机)rvsystem就是打死不认默认打印机,老是往自己默认的pdf里面打印   请问如何更改rvsystem的打印机设置