Jump to content

Print without Dll file


wolf9228
 Share

Recommended Posts

MSDN Example

The following example code prints the contents of a rich edit control to the specified printer.

http://msdn.microsoft.com/en-us/library/bb787875(VS.85).aspx

PrintRTF

PrintRTF.zip

New PrintRTF

New_PrintRTF.zip

#Include <WinAPI.au3>
#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <SendMessage.au3>
#Include <RichEditConstants.au3>
#include <Memory.au3>
;Print without Dll file
Const $PD_USEDEVMODECOPIESANDCOLLATE = 0x00040000 ,$PD_RETURNDC = 0x00000100 , _
$PD_RETURNDEFAULT = 0x00000400
$hGui = GUICreate("PrintRTF Example", 320, 350, -1, -1)
$hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
$Button_1 = GUICtrlCreateButton("Print RTF", 10, 250, 100)
GUISetState()
$tagPDW = "DWORD lStructSize;HWND hwndOwner;ptr hDevMode;ptr hDevNames;ptr hDC;" & _
"DWORD Flags;WORD nFromPage;WORD nToPage;WORD nMinPage;WORD nMaxPage;WORD nCopies;" & _
"LPARAM lCustData;ptr lpfnPrintHook;ptr lpfnSetupHook;ptr lpPrintTemplateName;" & _
"ptr lpSetupTemplateName;ptr hPrintTemplate;ptr hSetupTemplate"
$PRINTDLG = DllStructCreate($tagPDW)
DllStructSetData($PRINTDLG,"hwndOwner",$hGui)
DllStructSetData($PRINTDLG,"hDevMode",0)
DllStructSetData($PRINTDLG,"hDevNames",0)
DllStructSetData($PRINTDLG,"Flags",$PD_USEDEVMODECOPIESANDCOLLATE + $PD_RETURNDC)


While True
     $iMsg = GUIGetMsg()
     Select
Case $iMsg = $GUI_EVENT_CLOSE
_GUICtrlRichEdit_Destroy($hRichEdit)
             GUIDelete()
             Exit
         Case $iMsg = $Button_1
         if (PrintDlg($PRINTDLG)) Then
         $hdc = DllStructGetData($PRINTDLG,"hDC")
         MsgBox(0,"", _
         PrintRTF($hRichEdit, $hdc))
         EndIf
     EndSelect
WEnd

;MSDN Example
;The following example code prints the contents of a rich edit control to the specified printer.
;http://msdn.microsoft.com/en-us/library/bb787875(VS.85).aspx
Func PrintRTF($hwnd,$hdc)

$tagDOCINFO = "int cbSize;ptr lpszDocName;ptr lpszOutput;ptr lpszDatatype;DWORD fwType"
$DOCINFO = DllStructCreate($tagDOCINFO)
DllStructSetData($DOCINFO,"cbSize",DllStructGetSize($DOCINFO))
$di = DllStructGetPtr($DOCINFO)
if (Not StartDoc($hdc,$di)) Then
     return FALSE
EndIf

Local $PHYSICALWIDTH = 110 , $PHYSICALHEIGHT = 111
Local $PHYSICALOFFSETX = 112 , $PHYSICALOFFSETY = 113

$cxPhysOffset = _WinAPI_GetDeviceCaps($hdc, $PHYSICALOFFSETX)
$cyPhysOffset = _WinAPI_GetDeviceCaps($hdc, $PHYSICALOFFSETY)

$cxPhys = _WinAPI_GetDeviceCaps($hdc, $PHYSICALWIDTH)
$cyPhys = _WinAPI_GetDeviceCaps($hdc, $PHYSICALHEIGHT)

; Create "print preview".
_SendMessage($hwnd,$EM_SETTARGETDEVICE,$hdc,$cxPhys/2,0,"ptr","int","int");
$tag_formatrange = "ptr hdc;ptr hdcTarget;int rcL;int rcT;int rcR;int rcB;int rcPageL;" & _
"int rcPageT;int rcPageR;int rcPageB;LONG CHARRANGE[2]"
$fr = DllStructCreate($tag_formatrange)

DllStructSetData($fr,"hdc",$hdc)
DllStructSetData($fr,"hdcTarget",$hdc)

; Set page rect to physical page size in twips.
Local $LOGPIXELSX = 88 , $LOGPIXELSY = 90

DllStructSetData($fr,"rcPageT",0)
DllStructSetData($fr,"rcPageL",0)
$rcPageR = _WinAPI_MulDiv($cxPhys, 1440, _WinAPI_GetDeviceCaps($hdc, $LOGPIXELSX));
$rcPageB = _WinAPI_MulDiv($cyPhys, 1440, _WinAPI_GetDeviceCaps($hdc, $LOGPIXELSY));
DllStructSetData($fr,"rcPageR",$rcPageR)
DllStructSetData($fr,"rcPageB",$rcPageB)

; Set the rendering rectangle to the pintable area of the page.
DllStructSetData($fr,"rcL",$cxPhysOffset)
DllStructSetData($fr,"rcR",$cxPhysOffset + $cxPhys)
DllStructSetData($fr,"rcT",$cyPhysOffset)
DllStructSetData($fr,"rcB",$cyPhysOffset + $cyPhys)

$chrg = DllStructGetPtr($fr,"CHARRANGE")
_SendMessage($hwnd, $EM_SETSEL, 0,-1,0,"int","int");         // Select the entire contents.
_SendMessage($hwnd, $EM_EXGETSEL, 0,$chrg,0,"int","ptr"); // Get the selection into a CHARRANGE.

$fSuccess = TRUE;

