找回密码
 加入
搜索
查看: 103|回复: 4

代码添加自定义纸张不成功,请教高手

[复制链接]
发表于 2024-6-4 12:27:54 | 显示全部楼层 |阅读模式

#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
以上代码添加自定义纸张不成功,请教高手

发表于 2024-6-4 16:10:19 | 显示全部楼层
这些问号冒号的,不知有没有影响




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2024-6-4 19:25:49 | 显示全部楼层
顽固不化 发表于 2024-6-4 16:10
这些问号冒号的,不知有没有影响

这种是三目运算表达式。3.3.10.x 及以上Au版本支持的语法。可以看做是 If .... Else ... 的简化表达式
发表于 2024-6-4 21:09:34 | 显示全部楼层
afan 发表于 2024-6-4 19:25
这种是三目运算表达式。3.3.10.x 及以上Au版本支持的语法。可以看做是 If .... Else ... 的简化表达式 ...

哦,又学了一招。
发表于 2024-6-4 23:01:17 | 显示全部楼层
好久没来了,居然看到A大还在回复。你们好,15年了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-15 03:28 , Processed in 0.095940 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表