Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print MDI Child Form 3

Status
Not open for further replies.

Ferlin

Programmer
Jan 18, 2001
71
US
Hello All,

I am using an api call to keybd_event(VK_SNAPSHOT, 0, 0, 0) in order to print a form. The problem I am having is that this is taken a picture of the entire screen, and not just the current active form.

Is there a way for me to capture the active mdi child form only and not the whole application screen?

Thanks in advance.

Ferlin Scarborough.
 
strongm,

Thanks for the reply. The code in that thread is close to what I have been trying.

I have a Print Form menu that the user clicks to print the currently active form. The code in my MDI Form looks like this:
Code:
Private Sub mnuPrint_Click()
  Printer.Orientation = vbPRORLandscape
  printCurrentForm Screen.ActiveForm
End Sub
I added the Screen.ActiveForm parameter so I could see what form it actually thought was active at the time.

In my module I have the following code:
Code:
'For printCurrentForm
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_MENU As Byte = &H12
Const VK_SNAPSHOT As Byte = &H2C
Const KEYEVENTF_KEYUP = &H2
Code:
Public Function printCurrentForm(pForm As Form)
  On Error GoTo ErrorHandler

  Dim lWidth As Long, lHeight As Long

  Clipboard.Clear
  Call keybd_event(VK_MENU, 0, 0, 0)
  MsgBox pForm.Name
  Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
  Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0)
  Call keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
  DoEvents

  Printer.Print
  If frmMDIMain.Width > Printer.ScaleWidth Then
    lWidth = Printer.ScaleWidth
    lHeight = (Printer.ScaleWidth / frmMDIMain.Width) * frmMDIMain.Height
  Else
    lWidth = frmMDIMain.Width
    lHeight = frmMDIMain.Height
  End If
  Printer.PaintPicture Clipboard.GetData, 0, 0, lWidth, lHeight
  Printer.EndDoc

  
  DoEvents

  On Error GoTo 0
  Exit Function

ErrorHandler:
  MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description, vbExclamation, "Error (printCurrentForm)"

End Function
When the user clicks the Print Form menu a message box appears showing the name of the MDI Child Form that they had active, but this code captures BOTH the MDI Form and the MDI Child form, and prints them both out, instead of just the MDI Child form, like they are actually wanting.

By the way, if I run the application and do an ALT-PrintScreen press then paste it into paint, I get both forms as well.

Any ideas?

Thanks again.

Ferlin Scarborough.
 
Unfortunately, Alt+PrinScreen cannot capture only the child window, but the complete MDI application window as you are experiencing.

See thread222-617312 for an alternate capture method or thread222-912111 which uses the PrintWindow API function. Both methods seem to work with MDI child form.
 
As Hypetia says, the normal PrintScreen key sequences can't cvapture MDI child windows
 
What I finally did was to add a form and set it's Backcolor property to White, and it's Autodraw property to True, and it's Visible property to False.

Then in my module I added the following code:
Code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function PrintWindow Lib "user32" (ByVal hWnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
Code:
Public Function printCurrentForm(pForm As Form)
  On Error GoTo ErrorHandler

  Dim hWnd As Long, hDC As Long, lWidth As Long, lHeight As Long
  
  hWnd = pForm.hWnd
  hDC = frmPrintWindow.hDC
  PrintWindow hWnd, hDC, 0
  DoEvents
  
  Printer.Print
  If frmMDIMain.Width > Printer.ScaleWidth Then
    lWidth = Printer.ScaleWidth
    lHeight = (Printer.ScaleWidth / frmMDIMain.Width) * frmMDIMain.Height
  Else
    lWidth = frmMDIMain.Width
    lHeight = frmMDIMain.Height
  End If
  Printer.PaintPicture frmPrintWindow.Image, 0, 0, lWidth, lHeight
  Printer.EndDoc

  DoEvents

  On Error GoTo 0
  Exit Function

ErrorHandler:
  MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description, vbExclamation, "Error (printCurrentForm)"

End Function

This seems to work fine.

Thanks again.

Ferlin Scarborough
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top