描述
SetDefaultPrinter功能设置本地计算机上当前用户的默认打印机的打印机名称。
C++ 语法
BOOL SetDefaultPrinter( __in LPCTSTR pszPrinter ); |
PowerBASIC 语法
FUNCTION SetDefaultPrinterA ( _ BYREF pszPrinter AS ASCIIZ _ ) AS LONG |
Unicode版本:
FUNCTION SetDefaultPrinterW ( _ BYREF pszPrinter AS WSTRINGZ _ ) AS LONG |
参数
pszPrinter
[in]指向包含默认打印机名称的以null结尾的字符串的指针。对于远程打印机,名称格式为\\\\服务器\\打印机名称.对于本地打印机,名称格式为打印机名称.
如果此参数为空或空字符串,即“”,则SetDefaultPrinter如果已经有默认打印机,则不执行任何操作。但是,如果没有默认打印机,SetDefaultPrinter将默认打印机设置为本地计算机上安装的打印机枚举中的第一台打印机(如果有)。
返回值
如果函数成功,则返回值为非零值。
如果函数失败,返回值为零。
备注
要在Windows 2000 / XP / Vista / Windows 7上设置默认打印机,请调用SetDefaultPrinter.要将默认打印机设置为较早的操作系统,请调用GetProfileString,WriteProfileString和SendNotifyMessage,如以下代码所示:
C++
if ((DWORD)(LOBYTE(LOWORD(GetVersion()))) < 5) {
// if this is running on Windows NT4.0 or earlier, then
// read PrinterPorts section from win.ini
// returned string should be of the form "driver,port,timeout,timeout", i.e. "winspool,LPT1:,15,45".
GetProfileString(TEXT("Windows"), TEXT("Device"), _T(",,,"), m_szName, COUNTOF(m_szName));
WriteProfileString(TEXT("Windows"), TEXT("Device"), pszPrinterName);
// Notify all open applications of the change. Note, only applications that handle the message will recognize the change.
if (WM_WININICHANGE != WM_SETTINGCHANGE) {
// Old message type; for windows 95
SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, (LPARAM)szWindows);
}
// New message type
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)szWindows);
} else {
// call SetDefaultPrinter Windows 2000 and later versions of Windows
SetDefaultPrinter(pszPrinterName);
}
PowerBASIC
IF LO(BYTE, LO(WORD, GetVersion)) < 5 THEN
' // if this is running on Windows NT4.0 or earlier, then
// read PrinterPorts section from win.ini
// returned string should be of the form "driver,port,timeout,timeout", i.e. "winspool,LPT1:,15,45".
GetProfileString("Windows", "Device", ",,,", m_szName, nSize)
WriteProfileString("Windows", "Device", pszPrinterName)
' // Notify all open applications of the change. Note, only applications that handle the message will recognize the change.
IF %WM_WININICHANGE <> %WM_SETTINGCHANGE THEN
' // Old message type; for windows 95
SendNotifyMessage(%HWND_BROADCAST, %WM_WININICHANGE, 0, VARPTR(szWindows))
END IF
' // New message type
SendNotifyMessage(%HWND_BROADCAST, %WM_SETTINGCHANGE, 0, BARPTR(szWindows))
ELSE
' // call SetDefaultPrinter Windows 2000 and later versions of Windows
SetDefaultPrinter(pszPrinterName)
END IF
使用此方法时,必须指定有效的打印机,驱动程序和端口。如果它们无效,则API不会失败,但没有定义结果。这可能会导致其他程序将打印机设置回以前的有效打印机。您可以使用EnumPrinters检索所有可用打印机的打印机名称,驱动程序名称和端口名称。
引用文件 #INCLUDE Once
WinSpool.inc