2011-11-10 43 views
6

嗨我有一個窗體裏面有幾個框架。Delphi 7 - 處理表單中嵌入框架的MouseWheel事件?

對於一些框架,我希望滾動的內容(或至少處理mousewheel事件)。

我曾嘗試以下:

只需指定一個OnMouseWheel事件處理程序對每一幀

重寫鼠標滾輪事件父窗體:

procedure TFmReview.MouseWheelHandler(var Message: TMessage); 
var Control: TControl; 
begin 
    Control := ControlAtPos(ScreenToClient(SmallPointToPoint(TWMMouseWheel(Message).Pos)), False, True); 
    if Assigned(Control) and (Control <> ActiveControl) then 
    begin 
     ShowMessage(Control.Name); 
     Message.Result := Control.Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam); 
     if Message.Result = 0 then 
      Control.DefaultHandler(Message); 
    end else inherited MouseWheelHandler(Message); 
end; 

不幸的是這兩個似乎沒有工作。

  • 在情況1中,事件從不觸發,但父窗體鼠標滾輪處理程序被觸發。
  • 在情況2中,接收焦點的控件是保存要發送mousewheel事件的幀的面板。

所以,簡單地說,我怎樣才能將mousewheel事件指向鼠標光標所在的最頂端控件(無論光標在哪個框架/父/表單等中)?

+0

看看[這些SO答案] [ 1]他們可能會幫助。 [1]:http://stackoverflow.com/questions/2472743/what-is-the-best-method-for-implementing-mouse-wheel-activity-in-delphi-vcl-form –

+0

可能的重複[如何指導鼠標滾輪輸入來控制光標,而不是集中?](http://stackoverflow.com/questions/2251019/how-to-direct-the-mouse-wheel-input-to-control -under-cursor-instead-of-focused) – Kromster

回答

1

要推遲鼠標滾輪處理到TWinControl超過這是目前的鼠標光標,覆蓋在主框架使用這樣的碼形成MouseWheelHandler方法:

type 
    TMainForm = class(TForm) 
    private 
    procedure MouseWheelHandler(var AMessage: TMessage); override; 
    public 
    { Public declarations } 
    end; 

implementation 

procedure TMainForm.MouseWheelHandler(var AMessage: TMessage); 
var 
    Control: TWinControl; 
begin 
    Control := FindVCLWindow(SmallPointToPoint(TWMMouseWheel(AMessage).Pos)); 
    if Assigned(Control) then 
    begin 
    AMessage.Result := Control.Perform(CM_MOUSEWHEEL, AMessage.WParam, 
     AMessage.LParam); 
    if AMessage.Result = 0 then 
     Control.DefaultHandler(AMessage); 
    end 
    else 
    inherited MouseWheelHandler(AMessage); 
end; 
+0

出於某種原因,當我在TMainForm上方滾動時,此代碼會生成StackOverflow – Kromster