Announcement

Collapse
No announcement yet.

PrintDlg (handles,dc)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PrintDlg (handles,dc)

    Hello all

    I'm using PrintDlg to get a printer DC. The question is do I have to free the global memory handles(hDevMode,hDevNames) and delete the DC myself or does this some how get resolved internaly?



    ------------------
    Cheers

  • #2
    Mark --

    In MSDN samples always
    Code:
    DeleteDC pd.hDC
    If pd.hDevMode Then GlobalFree pd.hDevMode
    If pd.hDevNames Then GlobalFree pd.hDevNames
    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      Thanks Semen!

      I thought that would be the case. Funny enough I have never seen an example about it. I guess the MSDN is a bit too big!

      ------------------
      Cheers

      Comment


      • #4
        For example, http://msdn.microsoft.com/library/bo.../chpt06-10.htm

        ------------------
        E-MAIL: [email protected]

        Comment


        • #5

          The save_ variables are very handy during the app lifetime.
          At the end the memory will be freed anyway.

          Why should i release it?


          ------------------
          hellobasic

          Comment


          • #6
            Edwin --
            With the same success you can do not delete memory bitmaps and so on.
            Imagine that you execute PrintDlg many times.


            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              Well, it becomes a serious question now

              No, I can't imagne..

              My printdlg USES the old save_.. each time.

              Afaik the handle is simply reused each time, or not?
              Only on app end, "i should" destroy the handles.
              But i don't care much for those small memory blocks.

              Can you explain where i'm wrong?

              ------------------
              hellobasic

              Comment


              • #8
                Memory leaks are small - agree (on my PC under Win2000 - about 20-30K per time).
                There is no "re-using"
                Code:
                   #Compile Exe
                   #Dim All
                   #Register None
                   #Include "Win32Api.Inc"
                   #Include "Comdlg32.Inc"
                
                  Sub PrintFile (hWnd As Long, FileBuf As Asciiz)
                     Dim di As DOCINFO
                     Dim nError As Integer, pd As PRINTDLGAPI
                
                     ' // Initialize the PRINTDLG structure.
                     pd.lStructSize = SizeOf (pd)
                     pd.hwndOwner = hWnd
                     pd.hInstance = GetModuleHandle(ByVal 0&)
                     pd.Flags = %PD_RETURNDC Or %PD_USEDEVMODECOPIES Or %PD_COLLATE Or %PD_NOSELECTION Or %PD_PRINTSETUP
                
                     '// Print a test Page If successful.
                     If PrintDlg (pd) Then
                        ' // Fill out the DOCINFO structure.
                        di.cbSize = SizeOf (di)
                        Dim aa As String
                        aa = "Printing Test"
                        di.lpszDocName = StrPtr(aa)
                        StartDoc pd.hDC, di
                
                        nError = StartPage (pd.hDC)
                        If (nError <= 0) Then
                           MessageBox hWnd, "Error in StartPage.", ByVal %Null, %MB_OK
                        Else
                           ' // Print the text.
                           TextOut pd.hDC, 5, 5, FileBuf, Len (FileBuf)
                           nError = EndPage (pd.hDC)
                           If (nError <= 0) Then
                               MessageBox hWnd, "Error in EndPage.", ByVal 0&, %MB_OK
                               AbortDoc pd.hDC
                           Else
                               ' // The document has ended.
                               nError = EndDoc (pd.hDC)
                               If (nError <= 0) Then
                                  MessageBox hWnd, "Error in EndDoc.", ByVal 0&, %MB_OK
                               End If
                           End If
                        End If
                        MsgBox Hex$(pd.hDC) + "  " + Hex$(pd.hDevMode)
                        'DeleteDC pd.hDC
                        'If pd.hDevMode Then GlobalFree pd.hDevMode
                        'If pd.hDevNames Then GlobalFree pd.hDevNames
                     Else
                        MsgBox "Error"
                     End If
                  End Sub
                
                  CallBack Function PrnProc
                    PrintFile CbHndl, "Test"
                  End Function
                
                  Function PbMain
                     Dim hDlg As Long
                     Dialog New 0, "Print", , , 200, 200 To hDlg
                     Control Add Button, hDlg, 101, "Print", 10, 10, 100, 15 Call PrnProc
                     Dialog Show Modal hDlg
                  End Function
                ------------------
                E-MAIL: [email protected]

                Comment


                • #9
                  But you didn't delete the DC, maybe this is the problem.

                  I'll check out your code this day..




                  ------------------
                  hellobasic

                  Comment


                  • #10
                    Semen, i modified your example.
                    It works the same as the PB procedures in comdlg32.inc (WICH I RECOMMEND)
                    No loss in GDI or user.

                    I can actually see that it succeeds because every 2nd prindlg() the old devmode handle is reused by winapi.
                    This can only be if the handle was released somehow.
                    (I mean, the same value for the new memory is used)

                    If you see my point differently please let me know.
                    I was just finishing a print method for the compagny.
                    I also heard of using ResetDC in between pages.
                    Maybe we can gather good tips for printing.

                    1) Like sideways printing is not allways supported.
                    2) Switching orientation in between pages might not be allways succesful.
                    3) Drivers and ports like USB
                    4) Would like to know more..

                    I think printing is rather problematic.
                    The printerdrivers are not allways the best, i mostly replace them for internal drivers from windows itself.
                    We still make 16bit VB3 software, USB is somehow NOT supported, we don't know if this will be a problem for 32bit in the near future.


                    Code:
                    #Compile Exe
                     
                       #Dim All
                       #Register None
                       #include "Win32Api.Inc"
                       #include "Comdlg32.Inc"
                     
                      Sub PrintFile (hWnd As Long, FileBuf As Asciiz)
                         Dim di As DOCINFO
                         Dim nError As Integer, pd As PRINTDLGAPI
                     
                         ' // Initialize the PRINTDLG structure.
                         pd.lStructSize = SizeOf (pd)
                         pd.hwndOwner = hWnd
                         pd.hInstance = GetModuleHandle(ByVal 0&)
                         pd.Flags = %PD_RETURNDC Or %PD_USEDEVMODECOPIES Or %PD_COLLATE Or %PD_NOSELECTION Or %PD_PRINTSETUP
                     
                    '//////////////////////////////////////////////////////////////////////// 
                    '// This is how i use it
                        pd.hDevMode    = save_hDevMode
                        pd.hDevNames   = save_hDevNames
                    '//////////////////////////////////////////////////////////////////////// 
                     
                         '// Print a test Page If successful.
                         If PrintDlg (pd) Then
                     
                            ' // Fill out the DOCINFO structure.
                            di.cbSize = SizeOf (di)
                     
                            Dim aa As String
                     
                            aa = "Printing Test"
                     
                            di.lpszDocName = StrPtr(aa)
                            StartDoc pd.hDC, di
                     
                            nError = StartPage (pd.hDC)
                            If (nError <= 0) Then
                               MessageBox hWnd, "Error in StartPage.", ByVal %Null, %MB_OK
                            Else
                               ' // Print the text.
                               TextOut pd.hDC, 5, 5, FileBuf, Len (FileBuf)
                               nError = EndPage (pd.hDC)
                               If (nError <= 0) Then
                                   MessageBox hWnd, "Error in EndPage.", ByVal 0&, %MB_OK
                                   AbortDoc pd.hDC
                               Else
                                   ' // The document has ended.
                                   nError = EndDoc (pd.hDC)
                                   If (nError <= 0) Then
                                      MessageBox hWnd, "Error in EndDoc.", ByVal 0&, %MB_OK
                                   End If
                               End If
                            End If
                    
                    '//////////////////////////////////////////////////////////////////////// 
                            MsgBox Str$( pd.hDC) + "  " + Str$( pd.hDevMode)
                            DeleteDC pd.hDC
                    '//////////////////////////////////////////////////////////////////////// 
                    
                            'If pd.hDevMode Then GlobalFree pd.hDevMode
                            'If pd.hDevNames Then GlobalFree pd.hDevNames
                     
                     
                    '//////////////////////////////////////////////////////////////////////// 
                            '// Store the devmode etc..
                                save_hDevMode = pd.hDevMode
                                save_hDevNames = pd.hDevNames
                    '//////////////////////////////////////////////////////////////////////// 
                     
                         Else
                            MsgBox "Error"
                         End If
                      End Sub
                     
                      CallBack Function PrnProc
                        PrintFile CbHndl, "Test"
                      End Function
                     
                      Function PbMain
                         Dim hDlg As Long
                         Dialog New 0, "Print", , , 200, 200 To hDlg
                         Control Add Button, hDlg, 101, "Print", 10, 10, 100, 15 Call PrnProc
                         Dialog Show Modal hDlg
                      End Function
                    ------------------
                    hellobasic

                    Comment

                    Working...
                    X