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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Screen Shot of only the active form

Status
Not open for further replies.

Pete222

Programmer
Dec 29, 2002
85
GB
I would like to make my program to tkae a screen shot of only the form and save it to a certain destionation.

Pete.
My site: clix.to/F
 
Place a small picture box on your form and set its properties as under.[ol][li]AutoRedraw = True
[li]BorderStyle = 0 (None)
[li]Visible = False[/ol]Now place a command button (Command1) on your form and insert the following code in it.
___

Option Explicit
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
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 Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Command1_Click()
SavePicture CaptureImage(hWnd), "C:\image.bmp"
End Sub
Function CaptureImage(hWnd As Long) As Picture
Dim hDC As Long, R As RECT
GetWindowRect hWnd, R
hDC = GetWindowDC(hWnd)
Picture1.Cls
Picture1.Width = (R.Right - R.Left) * Screen.TwipsPerPixelX
Picture1.Height = (R.Bottom - R.Top) * Screen.TwipsPerPixelY
BitBlt Picture1.hDC, 0, 0, R.Right - R.Left, R.Bottom - R.Top, hDC, 0, 0, vbSrcCopy
ReleaseDC hWnd, hDC
Set CaptureImage = Picture1.Image
End Function
___

Run the program and click the button, it will save the image of the form to file C:\Image.bmp.
 
is there a way you can change that code so it dosen't include the title bar?

Pete.
My site: clix.to/F
 
Try this code. Rest of the things remain intact.
___

Option Explicit
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As Any) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
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 Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Command1_Click()
SavePicture CaptureImage(hwnd), "C:\image.bmp"
End Sub
Function CaptureImage(hwnd As Long) As Picture
Dim hDC As Long, R1 As RECT, R2 As RECT
GetWindowRect hwnd, R1
R2 = R1
hDC = GetWindowDC(hwnd)
Picture1.Cls
Picture1.Move 0, 0, ScaleWidth, ScaleHeight
ScreenToClient hwnd, R2
BitBlt Picture1.hDC, 0, 0, R1.Right - R1.Left, R1.Bottom - R1.Top, hDC, -R2.Left, -R2.Top, vbSrcCopy
ReleaseDC hwnd, hDC
Set CaptureImage = Picture1.Image
End Function
 
that works but it doesn't capture the controls.
Ive found an .ocx that works. Thanks for the help, if you do know how to make it so i can see the controls, i would be intrested in knowing. thx

Pete.
My site: clix.to/F
 
Ive got it to work...Sorry, i forgot to set the Picture Box's Auto Redraw to True!

Pete.
My site: clix.to/F
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top