如何使用rad studio显示窗口总数

How to show total number of windows using rad studio?

本文关键字:窗口 显示 studio 何使用 rad      更新时间:2023-10-16

我正在尝试下面的代码在我的cpp文件,它给我的错误:

[bcc32错误]Unit1.cpp(15): E2031不能从'int (stdcall * (_closure)(HWND *,long))(HWND__ *,long)'转换为'int (stdcall *)(HWND *,long)'

我做错了什么?

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
    BOOL WINAPI EnumWindows((WNDENUMPROC) EnumWinProc, NULL);
}
BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam)
{
    char title[80];
    GetWindowText(hwnd,title,sizeof(title));
    Listbox1->Items->Add(title);
    return TRUE;
}

您所展示的不可能是您的实际代码。首先,您对EnumWindows()使用的语法是错误的,不能按原样编译。其次,错误是抱怨转换__closure,这意味着您试图使用非静态类方法作为回调(这是您不能做的),但在您所展示的代码中没有这样的方法。

代码应该是的样子:
class TForm1 : public TForm
{
__published:
    TListBox *ListBox1;
    ...
private:
    static BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam);
    ...
public:
    __fastcall TForm1(TComponent* Owner);
    ...
};

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    EnumWindows(&EnumWinProc, reinterpret_cast<LPARAM>(this));
}
BOOL CALLBACK TForm1::EnumWinProc(HWND hwnd, LPARAM lParam)
{
    TCHAR title[80];
    if (GetWindowText(hwnd, title, 80))
        reinterpret_cast<TForm1*>(lParam)->ListBox1->Items->Add(title);
    return TRUE;
}

另外:

// Note: NOT a member of the TForm1 class...
BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam)
{
    TCHAR title[80];
    if (GetWindowText(hwnd, title, 80))
        reinterpret_cast<TStrings*>(lParam)->Add(title);
    return TRUE;
}
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    EnumWindows(&EnumWinProc, reinterpret_cast<LPARAM>(ListBox1->Items));
}

丢失BOOL WINAPI。你正在尝试调用一个函数,而不是声明一个。

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
   EnumWindows((WNDENUMPROC) EnumWinProc, NULL);
}

同时,丢掉不必要的(WNDENUMPROC)转换。你的回调函数应该有正确的签名,如果没有,你想知道。