将指针作为LPARAM传递给EnumWindowsProc .....如何?

Lum*_*mpi 4 c++

我有一个使用该EnumWindows功能的问题.

我想做什么:

我想打电话EnumWindows,然后我的EnumVisiWindowTitles功能.本EnumVisiWindowTitles应得到所有可见窗口的每一个拉手和字幕和存储这些在"lumpi"结构.

后来在主要我想访问"lumpi"并搜索特定的标题字符串.

我的问题是我没有设法将指针传递lumpi[0]EnumVisiWindowTitlesas LPARAM.

也许我的genaral计划不是那么明亮,所以如果你们中的任何人可以帮助我,或者告诉我一个执行相同任务的解决方案,我将非常高兴你的帮助!

我的主要看起来像这样:

int _tmain(int argc, _TCHAR* argv[])

{
 MYHANDLES lumpi[10];
 EnumWindows(EnumVisiWindowTitles, (LPARAM) &lumpi[0]);

blabla
}
Run Code Online (Sandbox Code Playgroud)

Myhandles定义为:

#ifndef handlestruct_H
#define handlestruct_H
struct MYHANDLES
 { public:
  MYHANDLES();  //MYHANDLEconstructor.cpp
  HWND haendchen;
  int count;
  char title[200];
 };

#endif
Run Code Online (Sandbox Code Playgroud)

我的EnumWindowsProc看起来像这样:

using namespace std;
 BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM lumpi) 
{  

 TCHAR String[200]; 

 if (!hWnd)
  return TRUE;// Not a window, return TRUE to Enumwindows in order to get the next handle
 if (!::IsWindowVisible(hWnd))
  return TRUE;// Not visible, return TRUE to Enumwindows in order to get the next handle 
 if (!SendMessageW(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
  return TRUE;// No window title, return TRUE to Enumwindows in order to get the next handle

 lumpi[lumpi[0].count].haendchen = hWnd;

   for (int n=0; n<201; n++)//copy the caption to lumpi struct
    {
     lumpi[lumpi[0].count].title[n] = String[n];
    }

   lumpi[0].count++;  //Increase counter

   wcout<<String<<'\n';
   return true;   //return true to get next handle   
}
Run Code Online (Sandbox Code Playgroud)

我得到一个"表达必须有指向对象类型的指针"指示每个[0]

阿恩

In *_*ico 9

首先,将您的通话更改EnumWindows()为:

int _tmain(int argc, _TCHAR* argv[])
{
    MYHANDLES lumpi[10];
    EnumWindows(&EnumVisiWindowTitles, reinterpret_cast<LPARAM>(lumpi));
    // ...
}
Run Code Online (Sandbox Code Playgroud)

标准C++要求您使用&符号将指针传递给函数.该reinterpret_cast<>()告诉编译器的指针传递给MYHANDLES数组作为-是对LPARAM的参数EnumWindows().

然后,在你的回调中:

BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM ptr)
{
    MYHANDLES* lumpi = reinterpret_cast<MYHANDLES*>(ptr);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

然后我们reinterpret_cast<>()再次使用原始指针检索.然后,您可以lumpi像对待数组一样操作,因为它实际上是一个数组.

这个问题有助于我看到其他问题.你显然使用第一个元素来存储计数,这很奇怪.把它放在另一个struct.

struct MyHandles
{
public:
    MyHandles();
    HWND haendchen;
    char title[200];
};

struct ListOfMyHandles
{
public:
    int count;
    MyHandles handles[10];
};

int _tmain(int argc, _TCHAR* argv[])
{
    ListOfMyHandles lumpi;
    ::EnumWindows(&EnumVisiWindowTitles, reinterpret_cast<LPARAM>(&lumpi));
    // ...
}

BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM ptr)
{
    ListOfMyHandles* lumpi = reinterpret_cast<ListOfMyHandles*>(ptr);
    if(lumpi != 0 && lumpi->count < 10) // Avoid going past the array
    {
        lumpi->handles[lumpi.count] = //...
        // ...
        ++lumpi->count;
        return TRUE;
    }
    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这最多只能容纳10个窗口.为什么不使用std::vector,当你向它添加元素时,它会动态增长?

  • 很好,但它应该是`reinterpret_cast <LPARAM>(&lumpi)`,而不是`reinterpret_cast <LPARAM>(lumpi)`. (3认同)