【问题标题】:C++Builder TMouseWheelEvent compiler errorC++Builder TMouseWheelEvent 编译器错误
【发布时间】:2017-08-15 18:55:08
【问题描述】:

无论我如何尝试使用它,我都无法动态创建TScrollBox 并为其分配OnMouseWheelEvent 处理程序。我收到以下编译器错误:

E2034 无法将 'void (_fastcall * (_closure )(TObject *,TShiftState,int,TPoint &,bool &))(TObject *,TShiftState,int,TPoint &,bool &)' 转换为 'TMouseWheelEvent'

我对@9​​87654323@ 处理程序的声明是正确的(据我所知):

....
TScrollBox *sb = new TScrollBox(funnelCharts);
sb->Top = 5000;
sb->Parent = funnelCharts;
sb->Align = alClient;
sb->Height = funnelCharts->ClientHeight;
sb->OnMouseWheel = scrollEvent;
....

// --------------------------------------------------------------

void __fastcall TForm1::scrollEvent(TObject *Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled)
{
    TScrollBox *scrollbox = dynamic_cast<TScrollBox*>(Sender);
    if (scrollbox)
    {
        for (int i = 1; i < Mouse->WheelScrollLines; i++)
        {
            if (WheelDelta > 0)
            {
                scrollbox->Perform(WM_VSCROLL, SB_LINEUP, 0);
            }
            else
            {
                scrollbox->Perform(WM_VSCROLL, SB_LINEDOWN, 0);
            }
        }
        scrollbox->Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
        Handled = true;
    }
}

【问题讨论】:

    标签: c++builder tscrollbox


    【解决方案1】:

    这是编译器错误,而不是链接器错误。

    Controls.hpp 中查看TMouseWheelEvent 的实际声明。您的 scrollEvent() 方法与实际声明的不匹配,否则您不会收到错误消息。

    根据您是针对 32 位还是 64 位进行编译,TMouseWheelEvent(特别是其 MousePos 参数)的声明方式不同:

    32 位:

    typedef void __fastcall (__closure *TMouseWheelEvent)(System::TObject* Sender, System::Classes::TShiftState Shift, int WheelDelta, const System::Types::TPoint &MousePos, bool &Handled);
    

    64 位:

    typedef void __fastcall (__closure *TMouseWheelEvent)(System::TObject* Sender, System::Classes::TShiftState Shift, int WheelDelta, System::Types::TPoint MousePos, bool &Handled);
    

    这是因为 BCC32 和 BCC64 传递 8 字节结构类型的方式不同(如 TPoint)。这种差异记录在 Embarcadero 的 DocWiki 上:

    Events with Structures or Sets of 5-8 Bytes Are Not Valid for BCC64

    受此差异影响的其他事件类型包括 TGetSiteInfoEventTMouseWheelUpDownEventTContextPopupEvent

    要解决此问题,您必须#ifdef 记录您的代码:

    class TForm1 : public TForm
    {
        ...
        #ifndef _WIN64
        void __fastcall scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, const TPoint &MousePos, bool &Handled);
        #else
        void __fastcall scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, TPoint MousePos, bool &Handled);
        #endif
        ...
    };
    
    ...
    
    #ifndef _WIN64
    void __fastcall TForm1::scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, const TPoint &MousePos, bool &Handled)
    #else
    void __fastcall TForm1::scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, TPoint MousePos, bool &Handled)
    #endif
    {
        ...
    }
    

    或者,更简洁的方法:

    class TForm1 : public TForm
    {
        ...
        void __fastcall scrollEvent(
            TObject* Sender, TShiftState Shift, int WheelDelta,
            #ifndef _WIN64
            const TPoint &MousePos,
            #else
            TPoint MousePos,
            #endif
            bool &Handled);
        ...
    };
    
    ...
    
    void __fastcall TForm1::scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta,
        #ifndef _WIN64
        const TPoint &MousePos,
        #else
        TPoint MousePos,
        #endif
        bool &Handled)
    {
        ...
    }
    

    或者,更清洁:

    #ifndef _WIN64
    #define SAFE_5TO8_PARAM(type, name) const type &name
    #else
    #define SAFE_5TO8_PARAM(type, name) type name
    #endif
    
    class TForm1 : public TForm
    {
        ...
        void __fastcall scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, SAFE_5TO8_PARAM(TPoint, MousePos), bool &Handled);
        ...
    };
    
    ...
    
    void __fastcall TForm1::scrollEvent(TObject* Sender, TShiftState Shift, int WheelDelta, SAFE_5TO8_PARAM(TPoint, MousePos), bool &Handled)
    {
        ...
    }
    

    【讨论】:

    • 所以这只是我缺少的 const (对于 32 位)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2010-11-24
    • 2016-08-19
    • 1970-01-01
    相关资源
    最近更新 更多