Question capturing info on current form?

mcfly

Well-known member
Joined
Jun 15, 2009
Messages
54
Programming Experience
Beginner
Hi,

Am new to vb.net but have the following code, it works sometimes but other times when a form is minimized/Restored Down it will freeze, doesn't produce an actual error in VS but just freezes and does nothing...Any ideas why?

The code captures a current form, the forms title, process id, a pixel color etc

VB.NET:
Public Class Form1

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'clear clipboard
        Windows.Forms.Clipboard.Clear()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'clear clipboard before we begin
        Windows.Forms.Clipboard.Clear()
        'turn on timer
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'timer currently at 500 milliseconds
        'capture active form

        SendKeys.Send("%" + "{PRTSC}")

        'pasting copied image to the picturebox
        PasteImage()

        If PictureBox1.Image IsNot Nothing Then

            Dim TempBitmap As Bitmap
            TempBitmap = PictureBox1.Image

            Dim MyColor As Color
            MyColor = TempBitmap.GetPixel(10, 10)

            Dim str2 As String

            str2 = ColorTranslator.ToHtml(MyColor)

            Dim str As String
            str = MyColor.ToString

            txtColour.Text = str2

        End If


    End Sub

    Public Sub PasteImage()
        'check the data format in clipboard if type bitmap then proceed 
        If Clipboard.GetDataObject.GetDataPresent(DataFormats.Bitmap) Then
            'setting clipboard data to picturebox
            Me.PictureBox1.Image = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
        End If
    End Sub



    Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
    Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
    Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer

    Private Sub timerActiveWindowCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Get the Handle to the Current Forground Window 
        Dim hWnd As IntPtr = GetForegroundWindow()

        If hWnd = IntPtr.Zero Then Exit Sub

        'Find the Length of the Window's Title
        Dim TitleLength As Integer

        TitleLength = GetWindowTextLength(hWnd)

        'Find the Window's Title
        Dim WindowTitle As String = StrDup(TitleLength + 1, "*")

        GetWindowText(hWnd, WindowTitle, TitleLength + 1)

        'Find the PID of the Application that Owns the Window 
        Dim pid As Integer = 0

        GetWindowThreadProcessId(hWnd, pid)

        If pid = 0 Then Exit Sub

        'Get the actual PROCESS from the process ID
        Dim proc As Process = Process.GetProcessById(pid)

        If proc Is Nothing Then Exit Sub

        txtProcessID.Text = pid.ToString
        txtProcessName.Text = proc.ProcessName
        txtProcessTitle.Text = proc.MainWindowTitle
        txtCurrentWindowTitle.Text = WindowTitle
        txtTitleLength.Text = TitleLength.ToString

    End Sub

End Class
 
I have never used SendKeys so I'm guessing here, it could be that when the form is minimized, that a printscreen capture cannot happen. You might want to check to see if the form is minimized before doing the SendKeys...
 
Back
Top