[C#] Text auslesen...

Nizomi

Mitglied
Hi,

ich versuche den Text von z.b. Notepad auszulesen(der dort geschrieben wurde)
Code:
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public class GetWindows
    {
        
        public delegate bool CallBack(int hwnd, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetClassName(int hWnd, string lpClassName, int nMaxCount);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack callback, int param);
        [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()
        {
            CallBack callBack = new CallBack(DisplayWindowInfo);
            EnumWindows(callBack, 0);
            //char = (char)7;
            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();
        }
        public static bool DisplayWindowInfo(int hWnd, int lParam)
        {
            int chars = 500;
            StringBuilder buf = new StringBuilder(chars);
           
            if (GetWindowText(hWnd, buf, chars) != 0)
            {
                string searchf = "Unbenannt - Editor".ToString();
                if (String.Equals(searchf, buf.ToString()))
                {
                    StringBuilder bufc = new StringBuilder(chars);
                    Console.WriteLine("Application: " + buf);
                    Console.WriteLine("ClassName: " + GetClassName(hWnd, bufc.ToString(), chars));
                    Console.WriteLine("-----------------------------------------");

                }
                //else
                  //  Console.WriteLine("Unequal: " + searchf + " // " + buf);
            }
            return true;
        }
    }
}
nun scheint dies eigentlich zu gehen, mein Problem liegt nun darin, wie finde ich lpszClassname raus? GetClassName gibt ja nur ne Zahl zurück....
 
Zurück