gpt4 book ai didi

c++ - 如何使用 rad studio 显示窗口总数?

转载 作者:行者123 更新时间:2023-11-28 02:21:23 25 4
gpt4 key购买 nike

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

[bcc32 Error] Unit1.cpp(15): E2031 Cannot cast from 'int (stdcall * (_closure )(HWND *,long))(HWND__ *,long)' to '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));
}

关于c++ - 如何使用 rad studio 显示窗口总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com