2017-08-15 68 views
0

無論我如何嘗試使用它,我都無法動態創建TScrollBox併爲其分配OnMouseWheelEvent處理程序。我得到以下編譯器錯誤:C++ Builder TMouseWheelEvent編譯器錯誤

E2034 Cannot convert 'void (_fastcall * (_closure)(TObject *,TShiftState,int,TPoint &,bool &))(TObject *,TShiftState,int,TPoint &,bool &)' to 'TMouseWheelEvent'

我對OnMouseWheelEvent處理程序聲明是正確的(只要我可以告訴):

.... 
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; 
    } 
} 

回答

0

這是一個編譯錯誤,而不是一個鏈接錯誤。

請看Controls.hppTMouseWheelEvent的實際聲明。您的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)。這種差異在案英巴卡迪諾的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) 
{ 
    ... 
} 
+0

所以這只是我缺少的常量(對於32位)。 謝謝! –