C# how to use WM_GETTEXT / GetWindowText API

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I want to get the content of the control / handle of an application..

Here's the experimental code..

 Process[] processes = Process.GetProcessesByName("Notepad");         foreach (Process p in processes)         {             StringBuilder sb = new StringBuilder();             IntPtr pFoundWindow = p.MainWindowHandle;              List  s =    GetChildWindows(pFoundWindow);              // function that returns a              //list of handle from child component on a given application.               foreach (IntPtr test in s)              {               // Now I want something here that will return/show                 the text on the notepad..                }               GetWindowText(pFoundWindow, sb,256);             MessageBox.Show(sb.ToString()); // this shows the title.. no problem with that          }  

any idea? I've read some API method like GetWindowText or WM_GETTEXT but I dont know how to use it or apply it on my code.. I need a tutorial or sample code...

Thanks in advance : )

回答1:

    public class Class1       {          [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]         public static extern int RegisterWindowMessage(string lpString);           [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)] //         public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);           [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]         public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam,         int lparam);          const int  WM_GETTEXT = 0x000D;         const int WM_GETTEXTLENGTH = 0x000E;          public void RegisterControlforMessages()         {           RegisterWindowMessage("WM_GETTEXT");         }         public string GetControlText(IntPtr hWnd)         {            StringBuilder title = new StringBuilder();            // Get the size of the string required to hold the window title.            Int32 size = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();            // If the return is 0, there is no title.            if (size > 0)           {         title = new StringBuilder(size + 1);          SendMessage(hWnd,( int)WM_GETTEXT, title.Capacity, title);             }           return title.ToString();         }      } 

sorry code formatting took a long time , still getting use to it.



回答2:

GetWindowText won't give you the content of edit windows from other applications - it only supports default-managed text [like the captions of labels] across processes to prevent hangs... you'll have to send WM_GETTEXT.

You'll need to use a StringBuilder version of SendMessage:

[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 sb = new StringBuilder(65535); // needs to be big enough for the whole text SendMessage(hWnd_of_Notepad_Editor, WM_GETTEXT, sb.Length, sb); 


回答3:

Have a look at http://pinvoke.net/default.aspx/user32/GetWindowText.html and also the documentation on MSDN. Below you find a short code example how to use the GetWindowText method.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!