#include-once
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <FontConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>

; #INDEX# =======================================================================================================================
; Title .........: Windows Print API
; AutoIt Version : 3.2
; Description ...: Windows API calls for printing that have been translated to AutoIt functions.
; Author(s) .....: Paul Campbell (PaulIA), gafrost, Siao, Zedna, arcker, Prog@ndy, PsaltyDS, Raik, jpm, eltorro
; Dll ...........: kernel32.dll, user32.dll, gdi32.dll, comdlg32.dll, shell32.dll, ole32.dll, winspool.drv
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================
Global $g__Print_hFontOld = 0
Global $g__PixelsPerInchY
Global $g__TwipsPerPixelY
Global $g__PixelsPerInchX
Global $g__TwipsPerPixelX
Global $__g_winprint_init = 0
Global $g_current_HDC = 0
; ===============================================================================================================================

; #CONSTANTS# ===================================================================================================================
Global Const $HORZRES = 8
Global Const $VERTRES = 10
Global Const $PHYSICALWIDTH = 110
Global Const $PHYSICALHEIGHT = 111

;Global Const  $PS_SOLID = 0
;Global Const  $PS_DASH = 1
;Global Const  $PS_DOT = 2
;Global Const  $PS_DASHDOT = 3
;Global Const  $PS_DASHDOTDOT = 4
;Global Const  $PS_NULL = 5
;Global Const  $PS_INSIDEFRAME = 6
Global Const $AD_COUNTERCLOCKWISE = 1
Global Const $AD_CLOCKWISE = 2


Global Const $DM_IN_BUFFER = 8
Global Const $DM_OUT_BUFFER = 2

; ===============================================================================================================================
; #CURRENT# ======================================================================
;_WinPrint_Init
;_WinAPI_CreateDC
;_WinAPI_StartDoc
;_WinAPI_EndDoc
;_WinAPI_StartPage
;_WinAPI_EndPage
;_WinAPI_TextOut
;_WinAPI_CreatePen
;_WinAPI_MoveToEx
;_WinAPI_LineTo
;_WinAPI_SetArcDirection
;_WinAPI_ArcTo
;_WinAPI_Arc
;_WinAPI_Rectangle
;_WinAPI_RoundRect
;_WinAPI_Ellipse
;_WinAPI_DrawLine
;_WinAPI_StretchBlt
;_Printer_GetFont
;_Printer_GetFontIndirect
;_Printer_GetFontRotated
;_WinAPI_CreateBrushIndirectEx
;_WinAPI_CreateBrushIndirect
; ================================================================================
; #INTERNAL_USE_ONLY# ============================================================
;$g__Print_hFontOld
;$g__PixelsPerInchY
;$g__TwipsPerPixelY
;$g__PixelsPerInchX
;$g__TwipsPerPixelX
;$__g_winprint_init
;$g_current_HDC
;_Inch2PixelX
;_Inch2PixelY
; ================================================================================
; #FUNCTION# =====================================================================
; Name ..........: _WinPrint_Init
; Description ...: Init pixel and twip info
; Syntax ........: _WinPrint_Init()
; Parameters ....: $hDC - Handle to a device context for initializing size values with.
; Return values .: Success -  1
;                  Failure - 0 and @error set to 1
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......: If a DC handle is not passed, a handle to the default printer is used.
;                  If no default printer is installed, the function return 0 and an error.
; Related .......:
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _WinPrint_Init($hDC = -1)
	; get pixel and twips info
	Local $bLocalDC = False
	If $hDC = -1 Then
		$bLocalDC = True
		$hDC = _Printer_GetDefaultPrinter()
		$__g_winprint_init = 0 
	EndIf
	If @error Or $hDC = 0 Then Return SetError(1,0,0)
	If $__g_winprint_init = 0 Then
		$g__PixelsPerInchY = _WinAPI_GetDeviceCaps($hDC, $__WINAPICONSTANT_LOGPIXELSY) ; Get Pixels Per Inch Y
		$g__TwipsPerPixelY = 1440 / $g__PixelsPerInchY
		$g__PixelsPerInchX = _WinAPI_GetDeviceCaps($hDC, $__WINAPICONSTANT_LOGPIXELSX) ; Get Pixels Per Inch X
		$g__TwipsPerPixelX = 1440 / $g__PixelsPerInchX
		$__g_winprint_init = 1
	EndIf
	;If dc was not passed in, delete the one created.
	If $bLocalDC Then _WinAPI_DeleteDC($hDC)
	Return 1
EndFunc   ;==>_WinPrint_Init
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_CreateDC
; Description ...: Creates a device context (DC) for a device using the specified name.
; Syntax.........: _WinAPI_CreateDC($sDriver, $sDevice)
; Parameters ....: $sDriver - Specifies driver name, "winspool" for printing, "display" for screen
;                  $sDevice - Specifies device name
; Return values .: Success - handle to a DC for the specified device
; ====================================================================================================
Func _WinAPI_CreateDC($sDriver, $sDevice)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "hwnd", "CreateDC", "str", $sDriver, "str", $sDevice, "long", 0, "long", 0)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_CreateDC
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_StartDoc
; Description ...: Starts a print job
; Syntax.........: _WinAPI_StartDoc($hDC, $tDocInfo)
; Parameters ....: $hDC - Identifies the device contex
;                  $tDocInfo - $tagDOCINFO structure that specifies document info
; Return values .: Success - print job identifier for the document
; ====================================================================================================
Func _WinAPI_StartDoc($hDC, $tDocInfo)
	$g_current_HDC = $hDC
	_WinPrint_Init()
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "StartDoc", "hwnd", $hDC, "ptr", DllStructGetPtr($tDocInfo))
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_StartDoc
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_EndDoc
; Description ...: Ends a print job
; Syntax.........: _WinAPI_EndDoc($hDC)
; Parameters ....: $hDC - Identifies the device contex
; Return values .: Success - return value greater than zero
; ====================================================================================================
Func _WinAPI_EndDoc($hDC)
	Local $aResult, $err
	$aResult = DllCall("GDI32.dll", "long", "EndDoc", "hwnd", $hDC)
	$err = @error
	$g_current_HDC = 0
	$__g_winprint_init = 0
	If $err Then Return SetError($err, 0, 0)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_EndDoc
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_StartPage
; Description ...: Prepares the printer driver to accept data
; Syntax.........: _WinAPI_StartPage($hDC)
; Parameters ....: $hDC - Identifies the device contex
; Return values .: Success - return value greater than zero
; ====================================================================================================
Func _WinAPI_StartPage($hDC)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "StartPage", "hwnd", $hDC)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_StartPage
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_EndPage
; Description ...: Notifies the device that the application has finished writing to a page
; Syntax.........: _WinAPI_EndPage($hDC)
; Parameters ....: $hDC - Identifies the device contex
; Return values .: Success - return value greater than zero
; ====================================================================================================
Func _WinAPI_EndPage($hDC)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "EndPage", "hwnd", $hDC)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_EndPage
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_SetArcDirection
; Description ...: Sets the drawing direction to be used for arc and rectangle functions
; Syntax.........: _WinAPI_SetArcDirection($hDC, $Direction)
; Parameters ....: $hDC - Identifies the device contex
;                  $Direction - Specifies the new arc direction
;                  $AD_COUNTERCLOCKWISE = 1
;                  $AD_CLOCKWISE = 2
; Return values .: Success - old arc direction
; ====================================================================================================
Func _WinAPI_SetArcDirection($hDC, $Direction)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "SetArcDirection", "long", $hDC, "int", $Direction)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_SetArcDirection
; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_CreateBrushIndirectEx
; Description ...: Create a brush with the specfied params.
; Syntax ........: _WinAPI_CreateBrushIndirectEx([$hDC = 0[, $lbStyle = 0[,$lbColor = - 1[,$lbHatch = 0]]]])
; Parameters ....: $hDC  - IN/OPTIONAL -
;                  $lbStyle  - IN/OPTIONAL -
;                  $lbColor  - IN/OPTIONAL -
;                  $lbHatch  - IN/OPTIONAL -
; Return values .: Success - Handle to a brush
;                  Failure - 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......: Creates a LOGBRUSH structure with the specified parameters and calls
;                   _WinAPI_CreateBrushIndirect() for a handle to the brush.
; Related .......:
; Link ..........: @@MsdnLink@@ CreateBrushIndirect
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_CreateBrushIndirectEx($hDC = 0, $lbStyle = 0, $lbColor = -1, $lbHatch = 0)
	Local $result
	Local $tBrush = DllStructCreate("uint;uint;ptr")
	If @error Then Return SetError(1, 0, 0)
	DllStructSetData($tBrush, 1, $lbStyle)
	If $lbColor = -1 Then
		$lbColor = _WinAPI_SetTextColor($hDC, 0)
		_WinAPI_SetTextColor($hDC, $lbColor)
	EndIf
	DllStructSetData($tBrush, 2, $lbColor)
	DllStructSetData($tBrush, 3, $lbHatch)
	$result = _WinAPI_CreateBrushIndirect($tBrush)
	$tBrush = 0
	Return $result
