本文介绍了C# 如何使用 WM_GETTEXT/GetWindowText API/窗口标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取应用程序的控件/句柄的内容..
I want to get the content of the control / handle of an application..
这是实验代码..
Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process p in processes)
{
StringBuilder sb = new StringBuilder();
IntPtr pFoundWindow = p.MainWindowHandle;
List <IntPtr> 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
}
有什么想法吗?我已经阅读了一些 API 方法,如 GetWindowText 或 WM_GETTEXT,但我不知道如何使用它或将它应用到我的代码中.我需要教程或示例代码...
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...
提前致谢:)
推荐答案
public class GetTextTestClass{
[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 string GetControlText(IntPtr hWnd){
// Get the size of the string required to hold the window title (including trailing null.)
Int32 titleSize = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();
// If titleSize is 0, there is no title so return an empty string (or null)
if (titleSize == 0)
return String.Empty;
StringBuilder title = new StringBuilder(titleSize + 1);
SendMessage(hWnd, (int)WM_GETTEXT, title.Capacity, title);
return title.ToString();
}
}
这篇关于C# 如何使用 WM_GETTEXT/GetWindowText API/窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!