|
-
Jun 18th, 2009, 09:14 PM
#1
Thread Starter
New Member
Taking screenshot of a particular component of a frame
hi,
i am making a small application where i need to take a screenshot of a textbox. i am learning vb and i found in one of the books keybd_event to simulate a screenshot..
keybd_event 44, 1, 0, 0
Picture1.Image = Clipboard.GetData
keybd_event takes the shot of the entire frame.. (as expected)
does anyone know how to take a screenshot of a particular component of a frame?
another question -- is there any way i can edit an image that is there in the clipboard (or crop an image - for that matter) ??
thanks!
edit: this is my first post here, so please bear with me if i have not followed the ethics here! cheers!
-
Jun 18th, 2009, 09:28 PM
#2
Re: Taking screenshot of a particular component of a frame
Here's a quick example of taking a screenshot of a TextBox control. Just needs a picturebox on the form, and a command button.
You can do the same thing without using a temporary picturebox (by using a DC in memory) but I don't have the time right now.
Code:
Option Explicit
Private Declare Function GetDC Lib "user32" (ByVal hwnd 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 Sub Command1_Click()
ScreenshotControl Text1, picTemp, "C:\textbox.bmp"
End Sub
Private Sub Form_Load()
Me.ScaleMode = vbPixels
End Sub
Private Sub ScreenshotControl(ByRef TheControl As Control, _
ByRef TempPicture As PictureBox, _
ByRef SavePath As String)
Dim lonDC As Long
lonDC = GetDC(TheControl.hwnd)
With TempPicture
.Cls
.AutoRedraw = True
.ScaleMode = vbPixels
.Width = TheControl.Width
.Height = TheControl.Height
BitBlt .hDC, 0, 0, .ScaleWidth, .ScaleHeight, _
lonDC, 0, 0, vbSrcCopy
SavePicture TempPicture.Image, SavePath
End With
End Sub
-
Jun 18th, 2009, 09:31 PM
#3
Re: Taking screenshot of a particular component of a frame
Regarding the textbox. If the textbox is an actual textbox and you know the hWnd, you can get a "snapshot" but you could also get the text itself. But you will need the hWnd of that control either way.
Code:
' snapshot example
Private Declare Function PrintWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
Private Sub Command1_Click()
PrintWindow TexthWnd, Picture1.hDC, 0&
' supply the hWnd of the textbox control as TexthWnd
End Sub
To get the actual text
Code:
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Const WM_GETTEXTLENGTH As Long = &HE
Private Const WM_GETTEXT As Long = &HD
Private Sub Command1_Click()
Dim sText As String, lLen As Long
lLen = SendMessage(TexthWnd, WM_GETTEXTLENGTH, 0&, ByVal 0&)
If lLen Then
sText = String$(lLen, vbNullChar)
SendMessage TexthWnd, WM_GETTEXT, lLen + 1, ByVal sText
Picture1.Print sText
End If
' supply the hWnd of the textbox control as TexthWnd
End Sub
There are other methods of getting a "window capture", as DigiRev has displayed (beat me to the punch)
-
Jun 18th, 2009, 09:37 PM
#4
Thread Starter
New Member
Re: Taking screenshot of a particular component of a frame
that was fast! thanks! i'll try this! many many thanks!!
-
Jun 19th, 2009, 02:23 PM
#5
Re: Taking screenshot of a particular component of a frame
I never knew there was a PrintWindow API function. That way seems a lot easier.
-
Jun 19th, 2009, 02:24 PM
#6
Re: Taking screenshot of a particular component of a frame
 Originally Posted by DigiRev
I never knew there was a PrintWindow API function. That way seems a lot easier. 
PrintWindow. XP and above per MSDN link. Your way is compatible for all O/S.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|