Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet a control's image
Keywordscontrol image, clipboard, PrintForm
CategoriesGraphics, Miscellany, Tips and Tricks
 
Simulate Alt-PrntScrn to copy an image of the form to the clipboard. Paste the image into a PictureBox.

Use ClientToScreen to get the offset from the form's edge to the client area. Use the control's position plus this offset to copy the control's image from the picture using PaintPicture.

 
' Copy the image of this control onto the
' destination PictureBox.
Private Sub CopyControlImage(ByVal destination As _
    PictureBox, ByVal target As Control)
    destination.Width = target.Width + destination.Width - _
        destination.ScaleWidth
    destination.Height = target.Height + destination.Height _
        - destination.ScaleHeight

    destination.PaintPicture _
        picHidden.Picture, 0, 0, _
        target.Width, target.Height, _
        target.Left + m_XOffset, target.Top + m_YOffset, _
        target.Width, target.Height
    destination.Picture = destination.Image
End Sub

' Get the offset from the corner of the border.
Private Sub GetFormOffset()
Dim pt As POINTAPI

    pt.X = 0
    pt.Y = 0
    ClientToScreen Me.hwnd, pt

    m_XOffset = pt.X - ScaleX(Me.Left, vbTwips, vbPixels)
    m_YOffset = pt.Y - ScaleY(Me.Top, vbTwips, vbPixels)
End Sub

' Copythe form's image into picHidden.
Private Function GetFormPicture()
#Const WINDOWS_VERSION = "Windows2000"

Dim alt_key As Long

    ' Capture an image of the form in the clipboard.
    ' Press Alt.
    alt_key = MapVirtualKey(VK_MENU, 0)
    keybd_event VK_MENU, alt_key, 0, 0
    DoEvents

    ' Press Print Scrn.
    #If WINDOWS_VERSION = "Windows2000" Then
        keybd_event VK_SNAPSHOT, 0, 0, 0
    #Else
        keybd_event VK_SNAPSHOT, 1, 0, 0
    #End If
    DoEvents

    ' Release Alt.
    keybd_event VK_MENU, alt_key, KEYEVENTF_KEYUP, 0
    DoEvents

    ' Make picHidden big enough.
    picHidden.BorderStyle = vbBSNone
    picHidden.Width = Me.Width
    picHidden.Height = Me.Height

    ' Paste the image into picHidden.
    picHidden.Picture = Clipboard.GetData(vbCFBitmap)
End Function
 
Note that this doesn't work for controls that are covered by other controls or other windows, that are off the screen, or are otherwise not visible.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated