简体   繁体   English

在屏幕上的任何地方检测到焦点突出的文本框

[英]detecting a focused textbox anywhere on the screen

I am writing a simple app in C#. 我正在用C#编写一个简单的应用程序。 I want this app in systray detecting a focused element anywhere on Windows. 我希望系统托盘中的该应用能够检测Windows上任何位置的焦点元素。 If the focused element is a textbox (anywhere where it is possible for the user to input text), then the app should display a notification on systray. 如果关注的元素是文本框(用户可以在任何可能输入文本的地方),则应用程序应在systray上显示通知。 My problem is, how to detect a focused element and check it? 我的问题是,如何检测和检查集中的元素?

There is no absolute way of doing that, there are too many controls allow you to add text. 没有绝对的方法,有太多控件可用于添加文本。

you may search of the caret position like I did in my application, but it is not working every where, 您可以像在应用程序中一样搜索插入符的位置,但它并非在所有地方都有效,

Here is the code I used, 这是我使用的代码,

        GUITHREADINFO lpgui = new GUITHREADINFO();
        IntPtr fore = GetForegroundWindow();
        uint tpid = GetWindowThreadProcessId(fore, IntPtr.Zero);
        lpgui.cbSize = Marshal.SizeOf(lpgui.GetType());
        bool flag = GetGUIThreadInfo(tpid, out lpgui);
        WINDOWINFO pwi = new WINDOWINFO();
        pwi.cbSize = (uint)Marshal.SizeOf(pwi.GetType());
        GetWindowInfo((IntPtr)lpgui.hwndCaret, ref pwi);

        if (flag)
        {
            if (!(lpgui.rcCaret.Location.X == 0 && lpgui.rcCaret.Location.Y == 0))
            {

//TODO

            }
        }

this way works fine in too many applications, and you can place this code in a timer or what ever you wish 这种方式在太多的应用程序中都可以正常工作,您可以将这段代码放在计时器中或任何您想要的地方

You have to use winapi, Please inform me if you are not familiar with api 您必须使用winapi,如果您不熟悉api,请通知我

You can use this code to check focused TextBox. 您可以使用此代码来检查重点突出的TextBox。

foreach (Control item in this.Controls)
        {
            if (item is TextBox && item.Focused)
            {
                //Write your notification code here.
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM