【问题标题】:How to show total number of windows using rad studio?如何使用 rad studio 显示窗口总数?
【发布时间】:2015-09-01 15:21:01
【问题描述】:

我在我的 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;
}

【问题讨论】:

    标签: c++ winapi c++builder


    【解决方案1】:

    您显示的内容不可能是您的真实代码。首先,您对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));
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2011-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        相关资源
        最近更新 更多