我想获得WebBrowser1(控件)的场景设备, 结果出错, 各位帮忙看一下:
代码如下:    Dim hdcWeb As Long
    hdcWeb = GetDC(WebBrowser1.hwnd)报错原因: "Method 'HWND' of object 'IWebBrowser2' failed", 哪位高手知道该怎么改, 难道WebBrowser控件不能用GetDC吗?

解决方案 »

  1.   

    你先浏览一个空网页:WebBrowser1.navigate("about:blank")
    While Webbrwoser1.busy
        DoEvents
    Wend然后再:
    hdcWeb = GetDC(WebBrowser1.hwnd)
      

  2.   

    to 冰儿马甲:
       hi,不知道为什么, 你的代码我没有调试成功,还是会出现相同的错误, 可能是我不太熟悉GetDC用法的缘故; 我从网上找到了一个方法可以解决这个问题,不过抓图时会出现滚动条, 你能帮我看看吗?(如果方便的话,我可以把代码mail过去)代码如下:Option ExplicitPrivate 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 Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As LongPrivate Const GW_HWNDNEXT = 2
    Private Const GW_CHILD = 5
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long'******************************************************
    ' Find the child window with the indicated class name.
    Public Function FindHwnd(ByVal container_hwnd As Long, ByVal target_class As String) As Long
    Dim child_hwnd As Long
    Dim class_name As String * 256    child_hwnd = GetWindow(container_hwnd, GW_CHILD)
        Do
            ' See if this is the target class.
            GetClassName child_hwnd, class_name, 256
            If Left$(class_name, Len(target_class)) = target_class Then
                ' This is it.
                FindHwnd = child_hwnd
                Exit Do
            End If        ' Get the next child.
            child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
        Loop While child_hwnd <> 0
    End Function
    '*****************************************************Private Sub Form_Load()
        WebBrowser1.Navigate "http://www.vb-helper.com"
    End Sub
    Private Sub Form_Resize()
        WebBrowser1.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    Private Sub mnuFileCaptureImage_Click()
    Dim web_hwnd As Long
    Dim web_dc As Long    ' Prepare the display form.
        frmPicture.Picture1.Move 0, 0, ScaleWidth, ScaleHeight
        frmPicture.Width = frmPicture.Picture1.Width + frmPicture.Width - frmPicture.ScaleWidth
        frmPicture.Height = frmPicture.Picture1.Height + frmPicture.Height - frmPicture.ScaleHeight    frmPicture.Picture1.AutoRedraw = True
        frmPicture.Picture1.ScaleMode = vbPixels    ' Find the WebBrowser's hWnd.
        web_hwnd = FindHwnd(Me.hwnd, "Shell Embedding")    ' Get the corresponding DC.
        web_dc = GetDC(web_hwnd)    ' Copy the image from the DC to Picture1.
        BitBlt frmPicture.Picture1.hDC, _
            0, 0, _
            frmPicture.Picture1.ScaleWidth, _
            frmPicture.Picture1.ScaleHeight, _
            web_dc, _
            0, 0, SRCCOPY    ' Display the results.
        frmPicture.Show
    End Sub
      

  3.   

    to MmMVP马甲:   能告诉我要用到哪个接口吗, 谢谢?
      

  4.   

    VB声明 
    Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
     
    说明 
    获取指定窗口的设备场景
     
    返回值 
    Long,指定窗口的设备场景句柄,出错则为0 参数表 参数 类型及说明 
    hwnd Long,将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC 注解 
    若窗口所属类具有CS_OWNDC, CS_CLASSDC 或 CS_PARENTDC样式,则获取的设备场景属窗口或类专有。vb的窗体和图片框控件也是这种情况,它用该函数取得的结果和控件的hdc属性相同(在autoredraw为FALSE时)。您无须考虑取回的窗体或图片框控件设备场景的默认状态,特别是绘图对象。另外,默认状态随着窗体和控件autoredraw属性的设置而不同。在设备场景释放前您必须回复其状态为初始值。对于没有CS_OWNDC, CS_CLASSDC 或 CS_PARENTDC样式的窗口的设备场景,可从通用windows缓存中获取,其状态为默认值。缓存中可用设备场景数量是有限的,因此只要可能就释放设备场景
    用本函数获取的设备场景一定要用ReleaseDC函数释放,不能用DeleteDC
     
      

  5.   

    GetDC VB声明 
    Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long 
    说明 
    获取指定窗口的设备场景 
    返回值 
    Long,指定窗口的设备场景句柄,出错则为0 
    参数表 
    参数 类型及说明 
    hwnd Long,将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC 
    注解 
    若窗口所属类具有CS_OWNDC, CS_CLASSDC 或 CS_PARENTDC样式,则获取的设备场景属窗口或类专有。vb的窗体和图片框控件也是这种情况,它用该函数取得的结果和控件的hdc属性相同(在autoredraw为FALSE时)。您无须考虑取回的窗体或图片框控件设备场景的默认状态,特别是绘图对象。另外,默认状态随着窗体和控件autoredraw属性的设置而不同。在设备场景释放前您必须回复其状态为初始值。对于没有CS_OWNDC, CS_CLASSDC 或 CS_PARENTDC样式的窗口的设备场景,可从通用windows缓存中获取,其状态为默认值。缓存中可用设备场景数量是有限的,因此只要可能就释放设备场景
    用本函数获取的设备场景一定要用ReleaseDC函数释放,不能用DeleteDC