Results 1 to 4 of 4

Thread: [resolved] bitblt copy problem..

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19

    [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:
    1. Imports System.Drawing.Graphics
    2. Public Class frm_WC
    3.     Inherits System.Windows.Forms.Form
    4.     Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5.     Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long
    6.  
    7.     Private Declare Function GetWindowRect Lib "user32" (ByRef hwnd As Long, ByRef lpRect As S_RECT) As Long
    8.     Private Declare Function GetDesktopWindow Lib "user32" () As Long
    9.  
    10.     Private Declare Function GetActiveWindow Lib "user32" () As Long
    11.     Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
    12.     Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As S_RECT) As Long
    13.     Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    14.     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
    15.     Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
    16.     Private Const SRCCOPY = &HCC0020
    17.  
    18.     Private Structure S_RECT
    19.         Dim Left As Long
    20.         Dim Top As Long
    21.         Dim Right As Long
    22.         Dim Bottom As Long
    23.     End Structure
    24.     Private Structure S_BITMAP
    25.         Dim bmType As Long
    26.         Dim bmWidth As Long
    27.         Dim bmHeight As Long
    28.         Dim bmWidthBytes As Long
    29.         Dim bmPlanes As Integer
    30.         Dim bmBitsPixel As Integer
    31.         Dim bmBits As Long
    32.     End Structure
    33.  
    34.  
    35. #Region " Windows Form Designer generated code "
    36. '... omitted auto-generated code
    37. #End Region
    38.  
    39.     Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
    40.         Dim lpWindowHandle As Long
    41.         Dim lpWindowHandle2 As Long
    42.         Dim lpWindowHandle3 As Long
    43.         'name of window to capture
    44.         Dim lpWindowName As String = txt_Window.Text.Trim
    45.  
    46.         lpWindowHandle = FindWindow(vbNullString, lpWindowName)
    47.  
    48.         'display window handle
    49.         MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
    50.  
    51.         'bring it to the foreground
    52.         'SetForegroundWindow(lpWindowHandle)
    53.         GetShot(lpWindowHandle, PictureBox1)
    54.  
    55.     End Sub
    56.  
    57.     Private Sub GetShot(ByRef lWindowhWnd As Long, ByRef picScreen As PictureBox)
    58.         Dim nLeft As Long
    59.         Dim nTop As Long
    60.         Dim nWidth As Long
    61.         Dim nHeight As Long
    62.         Dim rRect As New S_RECT
    63.         Dim bm As New S_BITMAP
    64.         Dim lWindowhDC As Long
    65.         Dim vbSrcCopy As Long
    66.         Dim errCode As Integer
    67.  
    68.         'picScreen.Image = Nothing
    69.         GetWindowRect(lWindowhWnd, rRect)
    70.         lWindowhDC = GetWindowDC(lWindowhWnd)
    71.         '// Get coordinates
    72.         nLeft = 0
    73.         nTop = 0
    74.         nWidth = rRect.Right - rRect.Left
    75.         nHeight = rRect.Bottom - rRect.Top
    76.         '// Blt to frm.picScreen
    77.         BitBlt(picScreen.Handle, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, SRCCOPY)
    78.         '// Del DC
    79.         ReleaseDC(lWindowhWnd, lWindowhDC)
    80.         '// set picture
    81.         picScreen.Refresh()
    82.  
    83.     End Sub
    84. End Class
    Last edited by slanted; Apr 2nd, 2004 at 11:08 AM.

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