EndFunc   ;==>_WinAPI_CreateBrushIndirectEx
; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_CreateBrushIndirect
; Description ...: Create a brush for drawing
; Syntax ........: _WinAPI_CreateBrushIndirect($pLogBrush)
; Parameters ....: $pLogBrush - Pointer the a LOGBRUSH structure.
; Return values .: Success - Handle to the brush
;                  Failure - 0 and @error set to 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ CreateBrushIndirect
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_CreateBrushIndirect($pLogBrush)
	Local $result = DllCall("gdi32.dll", "handle", "CreateBrushIndirect", "ptr", DllStructGetPtr($pLogBrush))
	If @error Then Return SetError(1, 0, 0)
	Return $result[0]
EndFunc   ;==>_WinAPI_CreateBrushIndirect
; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_PatBlt
; Description ...:
; Syntax ........: _WinAPI_PatBlt($hDestDC, $iXDest, $iYDest, $iWidth, $iHeight, $iROP)
; Parameters ....: $hDestDC - IN -
;                  $iXDest - IN -
;                  $iYDest - IN -
;                  $iWidth - IN -
;                  $iHeight - IN -
;                  $iROP - IN -
; Return values .:
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_PatBlt($hDestDC, $iXDest, $iYDest, $iWidth, $iHeight, $iROP)
	Local $aResult = DllCall("gdi32.dll", "bool", "PatBlt", "handle", $hDestDC, "int", $iXDest, "int", $iYDest, "int", $iWidth, "int", $iHeight, "dword", $iROP)
	If @error Then Return SetError(@error, @extended, False)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_PatBlt
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_TextOut
; Description ...: Writes a character string at the specified location, using the currently selected font, background color, and text color
; Syntax.........: _WinAPI_TextOut($hDC, $iXStart, $iYStart, $sString)
; Parameters ....: $hDC - Identifies the device contex
;                  $iXStart - x-coordinate of starting position
;                  $iYStart - y-coordinate of starting position
;                  $sString - character string
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_TextOut($hDC, $iXStart, $iYStart, $sString = "")
	Local $aResult = DllCall("GDI32.dll", "long", "TextOutA", "hwnd", $hDC, "long", $iXStart, "long", $iYStart, "str", $sString, "long", StringLen($sString))
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_TextOut
; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_TextOutW
; Description ...:
; Syntax ........: _WinAPI_TextOutW($hDC, $iXStart, $iYStart[, $sString = ""[, $bInches = False]])
; Parameters ....: $hDC - IN -
;                  $iXStart - IN -
;                  $iYStart - IN -
;                  $sString  - IN/OPTIONAL -
; Return values .:
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_TextOutW($hDC, $iXStart, $iYStart, $sString = "")
	Local $aResult = DllCall("GDI32.dll", "long", "TextOutW", "hwnd", $hDC, "long", $iXStart, "long", $iYStart, "Wstr", $sString, "long", StringLen($sString))
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_TextOutW
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_MoveToEx
; Description ...: Updates the current position to the specified point and optionally retrieves the previous position
; Syntax.........: _WinAPI_MoveToEx($hDC, $iX, $iY, $tPoint)
; Parameters ....: $hDC - Identifies the device contex
;                  $iX - x-coordinate of the new position, in logical units
;                  $iY - y-coordinate of the new position, in logical units
;                  $tPoint - $tagPOINT structure that receives the previous position
; Return values .: Success - non-zero value
; ====================================================================================================
Func _WinAPI_MoveToEx($hDC, $iX, $iY, ByRef $tPoint)
	Local $aResult = DllCall("GDI32.dll", "long", "MoveToEx", "long", $hDC, "int", $iX, "int", $iY, "ptr", DllStructGetPtr($tPoint))
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_MoveToEx

; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_CopyImage
; Description ...: 
; Syntax ........: _WinAPI_CopyImage($hImage, $uType, $cxDesired, $cyDesired[, $fuFlags=0])
; Parameters ....: $hImage - IN -
;                  $uType - IN -
;                  $cxDesired - IN -
;                  $cyDesired - IN -
;                  $fuFlags  - IN/OPTIONAL -
; Return values .: 
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_CopyImage($hImage, $uType, $cxDesired, $cyDesired, $fuFlags=0)
	Local $aResult = DllCall('user32.dll', 'hwnd', 'CopyImage', 'ptr', $hImage, 'uint', $uType, 'int', $cxDesired, 'int',$cyDesired, 'int', $fuFlags)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_CopyImage

; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_ArcTo
; Description ...: Draws an elliptical arc
; Syntax.........: _WinAPI_ArcTo($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iXRadial1, $iYRadial1, $iXRadial2, $iYRadial2)
; Parameters ....: $hDC - Identifies the device contex
;                  $iLeftRect - x-coord of rectangle's upper-left corner
;                  $iTopRect - y-coord of rectangle's upper-left corner
;                  $iRightRect - x-coord of rectangle's lower-right corner
;                  $iBottomRect - y-coord of rectangle's lower-right corner
;                  $iXRadial1 - x-coord of first radial ending point
;                  $iYRadial1 - y-coord of first radial ending point
;                  $iXRadial2 - x-coord of second radial ending point
;                  $iYRadial2 - y-coord of second radial ending point
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_ArcTo($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iXRadial1, $iYRadial1, $iXRadial2, $iYRadial2)
	Local $aResult = DllCall("GDI32.dll", "long", "ArcTo", "long", $hDC, "int", $iLeftRect, "int", $iTopRect, "int", $iRightRect, "int", $iBottomRect, "int", $iXRadial1, "int", $iYRadial1, "int", $iXRadial2, "int", $iYRadial2)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_ArcTo
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_Arc
; Description ...: Draws an elliptical arc
; Syntax.........: _WinAPI_Arc($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iXStartArc, $iYStartArc, $iXEndArc, $iYEndArc)
; Parameters ....: $hDC - Identifies the device contex
;                  $iLeftRect - x-coord of rectangle's upper-left corner
;                  $iTopRect - y-coord of rectangle's upper-left corner
;                  $iRightRect - x-coord of rectangle's lower-right corner
;                  $iBottomRect - y-coord of rectangle's lower-right corner
;                  $iXStartArc - x-coord of first radial ending point
;                  $iYStartArc - y-coord of first radial ending point
;                  $iXEndArc - x-coord of second radial ending point
;                  $iYEndArc - y-coord of second radial ending point
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_Arc($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iXStartArc, $iYStartArc, $iXEndArc, $iYEndArc)
	Local $aResult = DllCall("GDI32.dll", "long", "Arc", "long", $hDC, "int", $iLeftRect, "int", $iTopRect, "int", $iRightRect, "int", $iBottomRect, "int", $iXStartArc, "int", $iYStartArc, "int", $iXEndArc, "int", $iYEndArc)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_Arc
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_Rectangle
; Description ...: Draws a rectangle
; Syntax.........: _WinAPI_Rectangle($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect)
; Parameters ....: $hDC - Identifies the device contex
;                  $iLeftRect - x-coord of rectangle's upper-left corner
;                  $iTopRect - y-coord of rectangle's upper-left corner
;                  $iRightRect - x-coord of rectangle's lower-right corner
;                  $iBottomRect - y-coord of rectangle's lower-right corner
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_Rectangle($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect)
	Local $aResult = DllCall("GDI32.dll", "long", "Rectangle", "long", $hDC, "int", $iLeftRect, "int", $iTopRect, "int", $iRightRect, "int", $iBottomRect)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_Rectangle
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_RoundRect
; Description ...: Draws a rectangle
; Syntax.........: _WinAPI_RoundRect($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iWidth, $iHeight)
; Parameters ....: $hDC - Identifies the device contex
;                  $iLeftRect - x-coord of rectangle's upper-left corner
;                  $iTopRect - y-coord of rectangle's upper-left corner
;                  $iRightRect - x-coord of rectangle's lower-right corner
;                  $iBottomRect - y-coord of rectangle's lower-right corner
;                  $iWidth - width of ellipse
;                  $iHeight - height of ellipse
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_RoundRect($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect, $iWidth, $iHeight)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "RoundRect", "long", $hDC, "int", $iLeftRect, "int", $iTopRect, "int", $iRightRect, "int", $iBottomRect, "int", $iWidth, "int", $iHeight)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_RoundRect
; #FUNCTION# =========================================================================================
; Name...........: _WinAPI_Ellipse
; Description ...: Draws a ellipse
; Syntax.........: _WinAPI_Ellipse($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect)
; Parameters ....: $hDC - Identifies the device contex
;                  $iLeftRect - x-coord of rectangle's upper-left corner of rectangle
;                  $iTopRect - y-coord of rectangle's upper-left corner of rectangle
;                  $iRightRect - x-coord of rectangle's lower-right corner of rectangle
;                  $iBottomRect - y-coord of rectangle's lower-right corner of rectangle
; Return values .: Success - return value is nonzero
; ====================================================================================================
Func _WinAPI_Ellipse($hDC, $iLeftRect, $iTopRect, $iRightRect, $iBottomRect)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "long", "Ellipse", "long", $hDC, "int", $iLeftRect, "int", $iTopRect, "int", $iRightRect, "int", $iBottomRect)
	If @error Then Return SetError(@error, @extended, -1)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_Ellipse
