Druckfunktion

  • Abend Leute,

    ich weiß, dass das Thema öfters besprochen wurde, nur helfen diese mir nicht weiter.
    Ich habe eine Frage. Und zwar wie kann ich eine eigene Druckfunktion schreiben? Mir reicht auch diese Variante:

    [autoit]

    Func _print()
    Local $printfile = @TempDir & "\temp_printfile.rtf"

    _GuiCtrlRichEdit_StreamToFile($edit, $printfile)

    Local $objprint = ObjCreate("Shell.Application")
    $objprint.ShellExecute($printfile, 0, 0, "print", 0)

    FileDelete($printfile)
    EndFunc

    [/autoit]

    Nur möchte ich nicht, dass das Standard Programm sich mit öffnet, wie z.B. bei mir - Microsoft Office 2007.

    Hättet Ihr mir da eine lösung? Danke.

    Einmal editiert, zuletzt von MehmeX (25. Mai 2009 um 21:57)

  • Hallo Mehmex,

    probiers doch so:

    [autoit]

    Func _print()
    Local $printfile = @TempDir & "\temp_printfile.rtf"

    _GuiCtrlRichEdit_StreamToFile($edit, $printfile)

    ShellExecute($printfile, "", "", "print", @SW_HIDE )

    FileDelete($printfile)
    EndFunc

    [/autoit]


    Habe es selbst noch nicht getestet, müsste aber laut Hilfe so klappen,

    mfg (Auto)Bert

  • Spoiler anzeigen
    [autoit]


    #include <File.au3>
    MsgBox(0,"Info","Hallo schreib etwas in den Editor dann dürcke F1 und du Siehst in einer MsgBox was du geschrieben hast")
    run ("notepad.exe")
    $str=""
    $bs=StringSplit(", ,+,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,!,?,&,ä,ü,_,.",",")
    $i=1;
    Func e()
    HotKeySet(@HotKeyPressed)
    Send(@HotKeyPressed)
    $str=$str&@HotKeyPressed
    HotKeySet(@HotKeyPressed,"e")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While $i<=$bs[0]
    HotKeySet($bs[$i],"e")
    $i=$i+1;
    WEnd
    HotKeySet ("{F1}","Test")
    Func Test()
    MsgBox (0,"Outpoot",$str)
    endfunc
    while 1
    sleep (999)
    wend

    [/autoit] [autoit][/autoit] [autoit][/autoit]
  • Danke für eure Hilfe.

    autoBert: Das habe ich auch schon so versucht, aber es klapt auch nicht, es wird immer wieder von einem Standard Programm gedruckt.

    Ich hoffe es kann mir jemand helfen.

  • Hier ein beispiel für das Drucken von RTFs ;) (Mit GDI)

    Spoiler anzeigen
    [autoit]

    Opt("MustDeclareVars", 1)
    #include <GUIConstants.au3>
    #include "GuiRichEdit.au3"
    #include <SendMessage.au3>
    #include "GDI\GDI.au3"

    [/autoit] [autoit][/autoit] [autoit]

    _Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func _Main()
    Local $hgui, $msg, $hRichEdit
    $hgui = GUICreate("Rich Edit Example", 900, 750)
    $hRichEdit = _GUICtrlRichEdit_Create($hgui, "", 10, 10, 880, 680, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUISetState()
    _GUICtrlRichEdit_StreamFromFile($hRichEdit, @ScriptDir & "\Notes.rtf")
    _GuiCtrlRichEdit_GotoCharPos($hRichEdit, 0)
    Local $PrintButt = GUICtrlCreateButton("Print", 10, 710, 80, 30)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    Select
    Case $msg = $GUI_EVENT_CLOSE
    Exit
    Case $msg = $PrintButt
    _PrintRichEdit($hgui, $hRichEdit)
    EndSelect
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_Main

    [/autoit] [autoit][/autoit] [autoit]

    Func _PrintRichEdit($GUI, $h_RichEdit, $StatusInp = 0)
    ; Prog@ndy
    Local $PrinterStruct = _GDI_PrintDlg($Gui)
    If @error Then
    If _GDI_CommDlgExtendedError() = 0 Then
    GUICtrlSetData($StatusInp,"Error: User aborted Printing")
    Return SetError(1,0,0)
    EndIf
    GUICtrlSetData($StatusInp,"Error: " & _GDI_CommDlgExtendedError())
    Return SetError(Dec(Hex(_GDI_CommDlgExtendedError())),0,0)
    EndIf
    Local $pDC = DllStructGetData($PrinterStruct, "hDC")

    [/autoit] [autoit][/autoit] [autoit]

    If $pDC Then
    GUICtrlSetData($StatusInp,"Status: Printing")
    Local $TotalHeight = _GDI_GetDeviceCaps($pDC, $PHYSICALHEIGHT)
    Local $TotalWidth = _GDI_GetDeviceCaps($pDC, $PHYSICALWIDTH)
    Local $PrintOffsetX = _GDI_GetDeviceCaps($pDC, $PHYSICALOFFSETX)
    Local $PrintOffsetY = _GDI_GetDeviceCaps($pDC, $PHYSICALOFFSETY)
    Local $PrintableWidth = _GDI_GetDeviceCaps($pDC, $HORZRES)
    Local $PrintableHeight = _GDI_GetDeviceCaps($pDC, $VERTRES)
    Local $LogX = _GDI_GetDeviceCaps($pDC, $LOGPIXELSX)
    Local $LogY = _GDI_GetDeviceCaps($pDC, $LOGPIXELSY)
    If $PrintOffsetX = 0 Then
    $PrintOffsetX = 50
    $PrintableWidth -= 50
    EndIf
    If $PrintOffsetY = 0 Then
    $PrintOffsetY = 50
    $PrintableHeight -= 50
    EndIf
    $PrintOffsetY += 50
    $PrintableHeight -= 50
    Local $di = DllStructCreate($tagDOCINFO)
    DllStructSetData($di, 1, DllStructGetSize($di))
    Local $DocName = _PrintUDF_CreateTextStruct("Test Doc")
    DllStructSetData($di, "lpszDocName", DllStructGetPtr($DocName))

    [/autoit] [autoit][/autoit] [autoit]

    Local Const $tagRangeToFormat = _
    "hwnd hdc;" & _ ; // The HDC (device context) we print to
    "hwnd hdcTarget;" & _ ; // The HDC we use for measuring (may be same as hdc)
    "int rc[4];" & _ ; // Rectangle in which to print
    "int rcPage[4];" & _ ; // Physically printable page size
    "long chrg[2]" ; //CharacterRange: Range of characters to print
    Local $tRangeToFormat = DllStructCreate($tagRangeToFormat)
    DllStructSetData($tRangeToFormat, 1, $pDC)
    DllStructSetData($tRangeToFormat, 2, $pDC)
    DllStructSetData($tRangeToFormat, "rc", $PrintOffsetX*Int(1440/$LogX), 1)
    DllStructSetData($tRangeToFormat, "rc", $PrintOffsetY*Int(1440/$LogY), 2)
    DllStructSetData($tRangeToFormat, "rc", $PrintableWidth*Int(1440/$LogX), 3)
    DllStructSetData($tRangeToFormat, "rc", $PrintableHeight*Int(1440/$LogY), 4)
    DllStructSetData($tRangeToFormat, "rcPage", 0, 1)
    DllStructSetData($tRangeToFormat, "rcPage", 0, 2)
    DllStructSetData($tRangeToFormat, "rcPage", $TotalWidth*Int(1440/$LogX), 3)
    DllStructSetData($tRangeToFormat, "rcPage", $TotalHeight*Int(1440/$LogY), 4)
    DllStructSetData($tRangeToFormat, "chrg", 1, 1)
    DllStructSetData($tRangeToFormat, "chrg", 1000, 2)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Local $TextLen = _GUICtrlRichEdit_GetCtrlTextLength($h_RichEdit, True, True)-1
    Local $StartPos = 0
    _GDI_StartDoc($pDC, $di)
    Local $arPoints[3][2] = [[14,25],[100,100],[200,400]]
    Local $OldPos=0
    Do
    $OldPos = $StartPos
    DllStructSetData($tRangeToFormat, "chrg", $StartPos, 1)
    DllStructSetData($tRangeToFormat, "chrg", -1, 2)
    _GDI_StartPage($pDC)

    $StartPos = _SendMessage($h_RichEdit, $EM_FORMATRANGE, 1, DllStructGetPtr($tRangeToFormat), 0, "wparam", "ptr")
    _SendMessage($h_RichEdit, $EM_FORMATRANGE, 0,0)
    _GDI_TextOut($pDC,$PrintOffsetX,$PrintOffsetY-50,"An RTF Document")
    _GDI_TextOut($pDC,$PrintableWidth+$PrintOffsetX-400,$PrintOffsetY-50,"Text 2 for Header")

    _GDI_EndPage($pDC)

    [/autoit] [autoit][/autoit] [autoit]

    Until $StartPos >= $TextLen Or $StartPos=-1 Or $OldPos >= $StartPos

    [/autoit] [autoit][/autoit] [autoit]

    _GDI_EndDoc($pDC)
    _GDI_DeleteDC($pDC)
    ;~ DllCall($hDLL,"int","DeleteDC","hwnd",$pDC)
    _MemGlobalFree(DllStructGetData($PrinterStruct, 3))

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlSetData($StatusInp,"Status: Ready")
    Return 1
    EndIf
    Return 0
    EndFunc

    [/autoit]
  • Wow, danke dir @progandy.
    Geht es nicht kürzer? Schon heftig, nur um etwas drucken zu können. ^^

    MfG

    MehmeX

  • Das hatte ich schon mal für den SciLexer geschrieben. Da ist der Code fast genau der gleiche ;)
    Aber für das RichEdit ist er nicht ganz fehlerfrei... Bei mir hängte er im Test immer eine leere Seite an.

  • Hmm ... wie ich aber bemerkt habe, ist das Drucken unter einem normalen "Edit" einfach. Da öffnet sich kein anderes Programm oder Sonstiges, es wird einfach gedruckt.
    Habe noch eine Frage, welche Dateien von GDI sind jetzt die wichtigen .au3-Dateien? Denn da sind auch mehrere Beispiele enthalten.
    Aber nochmals danke. :)

    Edit: Ich habe es jetzt mal Probiert und es klappt, was schon mal gut aussieht. Aber einen Einwand gibt es noch und zwar druckt er nicht auf die richtigen Stellen (siehe Bild).
    Ich habe auch noch eine Fehlermeldung bekommen, die ich halt aber erst mal gelöscht habe, also die Stelle.

    Spoiler anzeigen

    C:\Program Files\AutoIt3\Include\PrintRichEdit.au3(9,34) : ERROR: _GDI_CommDlgExtendedError(): undefined function.
    If _GDI_CommDlgExtendedError()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\Mehmet\Desktop\AutoIt Projekte\EditorXXL\EditorXXL v3.5.0.0 RC 1 (RC 2).au3 - 1 error(s), 0 warning(s)

    3 Mal editiert, zuletzt von MehmeX (31. Mai 2009 um 17:18)

  • Ich hab es nochmal geändert und nur die verwendeten GDI-Includes eingefügt:

    Spoiler anzeigen
    [autoit]

    Opt("MustDeclareVars", 1)
    #include <GUIConstants.au3>
    #include "GuiRichEdit.au3"
    #include <SendMessage.au3>
    #include "GDIBase.au3"
    #include <StructureConstants.au3>
    #include <WindowsConstants.au3>
    #include "GDI\Macros.au3"
    #include "GDIConstants.au3"
    #include "GDIStructures.au3"
    #include "GDI\DeviceContexts.au3"
    #include "GDI\Printing.au3"
    #include "GDI\FontsText.au3"

    [/autoit] [autoit][/autoit] [autoit]

    _Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func _Main()
    Local $hgui, $msg, $hRichEdit
    $hgui = GUICreate("Rich Edit Example", 900, 750)
    $hRichEdit = _GUICtrlRichEdit_Create($hgui, "", 10, 10, 880, 680, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUISetState()
    _GUICtrlRichEdit_StreamFromFile($hRichEdit, @ScriptDir & "\Notes.rtf")
    _GuiCtrlRichEdit_GotoCharPos($hRichEdit, 0)
    Local $PrintButt = GUICtrlCreateButton("Print", 10, 710, 80, 30)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    Select
    Case $msg = $GUI_EVENT_CLOSE
    Exit
    Case $msg = $PrintButt
    _PrintRichEdit($hgui, $hRichEdit)
    EndSelect
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_Main

    [/autoit] [autoit][/autoit] [autoit]

    Func _PrintRichEdit($GUI, $h_RichEdit, $StatusInp = 0)
    ; Prog@ndy
    Local $PrinterStruct = _GDI_PrintDlg($Gui)
    If @error Then
    ;~ If _GDI_CommDlgExtendedError() = 0 Then
    ;~ GUICtrlSetData($StatusInp,"Error: User aborted Printing")
    ;~ Return SetError(1,0,0)
    ;~ EndIf
    ;~ GUICtrlSetData($StatusInp,"Error: " & _GDI_CommDlgExtendedError())
    ;~ Return SetError(Dec(Hex(_GDI_CommDlgExtendedError())),0,0)
    Return
    EndIf
    Local $pDC = DllStructGetData($PrinterStruct, "hDC")

    [/autoit] [autoit][/autoit] [autoit]

    If $pDC Then
    GUICtrlSetData($StatusInp,"Status: Printing")
    Local $TotalHeight = _GDI_GetDeviceCaps($pDC, $PHYSICALHEIGHT)
    Local $TotalWidth = _GDI_GetDeviceCaps($pDC, $PHYSICALWIDTH)
    Local $PrintOffsetX = _GDI_GetDeviceCaps($pDC, $PHYSICALOFFSETX)
    Local $PrintOffsetY = _GDI_GetDeviceCaps($pDC, $PHYSICALOFFSETY)
    Local $PrintableWidth = _GDI_GetDeviceCaps($pDC, $HORZRES)
    Local $PrintableHeight = _GDI_GetDeviceCaps($pDC, $VERTRES)
    Local $LogX = _GDI_GetDeviceCaps($pDC, $LOGPIXELSX)
    Local $LogY = _GDI_GetDeviceCaps($pDC, $LOGPIXELSY)
    If $PrintOffsetX = 0 Then
    $PrintOffsetX = 50
    $PrintableWidth -= 50
    EndIf
    If $PrintOffsetY = 0 Then
    $PrintOffsetY = 50
    $PrintableHeight -= 50
    EndIf
    $PrintOffsetY += 50
    $PrintableHeight -= 50
    Local $di = DllStructCreate($tagDOCINFO)
    DllStructSetData($di, 1, DllStructGetSize($di))
    Local $DocName = _PrintUDF_CreateTextStruct("Test Doc")
    DllStructSetData($di, "lpszDocName", DllStructGetPtr($DocName))

    [/autoit] [autoit][/autoit] [autoit]

    Local Const $tagRangeToFormat = _
    "hwnd hdc;" & _ ; // The HDC (device context) we print to
    "hwnd hdcTarget;" & _ ; // The HDC we use for measuring (may be same as hdc)
    "int rc[4];" & _ ; // Rectangle in which to print
    "int rcPage[4];" & _ ; // Physically printable page size
    "long chrg[2]" ; //CharacterRange: Range of characters to print
    Local $tRangeToFormat = DllStructCreate($tagRangeToFormat)
    DllStructSetData($tRangeToFormat, 1, $pDC)
    DllStructSetData($tRangeToFormat, 2, $pDC)
    DllStructSetData($tRangeToFormat, "rc", $PrintOffsetX*Int(1440/$LogX)+100, 1)
    DllStructSetData($tRangeToFormat, "rc", $PrintOffsetY*Int(1440/$LogY)+100, 2)
    DllStructSetData($tRangeToFormat, "rc", $PrintableWidth*Int(1440/$LogX)-100, 3)
    DllStructSetData($tRangeToFormat, "rc", $PrintableHeight*Int(1440/$LogY)-200, 4)
    DllStructSetData($tRangeToFormat, "rcPage", 0, 1)
    DllStructSetData($tRangeToFormat, "rcPage", 0, 2)
    DllStructSetData($tRangeToFormat, "rcPage", $TotalWidth*Int(1440/$LogX), 3)
    DllStructSetData($tRangeToFormat, "rcPage", $TotalHeight*Int(1440/$LogY), 4)
    DllStructSetData($tRangeToFormat, "chrg", 1, 1)
    DllStructSetData($tRangeToFormat, "chrg", 1000, 2)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Local $TextLen = _GUICtrlRichEdit_GetCtrlTextLengthPrint($h_RichEdit)
    Local $StartPos = 0, $savedDC
    _GDI_StartDoc($pDC, $di)
    Local $arPoints[3][2] = [[14,25],[100,100],[200,400]]
    _SendMessage($h_RichEdit, $EM_FORMATRANGE, 0,0)
    Do
    DllStructSetData($tRangeToFormat, "chrg", $StartPos, 1)
    DllStructSetData($tRangeToFormat, "chrg", $TextLen+1, 2)
    _GDI_StartPage($pDC)

    [/autoit] [autoit][/autoit] [autoit]

    $savedDC = _GDI_SaveDC($pDC)
    $StartPos = _SendMessage($h_RichEdit, $EM_FORMATRANGE, 1, DllStructGetPtr($tRangeToFormat), 0, "wparam", "ptr")
    _GDI_RestoreDC($pDC,$savedDC)

    [/autoit] [autoit][/autoit] [autoit]

    _GDI_TextOut($pDC,$PrintOffsetX,$PrintOffsetY-50,"An RTF Document")
    _GDI_TextOut($pDC,$PrintableWidth+$PrintOffsetX-400,$PrintOffsetY-50,"Text 2 for Header")

    [/autoit] [autoit][/autoit] [autoit]

    _GDI_EndPage($pDC)
    ConsoleWrite($TextLen & " - " & $StartPos & @CRLF)
    Until $StartPos >= $TextLen Or $StartPos=-1
    _SendMessage($h_RichEdit, $EM_FORMATRANGE, 0,0)

    [/autoit] [autoit][/autoit] [autoit]

    _GDI_EndDoc($pDC)
    _GDI_DeleteDC($pDC)
    ;~ DllCall($hDLL,"int","DeleteDC","hwnd",$pDC)
    _MemGlobalFree(DllStructGetData($PrinterStruct, 3))

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlSetData($StatusInp,"Status: Ready")
    Return 1
    EndIf
    Return 0
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GUICtrlRichEdit_GetCtrlTextLengthPrint($hWnd)
    ; Authors........: Prog@ndy
    ; Modified ......: Chris Haslam (c.haslam)
    Local $tGetTextLen, $iResult, $iFlags

    [/autoit] [autoit][/autoit] [autoit]

    If Not IsHWnd($hWnd) Then Return SetError(101, 0, False)

    [/autoit] [autoit][/autoit] [autoit]

    $tGetTextLen = DllStructCreate($tagGETTEXTLENGTHEX)
    DllStructSetData($tGetTextLen, 1, BitOR($GTL_PRECISE, $GTL_DEFAULT))
    DllStructSetData($tGetTextLen, 2, $CP_ACP)
    $iResult = __GCR_SendMessage($hWnd, $EM_GETTEXTLENGTHEX, DllStructGetPtr($tGetTextLen), 0)
    Return $iResult
    EndFunc ;==>_GUICtrlRichEdit_GetCtrlTextLengthPrint

    [/autoit]


    //Edit: Hier gibt es noch ein paar Anregungen, was du noch alles einbauen kannst ;) (ist zwar Delphi, aber so in etwa bekommt ma eine idee, was man in AutoIt machen muss)

    Einmal editiert, zuletzt von progandy (1. Juni 2009 um 14:13)

  • Super, einfach klasse, danke @progandy. Benutze es jetzt als UDF. :thumbup: :thumbup: :thumbup:

    Aber wie schon erwähnt, druckt er nicht richtig, also die Stelle. Ach und noch, ich habe nur #Inlcude <GDI.au3> geschrieben und es funktioniert. :)

  • Super, einfach klasse, danke @progandy. Benutze es jetzt als UDF. :thumbup: :thumbup: :thumbup:

    Aber wie schon erwähnt, druckt er nicht richtig, also die Stelle. Ach und noch, ich habe nur #Inlcude <GDI.au3> geschrieben und es funktioniert. :)


    Mit dem richtig drucken... das verstehe ich auch nich nicht so ganz ;)
    Aber es ist klar, dass es nur mit GDI.au3 auch funktioniert. Da werden alle anderen Includes eingebunden. Du wolltest aber wissen, welche Includes der GDI genau benötigt werden und da hab ich eben die verwendet ;)