我的系统是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就不会有这个问题
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就不会有这个问题
解决方案 »
- is,as操作符判断接口
- Canvas dos not Allow Drawing 是什么错误,怎么解决?
- 如果获取Edit文本框中当前光标所在的位置
- 据说有这样新的木马技术:把DLL木马注射到其他exe文件中,使其成为木马的合法载体(如explorer.exe中),请问该如何实现?
- 在内存中读取字符串!
- 今天买了台电脑--终于有自己的电脑了!高兴啊!
- 高手来罐水 高手来罐水 高手来罐水 高手来罐水-------谢谢 当场给分
- 菜鸟高分请教老问题:相对路径?(分不够再加加)以前的帖子打不开了
- dbgrideh里 怎样不显示0或者0.00 而其他的则显示为如:5.00 6.10 1.11
- 高分邀你来相助,Delphi高手请进:关于局域网收发邮件问题?
- TXMLTransformClient问题?
- 如何在文件在某些位置插入一段内容?
设置默认打印机
窗体上有个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;
前人写的
具体见: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;
谢谢你的解答,可是第一个程序需要uses那些东东呢?
谢谢你的解答,可是我发现还是不能解决我的问题 我用rave做的报表,用你的方法改了默认打印机之后(好像还是没有成功,因为显示的是没有了默认打印机)rvsystem就是打死不认默认打印机,老是往自己默认的pdf里面打印 请问如何更改rvsystem的打印机设置