Also der Code ist jetzt nicht von mir. Hatte den auf der Festplatte. Leider stand nirgends mehr dabei von wem er ist. Aber er ist definitiv nicht von mir.
Code um Text in den Editor zu schreiben und um Text daraus zu lesen.
Public Class Form1
Private Declare Function SendMessageByString Lib "user32.dll" Alias _
"SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal uMsg As Int32, _
ByVal wParam As IntPtr, _
ByVal lParam As String) As Integer
Private Const WM_SETTEXT As Int32 = &HC
Private Const WM_GETTEXT As Int32 = &HD
Private Declare Function SendMessageByInt Lib "user32.dll" Alias _
"SendMessageA" _
(ByVal hwnd As IntPtr, _
ByVal uMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Integer
Private Const WM_GETTEXTLENGTH As Int32 = &HE
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32.dll" Alias _
"FindWindowExA" _
(ByVal hWnd1 As IntPtr, _
ByVal hWnd2 As IntPtr, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As IntPtr
<Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SendMessage( _
ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As System.Text.StringBuilder) _
As Integer
End Function
Public Function GetHandle(ByVal ClassName As String) As IntPtr
Dim hwnd As IntPtr = FindWindow(ClassName, Nothing)
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Edit", Nothing)
If (Not hwnd.Equals(IntPtr.Zero)) Then
Return hwnd
Else
Return IntPtr.Zero
End If
End Function
Public Sub SetNewText(ByVal hwnd As IntPtr, ByVal txt As String)
If (Not hwnd.Equals(IntPtr.Zero)) Then
Call SendMessageByString(hwnd, WM_SETTEXT, IntPtr.Zero, txt)
End If
End Sub
Public Function GetNewText(ByVal hwnd As IntPtr) As String
If (Not hwnd.Equals(IntPtr.Zero)) Then
Dim SB As New System.Text.StringBuilder
Dim BufferSize As Integer = 32768
SB.EnsureCapacity(BufferSize)
SendMessage(hwnd, WM_GETTEXT, BufferSize, SB)
Return SB.ToString
Else
Return ""
End If
End Function
Public Function GetTextLength(ByVal hwnd As IntPtr) As Integer
If (Not hwnd.Equals(IntPtr.Zero)) Then
Dim Length As Integer = SendMessageByInt(hwnd, WM_GETTEXTLENGTH, 0, _
0)
If Length > 0 Then
Return Length
Else
Return 0
End If
End If
End Function
Private Sub Button1Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim hwnd As IntPtr = GetHandle("Notepad")
If (Not hwnd.Equals(IntPtr.Zero)) Then
'Call SetNewText(hwnd, "http://www.visual-basic5.de")
MsgBox(GetNewText(hwnd))
Else
MessageBox.Show("Notepad not found...", "Info")
End If
End Sub
End ClassGeht bestimmt auch einfacher und eleganter, aber er funktioniert zumindest.
Christian |