.Net遍历窗口

前端之家收集整理的这篇文章主要介绍了.Net遍历窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

'需要导入3个命名空间导入方法为,在文件最前面写以下内容@H_301_1@'VB.NET code@H_301_1@Imports System.Runtime.InteropServices@H_301_1@Imports System.Text@H_301_1@Imports System.Threading

'以下方法测试通过。有效。获得文本框内容。@H_301_1@'VB.NET code@H_301_1@ <DllImport("user32.dll",EntryPoint:="FindWindowEx")> _@H_301_1@ Public Shared Function FindWindowEx(ByVal hWnd1 As IntPtr,_@H_301_1@ ByVal hWnd2 As Integer,ByVal lpsz1 As String,_@H_301_1@ ByVal lpsz2 As String) As Integer@H_301_1@ End Function@H_301_1@ <DllImport("User32 ")> @H_301_1@ Public Shared Function SendMessage(ByVal hWnd As Integer,_@H_301_1@ ByVal Msg As Integer,ByVal wParam As Integer,_@H_301_1@ ByVal lParam As IntPtr) As Boolean@H_301_1@ End Function

Public Const WM_GETTEXT As Integer = &HD@H_301_1@ Private Shared Sub Test04()@H_301_1@ Dim running As Boolean = True@H_301_1@ Dim process_array As Process() = Process.GetProcesses()@H_301_1@ For Each p As Process In process_array@H_301_1@ If p.MainWindowTitle.IndexOf("记事本") <> -1 Then@H_301_1@ Dim hEdit As Integer = FindWindowEx(p.MainWindowHandle,"Edit","")@H_301_1@ Dim w As String = " "@H_301_1@ Dim ptr As IntPtr = Marshal.StringToHGlobalAnsi(w)@H_301_1@ If SendMessage(hEdit,WM_GETTEXT,100,ptr) Then@H_301_1@ MessageBox.Show(Marshal.PtrToStringAnsi(ptr))@H_301_1@ End If@H_301_1@ End If@H_301_1@ Next@H_301_1@ End Sub

@H_301_1@using System.Runtime.InteropServices;@H_301_1@using System.Text;@H_301_1@using System.Threading;

@H_301_1@[DllImport("user32.dll",EntryPoint = "FindWindowEx")]@H_301_1@public static extern int FindWindowEx(IntPtr hWnd1,@H_301_1@ int hWnd2,string lpsz1,@H_301_1@ string lpsz2);@H_301_1@[DllImport("user32.dll",EntryPoint = "GetWindowText")]@H_301_1@public static extern int GetWindowText(int hwnd,StringBuilder @H_301_1@ lpString,int cch);@H_301_1@[DllImport("User32 ")]@H_301_1@public static extern bool SendMessage(int hWnd,@H_301_1@ int Msg,int wParam,@H_301_1@ IntPtr lParam);

public const int WM_GETTEXT = 0xD;@H_301_1@private static void Test04()@H_301_1@{@H_301_1@ bool running = true;@H_301_1@ new Thread(() =>@H_301_1@ {@H_301_1@ while (running)@H_301_1@ {@H_301_1@ Process[] process_array = Process.GetProcesses();@H_301_1@ foreach (Process p in process_array)@H_301_1@ {@H_301_1@ if (p.MainWindowTitle.IndexOf("记事本") != -1)//找到了@H_301_1@ {@H_301_1@ int hEdit = FindWindowEx(p.MainWindowHandle,"");@H_301_1@ string w = " ";@H_301_1@ IntPtr ptr = Marshal.StringToHGlobalAnsi(w);@H_301_1@ if (SendMessage(hEdit,ptr))

Console.WriteLine(Marshal.PtrToStringAnsi(ptr)); } } int tick = Environment.TickCount; while (running && Environment.TickCount - tick < 10000) { Thread.Sleep(10); } } Console.WriteLine("run over"); }).Start(); while (Console.ReadLine().ToLower() != "quit") ; running = false;}

猜你在找的VB相关文章