Results 1 to 6 of 6

Thread: Screenshot of a form in runtime

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2003
    Location
    India
    Posts
    25

    Screenshot of a form in runtime

    How can I convert the screenshot of a form(convert into bmp) in runtime by th app itself?

  2. #2
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    hi,
    I got this from a site
    VB Code:
    1. '
    2. 'First, you create a picture On your form (Picture1). Then you Set it's visible property to false. You add the next fragment of code at the cmdPrint_click() event:
    3. '
    4. '
    5. '  Set Picture1.Picture = CaptureForm(Me)
    6. '  PrintPicture Printer, Picture2.Picture
    7. '  Printer.EndDoc
    8. '
    9. '
    10. 'You have also To create a module1 And Add there the following fragment of code:
    11.  
    12.  
    13. Option Explicit
    14.  
    15. Global Const INVERSE = 6
    16. Const SOLID = 0
    17. Const DOT = 2
    18.  
    19. Global HoldX As Single
    20. Global HoldY As Single
    21. Global StartX As Single
    22. Global StartY As Single
    23. Global SavedDrawStyle
    24. Global SavedMode
    25.  
    26.  
    27. Option Base 0
    28.  
    29. Private Type PALETTEENTRY
    30.     peRed As Byte
    31.     peGreen As Byte
    32.     peBlue As Byte
    33.     peFlags As Byte
    34. End Type
    35.  
    36. Private Type LOGPALETTE
    37.     palVersion As Integer
    38.     palNumEntries As Integer
    39.     'Enough for 256 colors
    40.     palPalEntry(255) As PALETTEENTRY
    41. End Type
    42.  
    43. Private Type GUID
    44.     Data1 As Long
    45.     Data2 As Integer
    46.     Data3 As Integer
    47.     Data4(7) As Byte
    48. End Type
    49.  
    50.  
    51. Private Const RASTERCAPS As Long = 38
    52. Private Const RC_PALETTE As Long = &H100
    53. Private Const SIZEPALETTE As Long = 104
    54.  
    55. Private Type RECT
    56.     Left As Long
    57.     Top As Long
    58.     Right As Long
    59.     Bottom As Long
    60. End Type
    61.  
    62. Private Type PicBmp
    63.     Size As Long
    64.    Type As Long
    65.     hBmp As Long
    66.     hPal As Long
    67.     Reserved As Long
    68. End Type
    69.  
    70. Private Declare Function BitBlt Lib "GDI32" ( _
    71.         ByVal hDCDest As Long, ByVal XDest As Long, _
    72.         ByVal YDest As Long, ByVal nWidth As Long, _
    73.         ByVal nHeight As Long, ByVal hDCSrc As Long, _
    74.         ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) _
    75.         As Long
    76. Private Declare Function CreateCompatibleBitmap Lib _
    77.         "GDI32" (ByVal hDC As Long, ByVal nWidth As Long, _
    78.         ByVal nHeight As Long) As Long
    79. Private Declare Function CreateCompatibleDC Lib "GDI32" ( _
    80.         ByVal hDC As Long) As Long
    81. Private Declare Function CreatePalette Lib "GDI32" ( _
    82.         lpLogPalette As LOGPALETTE) As Long
    83. Private Declare Function DeleteDC Lib "GDI32" ( _
    84.         ByVal hDC As Long) As Long
    85. Private Declare Function GetDesktopWindow Lib "USER32" () As Long
    86. Private Declare Function GetDeviceCaps Lib "GDI32" ( _
    87.         ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long
    88. Private Declare Function GetForegroundWindow Lib "USER32" () _
    89.         As Long
    90. Private Declare Function GetSystemPaletteEntries Lib _
    91.         "GDI32" (ByVal hDC As Long, ByVal wStartIndex As Long, _
    92.         ByVal wNumEntries As Long, lpPaletteEntries _
    93.         As PALETTEENTRY) As Long
    94. Private Declare Function GetWindowDC Lib "USER32" ( _
    95.         ByVal hWnd As Long) As Long
    96. Private Declare Function GetDC Lib "USER32" ( _
    97.         ByVal hWnd As Long) As Long
    98. Private Declare Function GetWindowRect Lib "USER32" ( _
    99.         ByVal hWnd As Long, lpRect As RECT) As Long
    100. Private Declare Function OleCreatePictureIndirect _
    101.         Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
    102.         ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    103. Private Declare Function RealizePalette Lib "GDI32" ( _
    104.         ByVal hDC As Long) As Long
    105. Private Declare Function ReleaseDC Lib "USER32" ( _
    106.         ByVal hWnd As Long, ByVal hDC As Long) As Long
    107. Private Declare Function SelectObject Lib "GDI32" ( _
    108.         ByVal hDC As Long, ByVal hObject As Long) As Long
    109. Private Declare Function SelectPalette Lib "GDI32" ( _
    110.         ByVal hDC As Long, ByVal hPalette As Long, _
    111.         ByVal bForceBackground As Long) As Long
    112.  
    113. Public Function CaptureForm(frmSrc As Form) As Picture
    114.     On Error GoTo ErrorRoutineErr
    115.  
    116.     'Call CaptureWindow to capture the entire form
    117.     'given it's window
    118.     'handle and then return the resulting Picture object
    119.     Set CaptureForm = CaptureWindow(frmSrc.hWnd, 0, 0, _
    120.             frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), _
    121.             frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
    122.  
    123. ErrorRoutineResume:
    124.     Exit Function
    125. ErrorRoutineErr:
    126.     MsgBox "Project1.Module1.CaptureForm" & Err & Error
    127.     Resume Next
    128. End Function
    129.  
    130. Public Function CreateBitmapPicture(ByVal hBmp As Long, _
    131.     ByVal hPal As Long) As Picture
    132.  
    133.     On Error GoTo ErrorRoutineErr
    134.  
    135.     Dim r As Long
    136.     Dim Pic As PicBmp
    137.     'IPicture requires a reference to "Standard OLE Types"
    138.     Dim IPic As IPicture
    139.     Dim IID_IDispatch As GUID
    140.  
    141.     'Fill in with IDispatch Interface ID
    142.     With IID_IDispatch
    143.         .Data1 = &H20400
    144.         .Data4(0) = &HC0
    145.         .Data4(7) = &H46
    146.     End With
    147.  
    148.     'Fill Pic with necessary parts
    149.     With Pic
    150.     'Length of structure
    151.         .Size = Len(Pic)
    152.     'Type of Picture (bitmap)
    153.         .Type = vbPicTypeBitmap
    154.     'Handle to bitmap
    155.         .hBmp = hBmp
    156.     'Handle to palette (may be null)
    157.         .hPal = hPal
    158.     End With
    159.  
    160.     'Create Picture object
    161.     r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    162.  
    163.     'Return the new Picture object
    164.     Set CreateBitmapPicture = IPic
    165.  
    166. ErrorRoutineResume:
    167.     Exit Function
    168. ErrorRoutineErr:
    169.     MsgBox "Project1.Module1.CreateBitmapPicture" & Err & Error
    170.     Resume Next
    171. End Function
    172.  
    173. Public Function CaptureWindow(ByVal hWndSrc As Long, _
    174.     ByVal LeftSrc As Long, _
    175.     ByVal TopSrc As Long, ByVal WidthSrc As Long, _
    176.     ByVal HeightSrc As Long) As Picture
    177.  
    178.     On Error GoTo ErrorRoutineErr
    179.  
    180.     Dim hDCMemory As Long
    181.     Dim hBmp As Long
    182.     Dim hBmpPrev As Long
    183.     Dim rc As Long
    184.     Dim hDCSrc As Long
    185.     Dim hPal As Long
    186.     Dim hPalPrev As Long
    187.     Dim RasterCapsScrn As Long
    188.     Dim HasPaletteScrn As Long
    189.     Dim PaletteSizeScrn As Long
    190.  
    191.     Dim LogPal As LOGPALETTE
    192.  
    193.     'get device context for the window
    194.     hDCSrc = GetWindowDC(hWndSrc)
    195.  
    196.     'Create a memory device context for the copy process
    197.     hDCMemory = CreateCompatibleDC(hDCSrc)
    198.     'Create a bitmap and place it in the memory DC
    199.     hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    200.     hBmpPrev = SelectObject(hDCMemory, hBmp)
    201.  
    202.     'get screen properties
    203.     'Raster capabilities
    204.     RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
    205.     'Palette support
    206.     HasPaletteScrn = RasterCapsScrn And RC_PALETTE
    207.     'Size of palette
    208.     PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
    209.  
    210.     'If the screen has a palette, make a copy
    211.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    212.     'Create a copy of the system palette
    213.         LogPal.palVersion = &H300
    214.         LogPal.palNumEntries = 256
    215.         rc = GetSystemPaletteEntries(hDCSrc, 0, 256, _
    216.                 LogPal.palPalEntry(0))
    217.         hPal = CreatePalette(LogPal)
    218.     'Select the new palette into the memory
    219.     'DC and realize it
    220.         hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    221.         rc = RealizePalette(hDCMemory)
    222.     End If
    223.  
    224.     'Copy the image into the memory DC
    225.     rc = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
    226.             hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    227.  
    228.     'Remove the new copy of the  on-screen image
    229.     'hBmp = SelectObject(hDCMemory, hBmpPrev)
    230.  
    231.     'If the screen has a palette get back the palette that was
    232.     'selected in previously
    233.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    234.         hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    235.     End If
    236.  
    237.     'Release the device context resources back to the system
    238.     rc = DeleteDC(hDCMemory)
    239.     rc = ReleaseDC(hWndSrc, hDCSrc)
    240.  
    241.     'Call CreateBitmapPicture to create a picture
    242.     'object from the bitmap and palette handles.
    243.     'then return the resulting picture object.
    244.     Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    245.  
    246. ErrorRoutineResume:
    247.     Exit Function
    248. ErrorRoutineErr:
    249.     MsgBox "Project1.Module1.CaptureWindow" & Err & Error
    250.     Resume Next
    251. End Function
    252.  
    253. Public Sub PrintPicture(Prn As Printer, Pic As Picture)
    254.     On Error GoTo ErrorRoutineErr
    255.  
    256.     Prn.PaintPicture Pic, 0, 0
    257.  
    258. ErrorRoutineResume:
    259.     Exit Sub
    260. ErrorRoutineErr:
    261.     MsgBox "Project1.Module1.PrintPicture" & Err & Error
    262.     Resume Next
    263. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2003
    Location
    India
    Posts
    25
    hello,

    actually i wanna to convert the form into bmp or jpg format showing all of its controls (no caotion for the form..)

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    The attached project should help...
    Attached Files Attached Files


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2003
    Location
    India
    Posts
    25
    Hi,

    Thanx.........

    all i have to do was to change the code in cmdprint to

    SavePicture( Picture1.Picture, "c:\pic.bmp")

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    You're welcome


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width