从活动窗口中读取内容 [英] reading contents from active window

查看:69
本文介绍了从活动窗口中读取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


任何人都可以对如何阅读活动记事本文件的内容提出一些建议吗?即说我已经打开c:\ read.txt并正在使用它,因此其中已经有一些数据.....使用我的应用程序,我需要阅读此记事本的内容并将其打印在控制台窗口上或保存它到其他文件.

是否有任何win32 api可以执行此操作,如果可以的话,否则请告诉我如何阅读内容以及查找解决方案的可能方法是什么.

更清楚地假设用户打开了一个名为D:\ somedata.txt的文件,并且该文件包含一些数据.因此,现在这将是活动窗口,当我运行我的应用程序时,我应该能够读取此somdata.txt文件的内容,该文件名未作为输入提供,因为用户可以打开他/她需要的任何文本文件.无论文件名如何,我的应用程序都应该能够从已打开的记事本文件中读取内容.

谢谢

Hi
Can any one please suggest some idea on how to read the contents of active notepad file. i.e say i have opened c:\read.txt and working with it, so there is already some data in it ..... using my application i need to read the contents of this notepad and print it on the console window or save it to some other file.

Is there any win32 api to perform this, if yes please, else tell me how can i read the contents and what are the possible ways of finding solution.

More clearly Suppose user opens a file called D:\somedata.txt and this file contains some data. So for now this will be the active window, when i run my application i should be able to read the contents of this somdata.txt file, the file name is not given as input, because user may open any text file he/she needs. Irrespective of file name my application should be able to read contents from already opened notepad file.

Thank you

推荐答案

如果要获取活动窗口,请使用GetForegroundWindow API.
如果要获取所有正在运行的记事本实例,请像这样使用FindWindow-
If you want to get the active window, use the GetForegroundWindow API.
If you want to get all the running instances of Notepad, use FindWindow like so -
FindWindow(_T("Notepad"), 0);


获取窗口的句柄后,使用FindWindowEx将句柄移至记事本的编辑控件-


After you get the handle to the window, use FindWindowEx to get the handle to the edit control of notepad -

FindWindowEx(hNotepad, 0, _T("Edit"), 0);


然后,您可以使用SendMessageWM_GETTEXT消息发送到FindWindowEx返回的句柄,以在记事本中获取文本.


You can then send the WM_GETTEXT message using SendMessage to the handle returned by FindWindowEx to get the text within notepad.


没有记事本文件"之类的东西.

您可以执行以下操作:枚举系统中的所有窗口.首先,获取桌面上所有可以使用Windows API GetDesktopWindow http://msdn.microsoft.com/zh-cn/library/ms633515%28v=VS.85%29.aspx [ http://msdn.microsoft.com/en-us/library/ms633509(v = VS.85 ).aspx [ ^ ].

递归使用这些功能,您可以首先获取桌面上的所有顶级窗口,而不是每个顶级窗口的所有子级.

现在,要选择代表文本编辑控件的窗口,您需要了解一些有关其类型的信息.筛选正确类型的方法之一是使用函数GetClassName,请参见 http://msdn.microsoft.com/zh-CN/library/ms633582%28v=vs.85%29.aspx [ http://msdn.microsoft.com/en-us/library /ms633520%28v=VS.85%29.aspx [ ^ ].对于许多窗口来说,这是没有意义的;对于带有标题栏的重叠顶层窗口,它将返回标题文本,但是对于文本编辑控件,它将返回所需内容.

—SA
There is no such thing as "notepad file".

What you can do is this: enumerate all windows in the system. First, get all top-level windows on the desktop which you can get using Windows API GetDesktopWindow, http://msdn.microsoft.com/en-us/library/ms633504(v=VS.85).aspx[^].

You can iterate through children first using GetWindow with the uCmd parameter GW_CHILD, than GetNextWindow, see http://msdn.microsoft.com/en-us/library/ms633515%28v=VS.85%29.aspx[^], http://msdn.microsoft.com/en-us/library/ms633509(v=VS.85).aspx[^].

