Icq: Text in die ChatBoxen schreiben.

    • VB.NET

      Icq: Text in die ChatBoxen schreiben.

      Hi, Ich hab gestern etwas mit der Winapi rumgespielt.
      Mit diesem Code kann man, wenn ein Chatfenster offen ist, in die Chatbox schreiben.
      Ihr Braucht eine Textbox, eine ListBox und einen Button.

      Deklarationen:
      Da man nur 15000 Zeichen nutzen kann, hab ich die Messages auf meinen Space hochgeladen:
      tsuyo.de/files/enums.txt

      Spoiler anzeigen

      VB.NET-Quellcode

      1. <DllImport("User32.dll")> _
      2. Private Shared Function EnumChildWindows _
      3. (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
      4. End Function
      5. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As IntPtr) As IntPtr
      6. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As IntPtr
      7. Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
      8. Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
      9. Dim ChildrenList As New List(Of IntPtr)
      10. Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
      11. Try
      12. EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
      13. Finally
      14. If ListHandle.IsAllocated Then ListHandle.Free()
      15. End Try
      16. Return ChildrenList.ToArray
      17. End Function
      18. Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
      19. Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
      20. If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
      21. ChildrenList.Add(Handle)
      22. Return True
      23. End Function


      Hier sind die Funktionen und Hilffunktionen die die Handles zurück geben:
      Spoiler anzeigen

      VB.NET-Quellcode

      1. Function wn(ByVal id As IntPtr) As String 'Returnt den Fenstertitel
      2. Try
      3. Dim a As Integer
      4. Dim title As String
      5. Dim Leng As Integer
      6. a = id
      7. Leng = GetWindowTextLength(a)
      8. title = Space(Leng + 1)
      9. GetWindowText(a, title, Leng + 1)
      10. Return title
      11. Catch ex As Exception
      12. Return "a"
      13. End Try
      14. End Function
      15. Public Class users
      16. Public handle As IntPtr
      17. Public title As String
      18. Public Sub New(ByVal handle_ As IntPtr, ByVal title_ As String)
      19. handle = handle_
      20. title = title_
      21. End Sub
      22. End Class
      23. Dim userlist As New List(Of users)
      24. Public Sub Handles()
      25. Dim icqhandle As IntPtr = 0
      26. Dim icqtext As String = Nothing
      27. For Each pr As Process In Process.GetProcesses
      28. If pr.ProcessName = "ICQ" Then
      29. icqhandle = pr.Handle
      30. icqtext = pr.MainWindowTitle
      31. Exit For
      32. End If
      33. Next
      34. If icqhandle = 0 Then
      35. Exit Sub
      36. End If
      37. Dim ids() As IntPtr = GetChildWindows(FindWindow(Nothing, icqtext))
      38. For i As Integer = 0 To ids.Length - 1
      39. userlist.Clear()
      40. For a As Integer = 1 To i Step 4
      41. userlist.Add(New users(ids(a), Nothing))
      42. Next
      43. Next
      44. userlist.Reverse()
      45. ListBox1.Items.Clear()
      46. For i As Integer = 0 To userlist.Count - 1
      47. ListBox1.Items.Add(userlist(i).handle)
      48. Next
      49. End Sub

      Die Sub Handles() könnt ihr dann im ButtonKlickEvent aufrufen.

      Zur Anmerkung.
      Man kann in Icq ja Tabbed Chatten.
      Pro Tab, gibt es 4 Handles. Wir benötigen immer den 2ten Handle, somit fällt 1,3 und 4 Weg.

      Man könnte nun im Textbox ChangeText event das eintragen:

      VB.NET-Quellcode

      1. If Not TextBox2.TextLength = 0 Then
      2. SendMessage(CType(ListBox1.SelectedItem, IntPtr), WindowsMessages.WM_CHAR, Asc(TextBox2.Text(TextBox2.TextLength - 1)), 0)
      3. End If


      Und schon landet der Text in der Textbox, vom ausgewählten Handle.
      Was ihr Damit macht ist natürlich eure Sache.
      Oben in den Deklarationen hab ich bewusst diese Lange liste an WindowsMessages reingesetzt, sowas kann man immer gebrauchen ;)

      Kopieren bitte nur bei Erlaubnis.