如何更改默认打印机
我试图在列出打印机索引的组合框的更改事件中更改默认打印机。我使用“打印机”来获取打印机索引,但实际打印是使用专有打印代码完成的,该代码允许直接打印 pdf 并更轻松地进行页面布局。我试图使用下面的代码来更改默认打印机,然后我的打印代码将打印到该打印机。然而,当执行这行代码时,程序变得无响应,没有任何错误或程序不响应消息:
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1));
这是完整的函数。
function TMainFrm.SetDefaultPrinter(const PrinterName: string): boolean;
// Printername is bv: '\\MYPRINTER\HP5-k'
var
s2 : string;
dum1 : Pchar;
xx, qq : integer;
const
cs1 : pchar = 'Windows';
cs2 : pchar = 'Device';
cs3 : pchar = 'Devices';
cs4 : pchar = #0;
begin
xx := 254;
GetMem( dum1, xx);
Result := False;
try
qq := GetProfileString( cs3, pchar( PrinterName ), #0, dum1, xx);
if (qq > 0) and (trim( strpas( dum1 )) <> '') then
begin
s2 := PrinterName + ',' + strpas( dum1 );
while GetProfileString( cs1, cs2, cs4, dum1, xx) > 0 do
WriteProfileString( cs1, cs2, #0);
WriteProfileString( cs1, cs2, pchar( s2 ));
case Win32Platform of
VER_PLATFORM_WIN32_NT :
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1));
VER_PLATFORM_WIN32_WINDOWS :
SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE, 0,LongInt(cs1));
end; { case }
Result := True;
end;
finally
FreeMem( dum1 );
end;
end;
有人有任何提示或更好的方法来做到这一点吗?
附带说明一下,这不是我的职责。这是我在寻找问题解决方案时捡到的一段代码。
更多信息:
打印作业实际上是正在打印的 PDF
ShellExecute(Application.Handle, 'print', PChar(sPath), nil, nil, SW_HIDE);
目标是将默认打印机更改为选定的打印机,以便将 pdf 打印到所需的打印机,然后在退出应用程序时将打印机返回到原始默认值
I am attempting to change the default printer in the on change event of a combo box that list the printer index. I use "Printers" to get the printer index but the actual printing is done with proprietary print code that allows for direct to pdf printing and easier page layout. I am trying to use the below code to change the default printer and then my print code will print to that printer. However the program becomes unresponsive without any errors or program not responding messages when this line of code executes:
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1));
Here is the full function.
function TMainFrm.SetDefaultPrinter(const PrinterName: string): boolean;
// Printername is bv: '\\MYPRINTER\HP5-k'
var
s2 : string;
dum1 : Pchar;
xx, qq : integer;
const
cs1 : pchar = 'Windows';
cs2 : pchar = 'Device';
cs3 : pchar = 'Devices';
cs4 : pchar = #0;
begin
xx := 254;
GetMem( dum1, xx);
Result := False;
try
qq := GetProfileString( cs3, pchar( PrinterName ), #0, dum1, xx);
if (qq > 0) and (trim( strpas( dum1 )) <> '') then
begin
s2 := PrinterName + ',' + strpas( dum1 );
while GetProfileString( cs1, cs2, cs4, dum1, xx) > 0 do
WriteProfileString( cs1, cs2, #0);
WriteProfileString( cs1, cs2, pchar( s2 ));
case Win32Platform of
VER_PLATFORM_WIN32_NT :
SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1));
VER_PLATFORM_WIN32_WINDOWS :
SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE, 0,LongInt(cs1));
end; { case }
Result := True;
end;
finally
FreeMem( dum1 );
end;
end;
Anyone have any tips or a better way to do this?
As a side note this is not my function. It's a piece of code I picked up while searching for a solution to my problem.
Few more pieces of info:
The print job is actually a PDF being printed using
ShellExecute(Application.Handle, 'print', PChar(sPath), nil, nil, SW_HIDE);
The goal is to change the default printer to the selected printer in order to print the pdf to the desired printer and then return the printer to the original default on exit of the application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
Win32_Printer
WMI 类列出打印机和设置默认打印机
< /a> 设置默认打印机的方法。Try using the
Win32_Printer
WMI class to list the printers and theSetDefaultPrinter
method to set the default printer.只需简单浏览一下 此链接,您就会发现您错过了最关键的函数 SetDefaultPrinterA/SetDefaultPrinterW在“winspool.drv”中,
广播消息的设计也是为了对其他正在运行的程序有礼貌,让它们知道某些内容已经更改了默认打印机,即使在上面的文章中,它似乎也没有注意到结果,以便您可以更改对 PostMessage 的调用
Just a brief look at this link and it appears you're mising the most critical function SetDefaultPrinterA/SetDefaultPrinterW in 'winspool.drv'
Also the broadcast message is designed to be polite to other running programs to let them know something has changed the default printer, even in the above article it doesn't seem to pay any attention to the result so you could change the call to PostMessage
我还尝试使用“SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1))”代码块,但我的程序挂起。因此,我尝试简单地将 SetDefaultPrinter 编写为本地函数,然后就不会出现挂起情况。这是适合我的代码。
I also tried to use the "SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1))" code block and my program was hang. So, I tried to simply write the SetDefaultPrinter as local function and then no hang occours. Here is the code that works fine for me.