Solved

Print selective part of a form

Posted on 1998-09-23
7
185 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 55
String manipulation in Visual Basic 7 62
VB6 - Compare and highlight cell not the same 3 51
Excel Automation VBA 19 71
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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