Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 无法在vc+中获取特定窗口的坐标(我已经尝试了这些示例…仍然)+;_Windows_Winapi_Visual C++ - Fatal编程技术网

Windows 无法在vc+中获取特定窗口的坐标(我已经尝试了这些示例…仍然)+;

Windows 无法在vc+中获取特定窗口的坐标(我已经尝试了这些示例…仍然)+;,windows,winapi,visual-c++,Windows,Winapi,Visual C++,我一直在尝试各种代码片段,但仍然没有成功。我只是尝试使用带有特定标题字符串的FindWindow(NULL,WINDOWTITLE)查找一个打开的窗口(它是浏览器窗口)。一旦获得窗口句柄,我需要使用GetWindowRect获得坐标 这段代码给了我坐标,但它似乎是在一个无限循环中,大约有100行坐标输出,应该只有1行。我没有看到任何while构造(最初是Java程序员)。。。想知道为什么它在重复 struct WindowInfo { HWND m_hWnd; string m_titl

我一直在尝试各种代码片段,但仍然没有成功。我只是尝试使用带有特定标题字符串的
FindWindow(NULL,WINDOWTITLE)
查找一个打开的窗口(它是浏览器窗口)。一旦获得窗口句柄,我需要使用
GetWindowRect
获得坐标

这段代码给了我坐标,但它似乎是在一个无限循环中,大约有100行坐标输出,应该只有1行。我没有看到任何while构造(最初是Java程序员)。。。想知道为什么它在重复

struct WindowInfo
{
  HWND m_hWnd;
  string m_title;
  WindowInfo(HWND hwnd, string title) : m_hWnd(hwnd), m_title(title) {}
};

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
  vector<WindowInfo*> & windows = *(vector<WindowInfo*>*)lParam;
  char title[256];

  HANDLE wndHandle;
  LPCWSTR WINDOWTITLE = L"eBargain 2 Share - Windpos Internet Explorer";
  RECT rRect;
  LPRECT lpRect;
  RECT rc;

  hwnd = FindWindow(NULL,WINDOWTITLE);  

  GetWindowRect(hwnd,&rc);
  printf("Position: %d x %d\tSize: %d x %d\n",rc.left,rc.top,rc.right- rc.left,rc.bottom-rc.top);

       /* Enumerating through all the windows tells me that I am on the right    track... (Should I just try to find the TITLE STRING by comparing every title from the following enumeration ?  
       */
      GetWindowTextA(hwnd, title, 256);
    windows.push_back(new WindowInfo(hwnd,title));
   //  printf("%s\n", title);
    return TRUE;

}
int main()
{
  vector<WindowInfo*> windows;
  BOOL ret = EnumWindows(EnumWindowsProc, (LPARAM) &windows);
  if ( ret )
  {
      //windows have windowinfo of all enumerated windows
  }
}
struct WindowInfo
{
HWND m_HWND;
字符串m_标题;
WindowInfo(HWND-HWND,字符串标题):m_-HWND(HWND),m_-title(title){}
};
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
向量&windows=*(向量*)lParam;
字符标题[256];
处理wndHandle;
LPCWSTR WINDOWTITLE=L“eBargain 2共享-Windpos Internet Explorer”;
纠正;
LPRECT-LPRECT;
RECT-rc;
hwnd=FindWindow(NULL,WINDOWTITLE);
GetWindowRect(hwnd和rc);
printf(“位置:%d x%d\t大小:%d x%d\n”,rc.left,rc.top,rc.right-rc.left,rc.bottom rc.top);
/*在所有窗口中枚举表明我走对了方向…(我是否应该通过比较下面枚举中的每个标题来尝试查找标题字符串?
*/
GetWindowTextA(hwnd,标题,256);
windows.push_back(新窗口信息(hwnd,标题));
//printf(“%s\n”,标题);
返回TRUE;
}
int main()
{
向量窗口;
BOOL ret=EnumWindows(EnumWindowsProc,(LPARAM)和windows);
如果(ret)
{
//windows具有所有枚举窗口的windowinfo
}
}

您的
EnumWindowsProc
似乎有点困惑-您是在枚举还是在使用
FindWindow

如果枚举,只需获取窗口标题并与搜索的字符串进行比较:

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
  char title[256];
  if (GetWindowTextA(hwnd, title, 256)) {
    if (strcmp(title, "eBargain 2 Share - Windpos Internet Explorer") == 0) {
      /* GetWindowRect ... */
    }
  }
  return TRUE;
}
或者,如果您使用的是
FindWindow
,则无需枚举:

int main() {
  HWND hwnd = FindWindowA(0, "eBargain 2 Share - Windpos Internet Explorer");
  if (hwnd) {
    /* GetWindowRect ... */      
  }    
}

EnumWindowsProc版本的一个小补充:如果您只需要第一个匹配项,则在找到所需匹配项并准备返回后返回FALSE,枚举将停止。如果返回TRUE,windows将继续枚举,以防有任何进一步的匹配项。(这在多个窗口具有相同名称的情况下非常有用,您需要进行额外的测试以确定所需的窗口。)