Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Print selective part of a form

Hello

I need to print selective part of a form, which means I have a complete form, but I need to print only few of its controls.
Since I know how to print a PictureBox control, I learn how to capture a screen picture and how to deliver it to a PictureBox and then send it to the printer.
My problem is that I need to capture a control by control picture and then add then one by one to "printable" PictureBox as pictures.
How can I do it? How can I add a control’s picture to existing picture into particular co-ordinates (the (relative to form co-ordinates) without erase the existing picture (the previous controls) .

Thanks
Schild
0
schild
Asked:
schild
  • 4
  • 3
1 Solution
 
watyCommented:
You could use the PrintForm, but in that case, you will print the entire form.

Otherwise, you could use the following function. This function will copy the form to the clipboard. After that, paste it in a picture and remove all what you don't need.

Call CopyEntirePictureToClipboard(Me)



Option Explicit

' General functions:
Private Type RECT
   Left     As Long
   Top      As Long
   Right    As Long
   Bottom   As Long
End Type

' GDI functions:
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source

' Creates a memory DC
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

' Creates a bitmap in memory:
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

' Places a GDI Object into DC, returning the previous one:
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

' Deletes a GDI Object:
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

' Clipboard functions:
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CountClipboardFormats Lib "user32" () As Long
Private Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long

' Memory functions:
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Public Enum EPredefinedClipboardFormatConstants
   [_First] = 1
   CF_TEXT = 1
   CF_BITMAP = 2
   CF_METAFILEPICT = 3
   CF_SYLK = 4
   CF_DIF = 5
   CF_TIFF = 6
   CF_OEMTEXT = 7
   CF_DIB = 8
   CF_PALETTE = 9
   CF_PENDATA = 10
   CF_RIFF = 11
   CF_WAVE = 12
   CF_UNICODETEXT = 13
   CF_ENHMETAFILE = 14
   CF_HDROP = 15
   CF_LOCALE = 16
   CF_MAX = 17
   [_Last] = 17
End Enum

Public Function CopyEntirePictureToClipboard(ByRef objFrom As Object) As Boolean
   ' *** Copy the entire contents of a PictureBox to the clipboard
   
   Dim lhDC       As Long
   Dim lhBmp      As Long
   Dim lhBmpOld   As Long

   ' Create a DC compatible with the object we're copying
   ' from:
   lhDC = CreateCompatibleDC(objFrom.hdc)
   If (lhDC <> 0) Then
      ' Create a bitmap compatible with the object we're
      ' copying from:
      lhBmp = CreateCompatibleBitmap(objFrom.hdc, objFrom.ScaleWidth \ Screen.TwipsPerPixelX, objFrom.ScaleHeight \ Screen.TwipsPerPixelY)
      If (lhBmp <> 0) Then
         ' Select the bitmap into the DC we have created,
         ' and store the old bitmap that was there:
         lhBmpOld = SelectObject(lhDC, lhBmp)

         ' Copy the contents of objFrom to the bitmap:
         BitBlt lhDC, 0, 0, objFrom.ScaleWidth \ Screen.TwipsPerPixelX, objFrom.ScaleHeight \ Screen.TwipsPerPixelY, objFrom.hdc, 0, 0, SRCCOPY

         ' Remove the bitmap from the DC:
         SelectObject lhDC, lhBmpOld

         ' Now set the clipboard to the bitmap:
         EmptyClipboard
         OpenClipboard 0
         SetClipboardData CF_BITMAP, lhBmp
         CloseClipboard

         ' We don't delete the Bitmap here - it is now owned
         ' by the clipboard and Windows will delete it for us
         ' when the clipboard changes or the program exits.
      End If

      ' Clear up the device context we created:
      DeleteObject lhDC
     
      CopyEntirePictureToClipboard = True
     
   Else
      CopyEntirePictureToClipboard = False
     
   End If

End Function

0
 
watyCommented:
I forgot to post as an answer
0
 
schildAuthor Commented:
Hello Waty

Thanks for fast responding.
 I know how do this, as I wrote I know how to capture a screen (or form even a single control).
My Application must to do it automatically!!!.
What I need is  to capture a single control, deliver it to the memory in the same way you just suggested for a form.
Then copy it into a PictureBox as part of its picture, not as a contained control. After that I have to take the next control, capture it again and put it in the proper co-ordinates etc.
In the end I will have a picture which contains the  picture of the needed part of the form and then I will send it to the printer.
As I wrote, I know how to capture a single control, I know how to print a PictureBox, what I don’t know is how to add a picture into another picture in a proper place(co-ordinates).


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
watyCommented:
Use the following function, I use it in my Print Preview (see my profile)

frmPreview.PictList = LoadPicture(previewLine.sText)

' *** Paint the bitmap
frmPreview.pictPreview(0).PaintPicture frmPreview.PictList,  
               previewLine.nCurrentX,  
               previewLine.nCurrentY

0
 
schildAuthor Commented:
Hello Waty

Thanks again, your control does its job. The problem is that I can’t take it as a whole piece, it is taking to much memory for my needs.
Taking your control to my project and using it just for its ability to merge two picture is like using artillery for hunting a fly :) just an Hebrew saying :)
So  if you can send me the exact code for merging two picture I will be glad to receive it. In any case thank you very much for answering me.

Schild

0
 
watyCommented:
Here is the way I use in my Print Preview OCX :

I had posted this message yesterday, but it seems it has been lost

' *** Load the bitmap ***
On Error GoTo ERROR_LOAD_BITMAP
frmPreview.PictList = LoadPicture(previewLine.sText)

' *** Paint the bitmap
frmPreview.pictPreview(0).PaintPicture frmPreview.PictList,
         previewLine.nCurrentX,  
         previewLine.nCurrentY
     
Set frmPreview.PictList = Nothing

0
 
schildAuthor Commented:
Thank you very much

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now