Using these function recursively, you can get first get all top-level windows on the desktop and than all the children of each of those top-level windows.

Now, to select the window which represents the text editing control, you need to know something about its type. One of the ways to filter right type is using the function GetClassName, see http://msdn.microsoft.com/en-us/library/ms633582%28v=vs.85%29.aspx[^].

When this is done; and you figured out what window do you want, use the function GetWindowText to retrieve the text you need. See http://msdn.microsoft.com/en-us/library/ms633520%28v=VS.85%29.aspx[^]. For many windows it would not make sense, for overlapped top-level windows with the title bar it will return the title text, but for text edit control it will return what you need.

—SA


似乎我的回答如此之久,以至于超人已经在我度过的时间内提供了(更好)的解决方案.输入这个.哎呀.


当然可以虽然最初的诱惑可能是找到记事本主窗口,然后在其上获取GetWindowText,但这是行不通的.这只会给您窗口的标题.实际的文本区域是记事本主窗口的子窗口.

我将获得记事本主窗口的句柄,然后再枚举其所有子窗口.我希望目标窗口是子#N",其中每次运行程序时N都相同.
我在记事本中键入(说)100个字符,然后找到哪个子窗口的文本长度为100个字符.这将(应该!)是用户键入的编辑框.如果此窗口是第二或第三子窗口,则它应始终是第二或第三窗口-当编辑窗口的文本长度未知时很有用.

在这里,此代码将获取找到的第一个正在运行的记事本实例的窗口标题.

It seems my answer was so long that Superman has already provided a (much better) solution in the time I''ve spent typing this. Oops.


Sure thing. While the initial temptation may be to find the Notepad main window then to GetWindowText on it, this won''t work. This will give you the title of the window only. The actual text area is a child window of the Notepad main window.

I''d get the handle of the main window of notepad then I would enumerate all of it''s child windows. I would expect the target window to be Child #N, where N will be the same for each time you run the program.
I''d type (say) 100 characters into Notepad, then find which child window has a text length of 100 chars. This will(should!) be the edit-box that the user types into. If this window is the 2nd or 3rd child window, it should be consistently the 2nd or 3rd window - useful when the text length of the edit-window is unknown.

Here, this code will get the window title of the first running instance of Notepad found.

char *buffer;
HWND noteHwnd = NULL;

noteHwnd = FindWindowEx(NULL, NULL, "Notepad", NULL);
if (noteHwnd)
{
    int len = GetWindowTextLength(noteHwnd);
    if (len > 0)
    {
        buffer = (char*)calloc(1, len+1);
        GetWindowText(noteHwnd, buffer, len+1);
        printf("Number of characters found: %d\n", len);
        printf("Text: %s\n", buffer);
        free(buffer);
    }
}



当然,另一种方法是调试记事本的副本.通过仔细地逐步执行程序,您可以赶上创建文本编辑窗口的那一刻.然后,您可以确切地看到它存储的地址是什么.稍后再返回,您可以打开OpenProcessMemory并直接从其原始保存的内存中读取窗口的Hwnd. (您可以使用这种内存读取技术来制作一个可以完美地为您玩Minesweeper的机器人)

也就是说,我希望枚举主窗口的子窗口的方法是更有效的方法.


当然,可能已经有数百人这样做了.搜索一个完成的结果可能比获得一个自己的娱乐性要少.这就是为什么我从未寻找完整的解决方案的原因. :wink:



Of course, another way would be to debug a copy of Notepad. By carefull stepping through the program, you can catch the moment that the text-editing window gets created. You can then see exactly what the address is that this is stored. Coming back later you could OpenProcessMemory and read the Hwnd of the window directly from the memory that it was saved to originally. (You can use this technique of memory-reading to make a bot that will play Minesweeper for you flawlessly)

That said, I''d expect the method of enumerating the child windows of the main window to be the more effective method.


Of course, hundreds of people have probably already done just this. Searching for a finished result may well be less entertaining than deriving one yourself. That''s why I never searched for a finished solution. :wink:


这篇关于从活动窗口中读取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