; #FUNCTION# =====================================================================
; Name ..........: _WinAPI_StretchBlt
; Description ...:
; Syntax ........: _WinAPI_StretchBlt($hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iROP[,$bInches = False])
; Parameters ....: $hDestDC - IN -
;                  $iXDest - IN -
;                  $iYDest - IN -
;                  $iWidthDest - IN -
;                  $iHeightDest - IN -
;                  $hSrcDC - IN -
;                  $iXSrc - IN -
;                  $iYSrc - IN -
;                  $iWidthSrc - IN -
;                  $iHeightSrc - IN -
;                  $iROP - IN -
; Return values .:
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _WinAPI_StretchBlt($hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iROP)
	Local $aResult
	$aResult = DllCall("GDI32.dll", "int", "StretchBlt", "handle", $hDestDC, "int", $iXDest, "int", $iYDest, "int", $iWidthDest, "int", $iHeightDest, _
			"handle", $hSrcDC, "int", $iXSrc, "int", $iYSrc, "int", $iWidthSrc, "int", $iHeightSrc, "dword", $iROP)
	If @error Then Return SetError(@error, @extended, 0)
	Return SetError(($aResult[0] == 0), 0, $aResult[0])
EndFunc   ;==>_WinAPI_StretchBlt
; #FUNCTION# =====================================================================
; Name ..........: _Printer_GetFont
; Description ...: Create a font in the desired family and size.
; Syntax ........: _Printer_GetFont([$sFontName = 'Times New Roman'[, $DESIREDFONTSIZE = 12[, $bBold = False[, $bItalic = False[, $bUnderLine = False[, $bStrikeout = False]]]]]])
;                  $sFontName  - Font family name
;                  $DESIREDFONTSIZE  - Desired point size.
;                  $bBold  - Bold if true.
;                  $bItalic  - Italicize if true.
;                  $bUnderLine  - Underline if true.
;                  $bStrikeout  - Strikeout if true.
; Return values .: Success - Handle to the font.
;                  Failure - 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......: Wrapper around  _WinAPI_CreateFont
; Related .......:  _WinAPI_CreateFont, _Printer_GetFontIndirect, _Printer_GetFontRotated
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _Printer_GetFont($sFontName = 'Times New Roman', $DESIREDFONTSIZE = 12, $bBold = False, $bItalic = False, $bUnderLine = False, $bStrikeout = False)
	Local $iFontWeight
	If $bBold = True Then
		$iFontWeight = $FW_BOLD
	Else
		$iFontWeight = $FW_NORMAL
	EndIf
	Local $hFont = _WinAPI_CreateFont((($DESIREDFONTSIZE * - 16) / $g__TwipsPerPixelY), 0, 0, 0, $iFontWeight, $bItalic, $bUnderLine, $bStrikeout _
			, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $sFontName)
	Return $hFont
