C# 获得其他应用程序窗口中按钮、列表、文本框等控件内容的方法
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
代码如下:
1. // Program.cs 2. using System; 3. using System.Collections.Generic; 4. using System.ComponentModel; 5. using System.Data; 6. using System.Drawing; 7. using System.Linq; 8. using System.Text; 9. using System.Windows.Forms; 10.using System.Diagnostics; 11.using System.Runtime.InteropServices; 12. 13.namespace WindowsFormsApplication4 14.{ 15. public partial class Form1 : Form 16. { 17. public Form1() 18. { 19. InitializeComponent(); 20. } 21. 22. //查找窗口API 23. [DllImport("user32.dll", EntryPoint = "FindWindow")] 24. public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); 25. 26. //查找窗口内控件句柄 27. [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] 28. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 29. 30. //发送消息 31. 32. [DllImport("user32.dll", EntryPoint = "SendMessageA")] 33. private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam); 34. 35. private void button1_Click(object sender, EventArgs e) 36. { 37. IntPtr mW = FindWindow(null, "MyForm"); 38. if (mW != IntPtr.Zero) 39. { 40. MessageBox.Show("找到程序窗口!"); 41. IntPtr et = FindWindowEx(mW, IntPtr.Zero, "WindowsForms10.EDIT.app.0.2bf8098_r16_ad1", null); //第三个参数使用spy++找的,不要告诉我不知道呀 42. if (et != IntPtr.Zero) { 43. MessageBox.Show("找到文本框!"); 44. 45. 46. StringBuilder s1 = new StringBuilder(512); 47. SendMessage(et, 0x000d, 510, s1); //0x000d是向控件发送获取文本消息的消息编号 48. MessageBox.Show(s1.ToString()); 49. } else { 50. MessageBox.Show("没找到文本框!"); 51. } 52. } 53. else { 54. MessageBox.Show("没有窗口!"); 55. } 56. } 57. } 58.} 该文章在 2023/12/29 23:30:37 编辑过 |
关键字查询
相关文章
正在查询... |