正如标题所示,我在问什么可以通过点击屏幕上find。
更确切地说,能够检测到一个精确的元素(可以是在通用浏览器上打开新选项卡的+
符号,或者在Open Office文档上的File
然后Save as...
元素)的点击并返回某种types点击了什么string?
通过使用Win32 API,我可以检测到鼠标左键是否被点击过:
for(character=1; character<=222; character++) { if(GetAsyncKeyState(character) == -32767) { ... switch(character) { case VK_LBUTTON: ...; break; ... } } }
当检测到点击的时候,我知道它被点击的地方(或者至less是发生点击的窗口的名称):
void whichWindow(FILE *f) { HWND foreground = GetForegroundWindow(); char window_title[256]; if(foreground) { GetWindowText(foreground, window_title, 256); fputs(window_title, f); getMenuFromWindow(f, foreground); } else fputs("Problem retrieving window's title name", f); }
但是,我不知道点击了什么 。
你会发现,点击鼠标的坐标对我来说毫无用处,除非我知道屏幕上所有可以点击的坐标。
我检查了WindowsUI自动化,但其中大部分是在C#和我的porpouses,我宁愿留在C.
很多人告诉我使用networking中的一个程序,但正如我所说,我宁愿只使用C,而不包括一些extern库。
所以:有没有办法find我是否在某些文本编辑器上单击了“新build文件”,或者在VLC上单击了“播放”,只需使用Windows API?
你可以用下面的逻辑来获得鼠标光标下的(子)窗口的句柄,文本和类名:
HWND hWndF = GetForegroundWindow(); if (hWndF != NULL) { HWND hWndC; TCHAR szClassName[64], szWindowText[256]; POINT pt; GetClassName(hWndF, szClassName, _countof(szClassName)); // main window GetWindowText(hWndF, szWindowText, _countof(szWindowText)); // main window GetCursorPos(&pt); // in screen coordinates ScreenToClient(hWndF, &pt); // in client coordinates of main window hWndC = ChildWindowFromPointEx(hWndF, pt, CWP_ALL); if (hWndC != NULL && hWndC != hWndF) { GetClassName(hWndC, szClassName, _countof(szClassName)); // child window GetWindowText(hWndC, szWindowText, _countof(szWindowText)); // child window } }
我不是一个Windows专家,但阅读文档,它可以工作的一种方式:
TCHAR lpClassName[64]; HWND foreground = GetForegroundWindow(); int ret; ret = GetClassName(foreground, (LPTSTR)lpClassName, 64); if(ret == 0) { // GetLastError and friends... }
这样你应该得到以前用RegisterClass()
或RegisterClassEx()
RegisterClass()
的窗口类的名字。 该字符串应该告诉你更多关于窗口的性质。
注意:因为我不知道/获取Win32数据类型,所以使用char数组和/或GetClassName()
的长度参数可能是错误的。
您可以使用GetClassInfo()
从类中获取更多信息。 有关一般参考,请阅读MSDN中的关于窗口类 。