; Use GDI to print successive pages.
while (DllStructGetData($fr,"CHARRANGE",1) < DllStructGetData($fr,"CHARRANGE",2) And $fSuccess)

     $fSuccess = StartPage($hdc) > 0

     if (Not $fSuccess) Then ExitLoop

     $cpMin = _SendMessage($hwnd,$EM_FORMATRANGE, TRUE, DllStructGetPtr($fr),0,"BOOL","ptr")

     if ($cpMin <= DllStructGetData($fr,"CHARRANGE",1)) Then

         $fSuccess = FALSE
         ExitLoop

     EndIf

DllStructSetData($fr,"CHARRANGE",$cpMin,1)
     $fSuccess = EndPage($hdc) > 0

WEnd

_SendMessage($hwnd,$EM_FORMATRANGE, FALSE, 0,0,"BOOL")

if ($fSuccess) Then

     EndDoc($hdc)

else

     AbortDoc($hdc)

EndIf

return $fSuccess;

EndFunc

Func PrintDlg(ByRef $PRINTDLG)
DllStructSetData($PRINTDLG,"lStructSize",66)
$LPPRINTDLG_lppd = DllStructGetPtr($PRINTDLG)
$BOOL = DllCall("Comdlg32.dll","int","PrintDlgW","ptr",$LPPRINTDLG_lppd)
if @error Then Return SetError(@error,0,0)
Return $BOOL[0]
EndFunc

Func StartDoc($hdc,$lpdi)
$Return = DllCall("Gdi32.dll","int","StartDoc","ptr",$hdc,"ptr",$lpdi)
if @error Then Return SetError(@error,0,0)
Return $Return[0]
EndFunc

Func StartPage($hdc)
$Return = DllCall("Gdi32.dll","int","StartPage","HANDLE",$hdc)
if @error Then Return SetError(@error,0,0)
Return $Return[0]
EndFunc

Func EndPage($hdc)
$Return = DllCall("Gdi32.dll","int","EndPage","HANDLE",$hdc)
if @error Then Return SetError(@error,0,0)
Return $Return[0]
EndFunc

Func EndDoc($hdc)
$Return = DllCall("Gdi32.dll","int","EndDoc","HANDLE",$hdc)
if @error Then Return SetError(@error,0,0)
Return $Return[0]
EndFunc

Func AbortDoc($hdc)
$Return = DllCall("Gdi32.dll","int","AbortDoc","HANDLE",$hdc)
if @error Then Return SetError(@error,0,0)
Return $Return[0]
EndFunc

;typedef struct tagPDW {
; DWORD      lStructSize;
; HWND           hwndOwner;
; HGLOBAL        hDevMode;
; HGLOBAL        hDevNames;
; HDC            hDC;
; DWORD      Flags;
; WORD           nFromPage;
; WORD           nToPage;
; WORD           nMinPage;
; WORD           nMaxPage;
; WORD           nCopies;
; HINSTANCE  hInstance;
; LPARAM         lCustData;
; LPPRINTHOOKPROC lpfnPrintHook;
; LPSETUPHOOKPROC lpfnSetupHook;
; LPCWSTR        lpPrintTemplateName;
; LPCWSTR        lpSetupTemplateName;
; HGLOBAL        hPrintTemplate;
; HGLOBAL        hSetupTemplate;
;} PRINTDLGW, *LPPRINTDLGW;
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

wow, that looks interesting, but running this crashes real fast on my machine! I tried locating the line where it happens, it is in GetPrintDlgSizeW() on the line Local $Return = DllCallAddress("DWORD",$Address). I checked $Address, and the value looks good.

My OS is Windows 2008 R1. I tried it as x86 and x64, with the same crash result.

The (uninformative) error according to the windows app log:

Faulting application autoit3.exe, version 3.3.8.1, time stamp 0x4f25baec, faulting module unknown, version 0.0.0.0, time stamp 0x00000000, exception code 0xc0000005, fault offset 0x00d62c50, process id 0xf90, application start time 0x01cd110d1fbc3ed0.

The exe is built with no problem. But when I run it, the error in the app log is just as uninformative:

Faulting application printNoDll.exe, version 3.3.8.1, time stamp 0x4f25baec, faulting module unknown, version 0.0.0.0, time stamp 0x00000000, exception code 0xc0000005, fault offset 0x00a71128, process id 0x1360, application start time 0x01cd110ceb1cdef0.

Ideas?

Thanks!

Link to comment
Share on other sites

wow, that looks interesting, but running this crashes real fast on my machine! I tried locating the line where it happens, it is in GetPrintDlgSizeW() on the line Local $Return = DllCallAddress("DWORD",$Address). I checked $Address, and the value looks good.

My OS is Windows 2008 R1. I tried it as x86 and x64, with the same crash result.

The (uninformative) error according to the windows app log:

Faulting application autoit3.exe, version 3.3.8.1, time stamp 0x4f25baec, faulting module unknown, version 0.0.0.0, time stamp 0x00000000, exception code 0xc0000005, fault offset 0x00d62c50, process id 0xf90, application start time 0x01cd110d1fbc3ed0.

The exe is built with no problem. But when I run it, the error in the app log is just as uninformative:

Faulting application printNoDll.exe, version 3.3.8.1, time stamp 0x4f25baec, faulting module unknown, version 0.0.0.0, time stamp 0x00000000, exception code 0xc0000005, fault offset 0x00a71128, process id 0x1360, application start time 0x01cd110ceb1cdef0.

Ideas?

Thanks!

Save functions process does not run on Windows Service Thank you for this information

I will try to discover the reason why later

I make adjustments on the script you can run it now

صرح السماء كان هنا

 

Link to comment
Share on other sites

  • 6 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...