EndFunc   ;==>_Printer_GetFont
; #FUNCTION# =====================================================================
; Name ..........: _Printer_GetFontIndirect
; Description ...: Creates a font rotated by specified degrees.
; Syntax ........: _Printer_GetFontIndirect(ByRef $lf[, $sFontName = 'Times New Roman'[, $DESIREDFONTSIZE = 12[, $bBold = False[, $bItalic = False[, $bUnderLine = False[, $bStrikeout = False]]]]]])
; Parameters ....: $lf - LOGFONT structure to fill.
;                  $sFontName  - Font family name
;                  $DESIREDFONTSIZE  - Desired point size.
;                  $bBold  - Bold if true.
;                  $bItalic  - Italicize if true.
;                  $bUnderLine  - Underline if true.
;                  $bStrikeout  - Strikeout if true.
; Return values .: Success - Handle to the font.
;                  Failure - 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......: Wrapper around _Printer_GetFontRotated passing internally 0 or rotation degrees.
; Related .......:
; Link ..........: _Printer_GetFontRotated
; Example .......: [yes/no]
; ================================================================================
Func _Printer_GetFontIndirect(ByRef $lf, $sFontName = 'Times New Roman', $DESIREDFONTSIZE = 12, $bBold = False, $bItalic = False, $bUnderLine = False, $bStrikeout = False)
	Return _Printer_GetFontRotated($lf, $sFontName, $DESIREDFONTSIZE, 0, $bBold, $bItalic, $bUnderLine, $bStrikeout)
EndFunc   ;==>_Printer_GetFontIndirect
; #FUNCTION# =====================================================================
; Name ..........: _Printer_GetFontRotated
; Description ...: Creates a font rotated by specified degrees.
; Syntax ........: _Printer_GetFontRotated(ByRef $lf[, $sFontName = 'Times New Roman'[, $DESIREDFONTSIZE = 12[, $iRotation = 0[, $bBold = False[, $bItalic = False[, $bUnderLine = False[, $bStrikeout = False]]]]]]])
; Parameters ....: $lf - LOGFONT structure to fill.
;                  $sFontName  - Font family name
;                  $DESIREDFONTSIZE  - Desired point size.
;                  $iRotation  - Degrees to rotate.
;                  $bBold  - Bold if true.
;                  $bItalic  - Italicize if true.
;                  $bUnderLine  - Underline if true.
;                  $bStrikeout  - Strikeout if true.
; Return values .: Success - Handle to the font.
;                  Failure - 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _Printer_GetFontRotated(ByRef $lf, $sFontName = Default, $DESIREDFONTSIZE = 12, $iRotation = 0, $bBold = False, $bItalic = False, $bUnderLine = False, $bStrikeout = False)
	Local $iFontWeight
	If $sFontName = Default Then 
		$sFontName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes","MS Shell Dlg")
		If $sFontName = 0 Then 	$sFontName = 'Times New Roman'
	EndIf
	
	If $bBold = 1 Then
		$iFontWeight = $FW_BOLD
	Else
		$iFontWeight = $FW_NORMAL
	EndIf
	If $lf = 0 Then 
		$lf = DllStructCreate($tagLOGFONT)
		If @error Then Return SetError(1,0,0)
	EndIf
	If $iRotation < 0 Then $iRotation = 360 - $iRotation
	If $iRotation > 360 Then Return 0
	If $iRotation < -360 Then Return 0
	DllStructSetData($lf, "Escapement", $iRotation * 10)
	DllStructSetData($lf, "Height", (($DESIREDFONTSIZE * - 16) / $g__TwipsPerPixelY))
	DllStructSetData($lf, "Weight", $iFontWeight)
	DllStructSetData($lf, "Italic", $bItalic)
	DllStructSetData($lf, "Underline", $bUnderLine)
	DllStructSetData($lf, "Strikeout", $bStrikeout)
	DllStructSetData($lf, "FaceName", $sFontName)
	; set font
	Return _WinAPI_CreateFontIndirect($lf)
