0

Как по дескриптору(т.е. номеру) окна вывести его заголовок,у меня не получается может кто-нибудь подскажет?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace parol_udal { class Program { [DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)] public static extern IntPtr FindWindow(string sClassName,string sWindowName);

    [DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)]
     public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

    [DllImport("user32.dll",CharSet=CharSet.Auto ,SetLastError = true)]
    public static extern int GetWindowText (IntPtr hwnd,StringBuilder lpString,int nMaxCount);

    [DllImport("user32.dll",CharSet=CharSet.Auto, SetLastError = true)]
    static extern int GetWindowTextLength(IntPtr hWnd);

    static void Main()
    {
       IntPtr thisWindow = FindWindow(null, "Окно");
       IntPtr Rod = thisWindow;

       IntPtr otherWindow = FindWindowEx(Rod, IntPtr.Zero, "RICHEDIT50W", null);

        int len = GetWindowTextLength(otherWindow);
        StringBuilder sb = new StringBuilder(len);
        len = GetWindowText(otherWindow, sb, len);

        Console.WriteLine(sb.ToString(0,len)); //выводит пустую строку
        Console.WriteLine(thisWindow); //выводлит дескриптор родителя
        Console.WriteLine(otherWindow);//выводит дескриптор приемника
        Console.ReadKey(true); 
   }
}

} Please help, how to get title "otherWindow"?

markgenuine
  • 61
  • 1
  • 12
  • надо len = len * 2; и в GetWindowText вместо len передать sb.Capacity -- пример на c# тут – Stack Jan 12 '16 at 19:31

1 Answers1

3

Согласно MSDN:

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

Если вы пытаетесь получить текст контрола в другом процессе (а имя RICHEDIT50W предполагает, что это так), нужно посылать WM_GETTEXT напрямую.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

...

const int WM_GETTEXT = 0xD;
StringBuilder buffer = new StringBuilder(65535);
SendMessage(hWnd_of_Notepad_Editor, WM_GETTEXT, buffer.Length, buffer);
Nicolas Chabanovsky
  • 51,426
  • 87
  • 267
  • 507