Solved

Print selective part of a form

Posted on 1998-09-23
7
184 Views
Last Modified: 2010-04-30
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
Comment
Question by:schild
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1436180
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
 
LVL 14

Expert Comment

by:waty
ID: 1436181
I forgot to post as an answer
0
 
LVL 2

Author Comment

by:schild
ID: 1436182
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:waty
ID: 1436183
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
 
LVL 2

Author Comment

by:schild
ID: 1436184
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
 
LVL 14

Accepted Solution

by:
waty earned 220 total points
ID: 1436185
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
 
LVL 2

Author Comment

by:schild
ID: 1436186
Thank you very much

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now