So, mal ne dumme Frage =P
wie lese ich Text aus einem anderen Fenster(das nich zum Programm gehöhrt) aus? Oo
Hallo HolySheepy,
mit Win32: EnumWindows, GetWindowText.
herbivore
Hrm, nu hab ich das:
nur irgendwie geht das nich ^^", das EnumWindows geht aber das EnumChildWindows nich(bzw das GetWindowText in der CallBack dann)
using System;
using System.Text;
using System.Runtime.InteropServices;
public class GetWindows
{
public delegate bool CallBack(int hwnd, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack callback, int param);
[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int hWnd, CallBack callback, int param);
private static void Main()
{
CallBack callBack = new CallBack(DisplayWindowInfo);
CallBack callBackC = new CallBack(DisplayChildInfo);
EnumWindows(callBack, 0);
Console.ReadLine();
}
public static bool DisplayWindowInfo(int hWnd, int lParam)
{
int chars = 100;
CallBack callBackC = new CallBack(DisplayChildInfo);
StringBuilder buf = new StringBuilder(chars);
if (GetWindowText(hWnd, buf, chars) != 0)
{
string searchf = "Unbenannt - Editor".ToString();
if (String.Equals(buf.ToString(), searchf))
EnumChildWindows(hWnd, callBackC, 0);
}
return true;
}
public static bool DisplayChildInfo(int hWnd, int lParam)
{
int charsc = 500;
Console.WriteLine("callback");
StringBuilder bufc = new StringBuilder(charsc);
if (GetWindowText(hWnd, bufc, charsc) != 0)
Console.WriteLine(bufc);
else
Console.WriteLine("error");
return true;
}
}
Jaja, ich weiß der Code is bestimmt nicht der beste, aber bitte nicht nur meckern sondern auch Verbesserungsvorschläge machen =P
Hallo HolySheepy,
was heißt geht nicht? Kommt ein Fehler ==> LastError?
herbivore
Nein, er sagt "error"
Also schreibt er beim Ausführen:
callback
error
callback
error
also kann was mit dem GetWindowText in der DisplayChildInfo Funktion nich stimmen =/
Hallo HolySheepy,
naja, deshalb solltest du ja auch gucken, was in LastError steht.
DllImport(... SetLastError=true ...)
Sie können den Fehlercode durch Aufrufen von GetLastWin32Error abrufen.
herbivore
also habs jetzt so:
using System;
using System.Text;
using System.Runtime.InteropServices;
public class GetWindows
{
public delegate bool CallBack(int hwnd, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack callback, int param);
[DllImport("user32.dll", SetLastError=true)]
public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError=true)]
public static extern int EnumChildWindows(int hWnd, CallBack callback, int param);
private static void Main()
{
CallBack callBack = new CallBack(DisplayWindowInfo);
CallBack callBackC = new CallBack(DisplayChildInfo);
EnumWindows(callBack, 0);
Console.ReadLine();
}
public static bool DisplayWindowInfo(int hWnd, int lParam)
{
int chars = 100;
CallBack callBackC = new CallBack(DisplayChildInfo);
StringBuilder buf = new StringBuilder(chars);
if (GetWindowText(hWnd, buf, chars) != 0)
{
string searchf = "Unbenannt - Editor".ToString();
if (String.Equals(buf.ToString(), searchf))
EnumChildWindows(hWnd, callBackC, 0);
}
return true;
}
public static bool DisplayChildInfo(int hWnd, int lParam)
{
int charsc = 500;
Console.WriteLine("callback: "+Marshal.GetLastWin32Error());
StringBuilder bufc = new StringBuilder(charsc);
if (GetWindowText(hWnd, bufc, charsc) != 0)
Console.WriteLine(bufc);
else
Console.WriteLine("error "+Marshal.GetLastWin32Error()+ ";;");
return true;
}
}
Ausgabe beim Ausführen:
callback: 5
error 0;;
callback: 0
error 0;;
also scheints nicht an GetWindowText sondern an EnumChildWindows zu liegen
Code 5 war Access Denied, hab jetzt ein anderes Beispiel:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
public class GetWindows
{
[DllImport("user32.dll")]
public static extern int FindWindowEx(int hWndParent, int hWndChild, string lpsClass, string lpsTilte);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg,
IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessageBlah(IntPtr hwnd, int wMsg,
IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
public static extern int SendMessageFaul(IntPtr hwnd, int wMsg,
int wParam,
StringBuilder lParam);
private const int WM_GETTEXT = 0x000D;
private const int WM_GETTEXTLENGTH = 0x000E;
private static string GetText(IntPtr hwnd)
{
int lngLength;
StringBuilder strBuffer = new StringBuilder(); ;
int lngRet;
lngLength = SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero) + 1;
strBuffer.Capacity = lngLength;
lngRet = SendMessageFaul(hwnd, WM_GETTEXT, strBuffer.Capacity, strBuffer);
if (lngRet > 0)
return strBuffer.ToString().Substring(0, lngRet);
return
null;
}
private static void Main()
{
int hwnd = FindWindowEx(0, 0, "Notepad", null);
int hwndedit = FindWindowEx(hwnd, 0, "edit", null);
int chars = 100;
//if (GetWindowText(hwndedit, buf, chars) != 0)
string text = GetText((IntPtr)hwndedit);
if (text != null)
{
Console.WriteLine(text);
}
else
Console.WriteLine("myfunc: " + Marshal.GetLastWin32Error());
Console.ReadLine();
}
}
}
dieses geht soweit, doch nun Frage ich mich wie ich die Class Names bekomme?
Ich hab probiert GetClassName mitn DllImport reinzunehmen aber das gibt nur ne Zahl aus und keinen Namen ^^"
Entschuldigt bitte, dass ich dieses alte Thema ausgrabe, aber ich beschäftige mich gerade auch damit. Mein Code ist quasi identisch zu HolySheepys GetText. Nur kriege ich damit nur den Text von manchen Textfenstern. Ich habe mir schnell etwas gecoded, womit ich mir per Mausklick die Handles von Controls anzeigen lassen kann. Allerdings kann ich mir z.B. den Text, der im VS Codefenster steht nicht anzeigen lassen, obwohl ich das Handle habe. Ich bekomme einfach gar nichts zurück...