0

I am trying to read textbox value from another form application, so far everything has been successful, I did tests on the an notepad app, problems started when I want to get a value from the textbox which is part of the mdi client. My code completely does not see the text field that is part of another form in the mdi client.

My code:

        {
            public IntPtr hWnd;
            public string Text;
        }

        const int WM_GETTEXT = 0x0D;
        const int WM_GETTEXTLENGTH = 0x0E;

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<WinText> windows = new List<WinText>();

            //find the "first" window
            IntPtr hWnd = FindWindow("notepad", null);


            while (hWnd != IntPtr.Zero)
            {

                //find the control window that has the text
                IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "RichEditD2DPT", null);
                //initialize the buffer.  using a StringBuilder here
                System.Text.StringBuilder sb = new System.Text.StringBuilder(255);  // or length from call with GETTEXTLENGTH

                //get the text from the child control
                int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);

                windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() });
                label2.Text = "" + windows.Count();

                //find the next window
                hWnd = FindWindowEx(IntPtr.Zero, hWnd, "notepad", null);
            }

            //do something clever
            windows.OrderBy(x => x.Text).ToList().ForEach(y => label1.Text = " {0} = {1}\n" + y.hWnd + "\n" + y.Text);
        }

Jakobson
  • 101
  • 1
  • 2
  • 10
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 19 '22 at 13:31
  • What's this “another form application”? Is it something you wrote? Implement IPC so you don't have to go through this rigmarole. – Dour High Arch Aug 20 '22 at 17:18

0 Answers0