如何在C#应用程序中获得与Spy类似的功能?

如何在C#应用程序中获得与Spy类似的功能?,第1张

概述我有兴趣为开源密码管理员 Keepass开发一个插件.现在, Keepass目前根据窗口标题检测到什么密码复制/粘贴.这样可以防止Keepass从当前网站(例如Chrome)中不会主动更新其窗口标题的应用程序检测到您需要的当前密码. 我如何通过另一个进程窗口元素(按钮,标签,文本框)与Spy的工作方式类似?当您运行间谍时,您可以将鼠标悬停在其他程序窗口上,并获取有关各种控件(标签,文本框等)的各种 @H_404_1@我有兴趣为开源密码管理员 Keepass开发一个插件.现在,Keepass目前根据窗口标题检测到什么密码复制/粘贴.这样可以防止Keepass从当前网站(例如Chrome)中不会主动更新其窗口标题的应用程序检测到您需要的当前密码.

我如何通过另一个进程窗口元素(按钮,标签,文本框)与Spy的工作方式类似?当您运行间谍时,您可以将鼠标悬停在其他程序窗口上,并获取有关各种控件(标签,文本框等)的各种属性的各种信息.理想情况下,我想让我的Keepass插件通过浏览活动窗口的元素来增强当前的窗口检测,以找到匹配的帐户来复制/粘贴密码.

如何使用其他进程窗口元素,并可以使用C#检索标签和文本框值?

解决方法 我正在回答类似的问题: How can I detect if a thread has windows handles?.像它所说,主要的想法是通过进程窗口和他们的子窗口枚举使用 EnumWindows和 EnumChildWindows API调用来获取窗口句柄,然后使用WM_GETTEXT调用GetwindowText或SendDlgitemmessage来获取窗口文字.我修改了代码,做出一个应该做你所需要的例子(对不起,有点长:) :)它遍历进程和他们的窗口并将窗口文本转储到控制台中.
static voID Main(string[] args){    foreach (Process procesInfo in Process.GetProcesses())    {        Console.Writeline("process {0} {1:x}",procesInfo.Processname,procesInfo.ID);        foreach (Processthread threadInfo in procesInfo.Threads)        {            // uncomment to dump thread handles            //Console.Writeline("\tthread {0:x}",threadInfo.ID);            IntPtr[] windows = GetwindowHandlesForThread(threadInfo.ID);            if (windows != null && windows.Length > 0)                foreach (IntPtr hWnd in windows)                    Console.Writeline("\twindow {0:x} text:{1} caption:{2}",hWnd.ToInt32(),GetText(hWnd),GetEditText(hWnd));        }    }    Console.Readline();}private static IntPtr[] GetwindowHandlesForThread(int threadHandle){    _results.Clear();    Enumwindows(WindowEnum,threadHandle);    return _results.ToArray();}// enum windowsprivate delegate int EnumwindowsProc(IntPtr hwnd,int lParam);[Dllimport("user32.Dll")]private static extern int Enumwindows(EnumwindowsProc x,int y);[Dllimport("user32")]private static extern bool EnumChildwindows(IntPtr window,EnumwindowsProc callback,int lParam);[Dllimport("user32.dll")]public static extern int GetwindowThreadProcessID(IntPtr handle,out int processID);private static List<IntPtr> _results = new List<IntPtr>();private static int WindowEnum(IntPtr hWnd,int lParam){    int processID = 0;    int threadID = GetwindowThreadProcessID(hWnd,out processID);    if (threadID == lParam)    {        _results.Add(hWnd);        EnumChildwindows(hWnd,WindowEnum,threadID);    }    return 1;}// get window text[Dllimport("user32.dll",CharSet = CharSet.auto,SetLastError = true)]static extern int GetwindowText(IntPtr hWnd,StringBuilder lpString,int nMaxCount);[Dllimport("user32.dll",SetLastError = true,CharSet = CharSet.auto)]static extern int GetwindowTextLength(IntPtr hWnd);private static string GetText(IntPtr hWnd){    int length = GetwindowTextLength(hWnd);    StringBuilder sb = new StringBuilder(length + 1);    GetwindowText(hWnd,sb,sb.Capacity);    return sb.ToString();}// get richedit text public const int GWL_ID = -12;public const int WM_GETTEXT = 0x000D;[Dllimport("User32.dll")]public static extern int getwindowlong(IntPtr hWnd,int index);[Dllimport("User32.dll")]public static extern IntPtr SendDlgitemmessage(IntPtr hWnd,int IDDlgitem,int uMsg,int nMaxCount,StringBuilder lpString);[Dllimport("User32.dll")]public static extern IntPtr GetParent(IntPtr hWnd);private static StringBuilder GetEditText(IntPtr hWnd){    Int32 DWID = getwindowlong(hWnd,GWL_ID);    IntPtr hWndParent = GetParent(hWnd);    StringBuilder Title = new StringBuilder(128);    SendDlgitemmessage(hWndParent,DWID,WM_GETTEXT,128,Title);    return Title;}

希望这有帮助,问候

总结

以上是内存溢出为你收集整理的如何在C#应用程序中获得与Spy类似的功能?全部内容,希望文章能够帮你解决如何在C#应用程序中获得与Spy类似的功能?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1236619.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存