четверг, 21 марта 2013 г.

Process Explorer Project




Imports System.Runtime.InteropServices
Public Class Form1
    Dim WithEvents pr As Process
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each Process In System.Diagnostics.Process.GetProcesses() ' Получаем Все процессы в LB
            ListBox1.Items.Add(Process.ProcessName)
        Next
    End Sub
    Private Sub TaskHook()
        pr = Process.GetProcessesByName(ListBox1.SelectedItem)(0) ' Отлавливаем завершение процесса
        pr.EnableRaisingEvents = True
    End Sub
    Private Sub ProcEx() Handles pr.Exited
        MsgBox("Process Closed")
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.SelectedItems.Clear()
        For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).ToString().Contains(TextBox1.Text) Then ' Исчем процесс в списке
                ListBox1.SetSelected(i, True)
            End If
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            TaskHook()
            Dim классскрина As New СкриншотОкнаПоЗаголовку
            Dim process1 As Process = Process.GetProcessesByName(ListBox1.SelectedItem)(0)
            Dim title As String = process1.MainWindowTitle()
            Label1.Text = "H_WT :" & title.ToString
            PictureBox1.Image = классскрина.скрин_отдельного_окна(title)
            'Label2.Text = process1.MainModule.FileVersionInfo.ProductName
            Label3.Text = process1.MainModule.FileVersionInfo.CompanyName
            Label4.Text = process1.MainModule.FileVersionInfo.ProductVersion
            Label5.Text = process1.MainModule.FileVersionInfo.FileDescription
        Catch ex As Exception
            Label2.Text = "none"
            Label3.Text = "none"
            Label4.Text = "none"
            Label5.Text = "none"
        End Try

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub
End Class

Public Class СкриншотОкнаПоЗаголовку
     _
    Private Shared Function FindWindow( _
      ByVal lpClassName As String, _
      ByVal lpWindowName As String) As IntPtr
    End Function
    Public Shared Function FindWindow1(ByVal windowName As String, ByVal wait As Boolean) As Integer
        Dim hWnd As Integer = FindWindow(Nothing, windowName)
        While wait AndAlso hWnd = 0
            System.Threading.Thread.Sleep(500)
            hWnd = FindWindow(Nothing, windowName)
        End While
        Return hWnd
    End Function
    Function скрин_отдельного_окна(ByVal заголовок_окна As String) As Image
        Return CaptureWindow(FindWindow1(заголовок_окна, False))
    End Function
    Public Shared Function CaptureWindow(ByVal WindowHandle As IntPtr) As Image

        Dim windowRect As New User32.RECT()
        User32.GetWindowRect(WindowHandle, windowRect)
        Dim width As Integer = windowRect.right - windowRect.left + 1
        Dim height As Integer = windowRect.bottom - windowRect.top + 1

        User32.SetWindowPos(WindowHandle, CType(User32.HWND_TOPMOST, System.IntPtr), 0, 0, 0, 0, _
         User32.SWP_NOMOVE Or User32.SWP_NOSIZE Or User32.SWP_FRAMECHANGED)

        Dim hdcSrc As IntPtr = User32.GetWindowDC(WindowHandle)
        Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)

        Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)

        Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)

        GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, _
         0, 0, GDI32.SRCCOPY)

        User32.SetWindowPos(WindowHandle, CType(User32.HWND_NOTOPMOST, System.IntPtr), 0, 0, 0, 0, _
         User32.SWP_NOMOVE Or User32.SWP_NOSIZE Or User32.SWP_FRAMECHANGED)

        GDI32.SelectObject(hdcDest, hOld)
        GDI32.DeleteDC(hdcDest)
        User32.ReleaseDC(WindowHandle, hdcSrc)
        Dim img As Image = Image.FromHbitmap(hBitmap)
        GDI32.DeleteObject(hBitmap)
        Return img
    End Function
    Private Class User32

        Public Const SWP_FRAMECHANGED As Integer = &H20
        Public Const SWP_NOMOVE As Integer = &H2
        Public Const SWP_NOSIZE As Integer = &H1
        Public Const SWP_NOZORDER As Integer = &H4
        Public Const HWND_TOPMOST As Integer = -1
        Public Const HWND_NOTOPMOST As Integer = -2

         _
        Public Structure RECT
            Public left As Integer
            Public top As Integer
            Public right As Integer
            Public bottom As Integer
        End Structure

         _
        Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
        End Function

         _
        Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As IntPtr
        End Function

         _
        Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rect As RECT) As IntPtr
        End Function

         _
        Public Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
         ByVal uFlags As UInteger) As  Boolean
        End Function
    End Class
    Private Class GDI32

        Public Const SRCCOPY As Integer = &HCC0020

         _
        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

         _
        Public Shared Function CreateCompatibleBitmap(ByVal hDC As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
        End Function

         _
        Public Shared Function CreateCompatibleDC(ByVal hDC As IntPtr) As IntPtr
        End Function

         _
        Public Shared Function DeleteDC(ByVal hDC As IntPtr) As Boolean
        End Function

         _
        Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
        End Function

         _
        Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
        End Function
    End Class
End Class

среда, 20 марта 2013 г.

Listbox Tutorial


[Save , Load , Search , Clear]


Public Class Form1

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(TextBox1.Text)
    End Sub 'Add

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim ofd As New SaveFileDialog
        With ofd
            .FileName = "*.txt"
            .Title = "Choose a dir..."
            .Filter = "TxtFile(*.txt)|*.txt"
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    Dim w As New IO.StreamWriter(.FileName)
                    For i = 0 To ListBox1.Items.Count - 1
                        w.WriteLine(ListBox1.Items.Item(i))
                    Next
                    w.Close()
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End If
        End With
    End Sub 'Save

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()
    End Sub 'Clear

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).ToString().Contains(TextBox1.Text) Then
                ListBox1.SetSelected(i, True)
            Else
                ListBox1.SelectedItems.Clear()
            End If
        Next
    End Sub 'Search
End Class