Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- Public Class Form1
- <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
- Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
- End Function
- <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
- Private Shared Function EnumChildWindows(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
- End Function
- <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
- Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
- End Sub
- Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
- Dim ChildrenList As New List(Of IntPtr)
- Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
- Try
- EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
- Finally
- If ListHandle.IsAllocated Then ListHandle.Free()
- End Try
- Return ChildrenList.ToArray
- End Function
- Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
- Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
- Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
- If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
- ChildrenList.Add(Handle)
- Return True
- End Function
- Private Const WM_CHAR = &H102
- Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
- If e.KeyCode = Keys.Enter Then
- e.SuppressKeyPress = True
- For Each P As Process In Process.GetProcesses
- If P.ProcessName = "notepad" Then
- For Each I As IntPtr In GetChildWindows(P.MainWindowHandle)
- Dim L As Integer = 256
- Dim sb As New System.Text.StringBuilder(L)
- GetClassName(I, sb, L)
- If sb.ToString = "Edit" Then
- For Each c As Char In TextBox1.Text
- SendMessage(I, WM_CHAR, Asc(c), 0)
- Next
- SendMessage(I, WM_CHAR, Keys.Enter, 0)
- TextBox1.Clear()
- Return
- End If
- Next
- End If
- Next
- End If
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment