Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Explicit
' API Declarations
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 Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
' Type for window rectangle
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' Constant for BitBlt
Private Const SRCCOPY = &HCC0020
' Function to capture the area of a specific control and save it
' Note that this captures all graphics within the area outlined by the control, not just the
' control itself.
Public Sub CaptureControlArea(frm As Form, ctl As Control, ByVal FilePath As String)
Dim hDCForm As Long
Dim picBox As PictureBox
Dim frmRect As RECT
Dim ctlRect As RECT
Dim xSrcPixels As Long
Dim ySrcPixels As Long
Dim nWidthPixels As Long
Dim nHeightPixels As Long
' Get the control's rectangle in screen coordinates
GetWindowRect ctl.hwnd, ctlRect
' Get the form's rectangle in screen coordinates
GetWindowRect frm.hwnd, frmRect
' Calculate control's position relative to form's client area in pixels
xSrcPixels = ctlRect.Left - frmRect.Left
ySrcPixels = ctlRect.Top - frmRect.Top
nWidthPixels = (ctlRect.Right - ctlRect.Left)
nHeightPixels = (ctlRect.Bottom - ctlRect.Top)
' Create a temporary picture box to hold the captured image
Set picBox = frm.Controls.Add("VB.PictureBox", "TempPicBox")
With picBox
.Visible = False
.AutoRedraw = True
.Width = nWidthPixels * Screen.TwipsPerPixelX
.Height = nHeightPixels * Screen.TwipsPerPixelY
End With
' Get the form's device context
hDCForm = GetWindowDC(frm.hwnd)
' Capture the control's area to the picture box
BitBlt picBox.hDC, 0, 0, nWidthPixels, nHeightPixels, hDCForm, xSrcPixels, ySrcPixels, SRCCOPY
' Refresh the picture box to ensure the image is drawn
picBox.Refresh
' Save the captured image
SavePicture picBox.Image, FilePath
' Clean up
ReleaseDC frm.hwnd, hDCForm
frm.Controls.Remove "TempPicBox"
End Sub
Private Sub Command3_Click()
CaptureControlArea Form1, Picture1, "c:\downloads\sal21.bmp" 'your filepath goes here
End Sub