EndFunc   ;==>_Printer_GetFontRotated
; #FUNCTION# =====================================================================
; Name ..........: _Printer_GetDefaultPrinter
; Description ...: Get the default printers device context
; Syntax ........: _Printer_GetDefaultPrinter()
; Parameters ....: None.
; Return values .: Success - The printer device context
;                  Failure - 0 and @error set to 1
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......: Calls _WinSpool_GetDefaultPrinter to get printer name, then
;                  gets the DC by calling CreateDC api.
; Related .......: _WinSpool_GetDefaultPrinter()
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _Printer_GetDefaultPrinter()
	Local $szPrinterName = _WinSpool_GetDefaultPrinter()
	If $szPrinterName <> "" Then
		Local $hDC = _WinAPI_CreateDC("winspool", $szPrinterName)
		Return SetError(($hDC == 0), 0, $hDC)
	EndIf
	Return SetError(1, 0, 0)
EndFunc   ;==>_Printer_GetDefaultPrinter

Func _Printer_GetDocumentProperties($szPrinter = Default)
	If $szPrinter = Default Then  $szPrinter = _WinSpool_GetDefaultPrinter()
	If $szPrinter = "" Then Return SetError(1,0,0)
	Local $phPrinter, $hPrinter, $tDM = 0
	$phPrinter = DllStructCreate("ptr")
	If _WinSpool_OpenPrinter($szPrinter, $phPrinter, -1) Then
		$hPrinter = DllStructGetData($phPrinter, 1)
		Local $iNeeded = _WinSpool_DocumentProperties(0, $hPrinter, $szPrinter, $tDM, 0, 0)
		If $iNeeded > 218 Then
			$tDM = DllStructCreate($tagDEVMODE & ";byte[" & $iNeeded - 218 & "]")
		Else
			$tDM = DllStructCreate($tagDEVMODE)
		EndIf
		Local $iResult = _WinSpool_DocumentProperties(0, $hPrinter, $szPrinter, $tDM, 0, $DM_OUT_BUFFER)
		$phPrinter = 0
		_WinSpool_ClosePrinter($hPrinter)
		If $iResult = 0 Then
			$tDM = 0
			Return SetError(1,0,0)
		EndIf
		Return $tDM
	EndIf
	Return SetError(1,0,0)
EndFunc
	
; #FUNCTION# =====================================================================
; Name ..........: _WinSpool_GetDefaultPrinter
; Description ...: Get the name of the default printer
; Syntax ........: _WinSpool_GetDefaultPrinter()
; Parameters ....: None.
; Return values .: Success - The printer name
;                  Failure - Empty string
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......:
; Remarks .......:
; Related .......: _Printer_GetDefaultPrinter()
; Link ..........:
; Example .......: [yes/no]
; ================================================================================
Func _WinSpool_GetDefaultPrinter()
	Local $tcbSize = DllStructCreate("dword")
	DllCall("winspool.drv", "int", "GetDefaultPrinter", "str", '', "ptr", DllStructGetPtr($tcbSize))
	Local $tszPrinter = DllStructCreate("char[" & DllStructGetData($tcbSize, 1) & "]")
	DllCall("winspool.drv", "int", "GetDefaultPrinter", "ptr", DllStructGetPtr($tszPrinter), "ptr", DllStructGetPtr($tcbSize))
	Local $szPrinterName = DllStructGetData($tszPrinter, 1)
	$tcbSize = 0
	$tszPrinter = 0
	Return SetError(($szPrinterName == ""), 0, $szPrinterName)
EndFunc   ;==>_WinSpool_GetDefaultPrinter

