|
#include <WinAPI.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#RequireAdmin
Opt("WinTitleMatchMode", 2)
Global Const $tagPRINTER_DEFAULTS = "ptr pDatatype;ptr pDevMode;int DesiredAccess"
Global Const $tagFORM_INFO_1 = "wchar Name[64];uint Size;tagSIZE PaperSize;tagRECT ImageableArea"
; Define custom paper
_SetCustomPaper("123", 210, 2794)
Func _SetCustomPaper($sPaperName, $iPaperWidth, $iPaperHeight, $iLeftMargin = Default, $iRightMargin = Default, $iTopMargin = Default, $iBottomMargin = Default)
Local $sDefaultPrinter, $hPrinter, $tFormStruct, $iRet
$sDefaultPrinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")
If @error Then
MsgBox(16, "Error", "Failed to retrieve default printer! Error code: " & @error)
Return False
EndIf
$sDefaultPrinter = StringLeft($sDefaultPrinter, StringInStr($sDefaultPrinter, ",", 0, -1) - 1)
; Open printer
ConsoleWrite("Opening printer: " & $sDefaultPrinter & @CRLF)
Local $hPrinter = 0 ; 声明一个本地变量用于存储打印机句柄
If Not _WinAPI_OpenPrinter($sDefaultPrinter, $hPrinter, 0) Then
MsgBox(16, "Error", "Failed to open printer!")
Return False
EndIf
; Define form structure
$tFormStruct = DllStructCreate("char FormName[32]; uint Width; uint Height; uint LeftMargin; uint RightMargin; uint TopMargin; uint BottomMargin; uint Flags")
DllStructSetData($tFormStruct, "FormName", $sPaperName)
DllStructSetData($tFormStruct, "Width", $iPaperWidth * 1000)
DllStructSetData($tFormStruct, "Height", $iPaperHeight * 100)
DllStructSetData($tFormStruct, "LeftMargin", ($iLeftMargin == Default) ? 0 : $iLeftMargin * 1000)
DllStructSetData($tFormStruct, "RightMargin", ($iRightMargin == Default) ? ($iPaperWidth * 1000 - 1) : $iRightMargin * 1000)
DllStructSetData($tFormStruct, "TopMargin", ($iTopMargin == Default) ? 0 : $iTopMargin * 1000)
DllStructSetData($tFormStruct, "BottomMargin", ($iBottomMargin == Default) ? ($iPaperHeight * 100 - 1) : $iBottomMargin * 100)
DllStructSetData($tFormStruct, "Flags", 0)
; Add form
ConsoleWrite("Adding custom paper form..." & @CRLF)
$iRet = _WinAPI_AddForm($hPrinter, 1, $sPaperName, $iPaperWidth, $iPaperHeight, $iLeftMargin, $iRightMargin, $iTopMargin, $iBottomMargin)
; Close printer
ConsoleWrite("Closing printer..." & @CRLF)
_WinAPI_ClosePrinter($hPrinter)
If $iRet = 0 Then
MsgBox(16, "Error", "Failed to add custom paper form!")
Return False
Else
MsgBox(64, "Success", "Custom paper form added successfully!")
Return True
EndIf
EndFunc ;==>_SetCustomPaper
Func _WinAPI_OpenPrinter($sPrinterName, ByRef $hPrinter, $iLevel = 2)
Local $pDefaults = 0, $pPrinterInfo = 0, $tPrinterDefaults, $tPrinterInfo
Local $aResult
; Initialize the PRINTER_DEFAULTS structure
$tPrinterDefaults = DllStructCreate($tagPRINTER_DEFAULTS)
$pDefaults = DllStructGetPtr($tPrinterDefaults)
; Open the printer
$aResult = DllCall("winspool.drv", "bool", "OpenPrinter", _
"str", $sPrinterName, _
"ptr", DllStructGetPtr($tPrinterDefaults), _
"ptr", 0)
If @error Or Not $aResult[0] Then
MsgBox(16, "Error", "Failed to open printer! Error code: " & @error & ", Extended: " & @extended)
Return SetError(@error, @extended, 0)
EndIf
$hPrinter = $aResult[0] ; Set $hPrinter to the returned value from DllCall
Return $hPrinter
EndFunc ;==>_WinAPI_OpenPrinter
Func _WinAPI_AddForm($hPrinter, $iLevel, $sPaperName, $iPaperWidth, $iPaperHeight, $iLeftMargin, $iRightMargin, $iTopMargin, $iBottomMargin)
Local $aResult
; Initialize the form structure
Local $tFormStruct = DllStructCreate($tagFORM_INFO_1)
DllStructSetData($tFormStruct, "Name", $sPaperName)
DllStructSetData($tFormStruct, "Size", DllStructGetSize($tFormStruct))
DllStructSetData($tFormStruct, "Width", $iPaperWidth * 1000)
DllStructSetData($tFormStruct, "Height", $iPaperHeight * 1000)
DllStructSetData($tFormStruct, "ImageableTop", $iTopMargin * 1000)
DllStructSetData($tFormStruct, "ImageableRight", ($iPaperWidth - $iRightMargin) * 1000)
DllStructSetData($tFormStruct, "ImageableLeft", $iLeftMargin * 1000)
DllStructSetData($tFormStruct, "ImageableBottom", ($iPaperHeight - $iBottomMargin) * 1000)
; Add the form
ConsoleWrite("Before DllCall: _WinAPI_AddForm" & @CRLF)
$aResult = DllCall("winspool.drv", "bool", "AddForm", "hwnd", $hPrinter, "int", $iLevel, "ptr", DllStructGetPtr($tFormStruct))
ConsoleWrite("After DllCall: _WinAPI_AddForm" & @CRLF)
If @error Or Not $aResult[0] Then
MsgBox(16, "Error", "Failed to add custom paper form! Error code: " & @error & ", Extended: " & @extended)
Return SetError(@error, @extended, 0)
EndIf
Return $aResult[0]
EndFunc ;==>_WinAPI_AddForm
Func _WinAPI_ClosePrinter($hPrinter)
Local $aResult
; Close the printer
$aResult = DllCall("winspool.drv", "bool", "ClosePrinter", _
"hwnd", $hPrinter)
If @error Or Not $aResult[0] Then Return SetError(@error, @extended, False)
Return True
EndFunc ;==>_WinAPI_ClosePrinter
以上代码添加自定义纸张不成功,请教高手
|
|