|
-
Apr 1st, 2004, 11:24 PM
#1
Thread Starter
Junior Member
[resolved] bitblt copy problem..
I have a small form. Simply, it has a textbox, a button, and a picturebox.
On click of the button, I take the window title typed into the text box, find the handle of the window, and do a bitblt of the window's contents into the picture box.
Everything works except the last part, and I think the sample code I got (the GetShot() func) was written for legacy vb.
bitblt returns a non-zero return code, which as far as I can find is success, I get no errors, and the picturebox just doesn't contain anything.
Any help would be appreciated on figuring out where I'm goofing up...
Thanks.
(note: this is on xp pro using vs.net 2003)
VB Code:
Imports System.Drawing.Graphics
Public Class frm_WC
Inherits System.Windows.Forms.Form
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByRef hwnd As Long, ByRef lpRect As S_RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As S_RECT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As IntPtr, 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 ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Const SRCCOPY = &HCC0020
Private Structure S_RECT
Dim Left As Long
Dim Top As Long
Dim Right As Long
Dim Bottom As Long
End Structure
Private Structure S_BITMAP
Dim bmType As Long
Dim bmWidth As Long
Dim bmHeight As Long
Dim bmWidthBytes As Long
Dim bmPlanes As Integer
Dim bmBitsPixel As Integer
Dim bmBits As Long
End Structure
#Region " Windows Form Designer generated code "
'... omitted auto-generated code
#End Region
Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
Dim lpWindowHandle As Long
Dim lpWindowHandle2 As Long
Dim lpWindowHandle3 As Long
'name of window to capture
Dim lpWindowName As String = txt_Window.Text.Trim
lpWindowHandle = FindWindow(vbNullString, lpWindowName)
'display window handle
MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
'bring it to the foreground
'SetForegroundWindow(lpWindowHandle)
GetShot(lpWindowHandle, PictureBox1)
End Sub
Private Sub GetShot(ByRef lWindowhWnd As Long, ByRef picScreen As PictureBox)
Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long
Dim rRect As New S_RECT
Dim bm As New S_BITMAP
Dim lWindowhDC As Long
Dim vbSrcCopy As Long
Dim errCode As Integer
'picScreen.Image = Nothing
GetWindowRect(lWindowhWnd, rRect)
lWindowhDC = GetWindowDC(lWindowhWnd)
'// Get coordinates
nLeft = 0
nTop = 0
nWidth = rRect.Right - rRect.Left
nHeight = rRect.Bottom - rRect.Top
'// Blt to frm.picScreen
BitBlt(picScreen.Handle, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, SRCCOPY)
'// Del DC
ReleaseDC(lWindowhWnd, lWindowhDC)
'// set picture
picScreen.Refresh()
End Sub
End Class
Last edited by slanted; Apr 2nd, 2004 at 11:08 AM.
-
Apr 2nd, 2004, 06:01 AM
#2
you need to use " Integer or Int32 " inplace of your " Long's " when using Apis in .NET
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Apr 2nd, 2004, 09:11 AM
#3
Thread Starter
Junior Member
I'm not quite sure how that would cause my problem, but I changed everything to integer's, still a no go...
-
Apr 2nd, 2004, 11:07 AM
#4
Thread Starter
Junior Member
After some searching and tweaking I think I got it figured out... atleast, it works.
VB Code:
Imports System.Drawing.Graphics
Public Class frm_WC
Inherits System.Windows.Forms.Form
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Integer) As Integer
Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _
(ByVal hDestDC As IntPtr, ByVal x As Integer, _
ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, _
ByVal dwRop As Integer) As Integer
Private Declare Function GetDC Lib "user32" Alias "GetDC" _
(ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer
Private Const SRCCOPY As Integer = &HCC0020
#Region " Windows Form Designer generated code "
'removed system generated code
#End Region
Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
Dim lpWindowHandle As Integer
'name of window to capture
Dim lpWindowName As String = txtTitle.Text.Trim
lpWindowHandle = FindWindow(vbNullString, lpWindowName)
'display window handle
MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
'bring it to the foreground
SetForegroundWindow(lpWindowHandle)
Dim bmp As Bitmap = CreateScreenshot(lpWindowHandle)
PictureBox1.Image = bmp
End Sub
Function CreateScreenshot(ByVal winHandle As Integer) As Bitmap
Dim Rect As Rectangle = Screen.PrimaryScreen.Bounds
Dim gDest As Graphics
Dim hdcDest, hdcSrc As IntPtr
CreateScreenshot = New Bitmap(Rect.Right, Rect.Bottom)
gDest = gDest.FromImage(CreateScreenshot)
'hdcSrc = GetDC(IntPtr.Zero)
hdcSrc = GetDC(IntPtr.op_Explicit(winHandle))
hdcDest = gDest.GetHdc
BitBlt(hdcDest, 0, 0, _
Rect.Right, Rect.Bottom, hdcSrc, 0, 0, SRCCOPY)
gDest.ReleaseHdc(hdcDest)
ReleaseDC(IntPtr.Zero, hdcSrc)
End Function
End Class
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
|