Guest User

Notepad Handler

a guest
May 17th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.88 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.  
  5.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  6. Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
  7.     End Function
  8.  
  9.     <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  10.   Private Shared Function EnumChildWindows(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
  11.     End Function
  12.  
  13.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  14.  Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
  15.     End Sub
  16.  
  17.     Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
  18.         Dim ChildrenList As New List(Of IntPtr)
  19.         Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
  20.         Try
  21.             EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
  22.         Finally
  23.             If ListHandle.IsAllocated Then ListHandle.Free()
  24.         End Try
  25.  
  26.         Return ChildrenList.ToArray
  27.     End Function
  28.  
  29.     Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
  30.     Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
  31.         Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
  32.         If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
  33.         ChildrenList.Add(Handle)
  34.         Return True
  35.     End Function
  36.  
  37.     Private Const WM_CHAR = &H102
  38.  
  39.     Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  40.         If e.KeyCode = Keys.Enter Then
  41.             e.SuppressKeyPress = True
  42.             For Each P As Process In Process.GetProcesses
  43.                 If P.ProcessName = "notepad" Then
  44.                     For Each I As IntPtr In GetChildWindows(P.MainWindowHandle)
  45.                         Dim L As Integer = 256
  46.                         Dim sb As New System.Text.StringBuilder(L)
  47.                         GetClassName(I, sb, L)
  48.                         If sb.ToString = "Edit" Then
  49.                             For Each c As Char In TextBox1.Text
  50.                                 SendMessage(I, WM_CHAR, Asc(c), 0)
  51.                             Next
  52.                             SendMessage(I, WM_CHAR, Keys.Enter, 0)
  53.                             TextBox1.Clear()
  54.                             Return
  55.                         End If
  56.                     Next
  57.                 End If
  58.             Next
  59.         End If
  60.     End Sub
  61.  
  62.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  63.  
  64.     End Sub
  65. End Class
Advertisement
Add Comment
Please, Sign In to add comment