; #FUNCTION# =====================================================================
; Name ..........: _WinSpool_DocumentProperties
; Description ...: 
; Syntax ........: _WinSpool_DocumentProperties($hParent,$hPrinter,$szPrinter,ByRef $pDevModeOut [,$pDevModeIn=0[,$fMode=0]])
; Parameters ....: $hParent - A handle to the parent window of the printer-configuration property sheet.
;                  $hPrinter - A handle to a printer object. Use the OpenPrinter or AddPrinter function to retrieve a printer handle.
;                  $szPrinter - A pointer to a null-terminated string that specifies the name of the device for which the printer-configuration property sheet is displayed.
;                  $pDevModeOut - A pointer to a DEVMODE structure that receives the printer configuration data specified by the user.
;                  $pDevModeIn  - A pointer to a DEVMODE structure that the operating system uses to initialize the property sheet controls.
;                  |This parameter is only used if the DM_IN_BUFFER flag is set in the fMode parameter. If DM_IN_BUFFER is not set,
;                  |the operating system uses the printer's default DEVMODE.
;                  $fMode  - IThe operations the function performs. If this parameter is zero, the DocumentProperties 
;                  |function returns the number of bytes required by the printer driver's DEVMODE data structure. 
; Return values .: 
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: [yes/no]
; ================================================================================
Func _WinSpool_DocumentProperties($hParent,$hPrinter,$szPrinter,ByRef $pDevModeOut ,$pDevModeIn=0,$fMode=0)
	Local $err,$hwnd
	;get a handle for property page parent
	If $hParent = 0 Then
		$hwnd = _WinAPI_GetDesktopWindow()
	Else
		$hwnd = $hParent
	EndIf
	Local $result = DllCall("winspool.drv","int","DocumentPropertiesW","hwnd",$hwnd,"handle",$hPrinter,"wstr",$szPrinter,"ptr",DllStructGetPtr($pDevModeOut),"ptr",$pDevModeIn,"dword",$fMode)
	$err = @error
	If $err Then Return SetError($err,0,0)
	Return $result[0]
EndFunc

; #FUNCTION# =====================================================================
; Name ..........: _WinSpool_OpenPrinter
; Description ...: Retrieves a handle to the specified printer or print server or other types of handles in the print subsystem.
; Syntax ........: _WinSpool_OpenPrinter($pPrinter,ByRef $phPrinter[,$pDefaults=-1])
; Parameters ....: $pPrinter - Printer name.
;                  $phPrinter - A pointer to a variable that receives a handle (not thread safe) to the open printer or print server object.
;                  $pDefaults  - A pointer to a PRINTER_DEFAULTS structure. This value can be NULL.
; Return values .: Success - a nonzero value.
;                  Failure - Zero.
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: [yes/no]
; ================================================================================
Func _WinSpool_OpenPrinter($pPrinter,ByRef $phPrinter,$pDefaults=-1)
	Local $err,$tPDF
	If $pDefaults = -1 Then
		$tPDF = DllStructCreate("ptr;ptr;ptr")
		DllStructSetData($tPDF,3,8)
		$pDefaults = DllStructGetPtr($tPDF)
	EndIf
	Local $t_pPrinter = DllStructCreate("ptr")
	$phPrinter = DllStructGetPtr($t_pPrinter)
	
	Local $result = DllCall("winspool.drv","int","OpenPrinterW","wstr",$pPrinter,"ptr",$phPrinter,"ptr",$pDefaults)
	$err = @error
	$tPDF = 0
	If $err Then Return SetError($err,0,0)
	Return $result[0]
EndFunc	

; #FUNCTION# =====================================================================
; Name ..........: _WinSpool_ClosePrinter
; Description ...: Closes the specified printer object.
; Syntax ........: _WinSpool_ClosePrinter($pPrinter)
; Parameters ....: $pPrinter - A handle to the printer object to be closed. This handle is returned by the OpenPrinter or AddPrinter function.
; Return values .: Success -  A non-zero value.
;                  Failure - 0
; Author ........: Stephen Podhajecki (eltorro)
; Modified.......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: [yes/no]
; ================================================================================
Func _WinSpool_ClosePrinter($pPrinter)
	Local $err
	Local $result = DllCall("winspool.drv","int","ClosePrinter","handle",$pPrinter)
	$err = @error
	If $err Then Return SetError($err,0,0)
	Return $result[0]
EndFunc	
