• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

Cybernavigator

Print Screen

3 mensagens neste tópico

Boas, eu tenho este código

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace ScreenShotDemo
    ''' <summary>
    ''' Provides functions to capture the entire screen, or a particular window, and save it to a file.
    ''' </summary>
    Public Class ScreenCapture
        ''' <summary>
        ''' Creates an Image object containing a screen shot of the entire desktop
        ''' </summary>
        ''' <returns></returns>
        Public Function CaptureScreen() As Image
            Return CaptureWindow(User32.GetDesktopWindow())
        End Function
        ''' <summary>
        ''' Creates an Image object containing a screen shot of a specific window
        ''' </summary>
        ''' <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        ''' <returns></returns>
        Public Function CaptureWindow(ByVal handle As IntPtr) As Image
            ' get te hDC of the target window
            Dim hdcSrc As IntPtr = User32.GetWindowDC(handle)
            ' get the size
            Dim windowRect As New User32.RECT()
            User32.GetWindowRect(handle, windowRect)
            Dim width As Integer = windowRect.right - windowRect.left
            Dim height As Integer = windowRect.bottom - windowRect.top
            ' create a device context we can copy to
            Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)
            ' create a bitmap we can copy it to,
            ' using GetDeviceCaps to get the width/height
            Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
            ' select the bitmap object
            Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)
            ' bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, _
             0, 0, GDI32.SRCCOPY)
            ' restore selection
            GDI32.SelectObject(hdcDest, hOld)
            ' clean up
            GDI32.DeleteDC(hdcDest)
            User32.ReleaseDC(handle, hdcSrc)
            ' get a .NET image object for it
            Dim img As Image = Image.FromHbitmap(hBitmap)
            ' free up the Bitmap object
            GDI32.DeleteObject(hBitmap)
            Return img
        End Function
        ''' <summary>
        ''' Captures a screen shot of a specific window, and saves it to a file
        ''' </summary>
        ''' <param name="handle"></param>
        ''' <param name="filename"></param>
        ''' <param name="format"></param>
        Public Sub CaptureWindowToFile(ByVal handle As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
            Dim img As Image = CaptureWindow(handle)
            img.Save(filename, format)
        End Sub
        ''' <summary>
        ''' Captures a screen shot of the entire desktop, and saves it to a file
        ''' </summary>
        ''' <param name="filename"></param>
        ''' <param name="format"></param>
        Public Sub CaptureScreenToFile(ByVal filename As String, ByVal format As ImageFormat)
            Dim img As Image = CaptureScreen()
            img.Save(filename, format)
        End Sub

        ''' <summary>
        ''' Helper class containing Gdi32 API functions
        ''' </summary>
        Private Class GDI32

            Public Const SRCCOPY As Integer = 13369376
            ' BitBlt dwRop parameter
            <DllImport("gdi32.dll")> _
            Public Shared Function BitBlt(ByVal hObject As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hObjectSource As IntPtr, _
             ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
            End Function
            <DllImport("gdi32.dll")> _
            Public Shared Function CreateCompatibleBitmap(ByVal hDC As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
            End Function
            <DllImport("gdi32.dll")> _
            Public Shared Function CreateCompatibleDC(ByVal hDC As IntPtr) As IntPtr
            End Function
            <DllImport("gdi32.dll")> _
            Public Shared Function DeleteDC(ByVal hDC As IntPtr) As Boolean
            End Function
            <DllImport("gdi32.dll")> _
            Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
            End Function
            <DllImport("gdi32.dll")> _
            Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
            End Function
        End Class

        ''' <summary>
        ''' Helper class containing User32 API functions
        ''' </summary>
        Private Class User32
            <StructLayout(LayoutKind.Sequential)> _
            Public Structure RECT
                Public left As Integer
                Public top As Integer
                Public right As Integer
                Public bottom As Integer
            End Structure
            <DllImport("user32.dll")> _
            Public Shared Function GetDesktopWindow() As IntPtr
            End Function
            <DllImport("user32.dll")> _
            Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
            End Function
            <DllImport("user32.dll")> _
            Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As IntPtr
            End Function
            <DllImport("user32.dll")> _
            Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rect As RECT) As IntPtr
            End Function
        End Class
    End Class
End Namespace

que se usa desta forma

        Dim sc As New ScreenShotDemo.ScreenCapture()
        ' Capturar todo o desktop
        Dim img As Image = sc.CaptureScreen()
        ' Mostrar a imagem numa picturebox com o nome imageDisplay
        Me.imageDisplay.Image = img
        ' capturar a janela do programa e gravá-la
        sc.CaptureWindowToFile(Me.Handle, "C:\temp2.gif", System.Drawing.Imaging.ImageFormat.Gif)
        ' capturar a tudo e gravar
        sc.CaptureScreenToFile("c:\temp3.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        sc.CaptureScreenToFile("c:\temp3.png", System.Drawing.Imaging.ImageFormat.Png)

Tá 5*, o problema eh que eu kero capturar outra janela que não a do programa. Outra janela qualquer.

Para capturar a janela do programa é o Me.Handle mas e se for outra?

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

eu como até sou simpatico e tal :P

toma la

 
Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
      Private Shared Function FindWindow( _
               ByVal lpClassName As String, _
               ByVal lpWindowName As String) As IntPtr
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim nWnd As IntPtr
        Dim ceroIntPtr As New IntPtr(0)
        Dim Wnd_name As String
        Dim handle = 0

        Wnd_name = "notepad"
        nWnd = FindWindow(Wnd_name, Nothing)
        'show the info
        If nWnd.Equals(ceroIntPtr) Then
            MsgBox("App Not Running")
        Else
            MsgBox("App Running")
            handle = nWnd
        End If

    End Sub
End Class

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Muito bem, é isso mesmo. Isso era o que eu precisava, bastava saber o nome da janela, não a aplicação a correr. Isto porque o nome da janela eu consigo obter.

Mas a aplicação que está a correr não.

Então deixo aqui um exemplo.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim nWnd As IntPtr
        Dim ceroIntPtr As New IntPtr(0)
        Dim Wnd_name As String
        Dim handle = 0

        Wnd_name = "Sem título - Bloco de notas"
        nWnd = FindWindow(Nothing, Wnd_name)
        'show the info
        If nWnd.Equals(ceroIntPtr) Then
            MsgBox("App Not Running")
        Else
            MsgBox("App Running")
            handle = nWnd
            MsgBox(handle)
        End If

    End Sub

Assim, só preciso do titulo da janela. Tanx a bunch.

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!


Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.


Entrar Agora