3

I am trying to tidy up my code now that Delphi XE2 is available - my code dates from Borland Pascal 7 so there are lots of 'old' (but working!) Win32 techniques and natually I have platform independence in mind too. Support for the mouse wheel has come up before here with several prior questions 1 2 and 3. As with some of these answers, my own solution is a simple mouse message intercept using a TApplicationEvents component:

  procedure TForm6.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);

  procedure ProcessMouseWheelMessage;
  begin
    Msg.message := WM_KEYDOWN;

    Msg.lParam := 0;

    If Integer(Msg.wParam) > 0 then
      Msg.wParam := VK_UP
    else
      Msg.wParam := VK_DOWN;

    Handled := False;
  end;

begin
  Case Msg.message of
    WM_MOUSEWHEEL :
      ProcessMouseWheelMessage;
  end;
end;

I revisited this code today because 'Msg.wParam' is now NativeInt, breaking use of negative Msg.wParam values in the above code unless you use Integer(Msg.wParam). It made me notice that I had not seen any really definitive use of the mouse wheel for Delphi code - terrible when all mice now have wheels and Delphi is at the 'cutting edge' again! I would have expected a property, a component or some other more 'exposed' solution, and what about Fire Monkey wheel support?

Do I carry on with my solution or is there a better way?

Community
  • 1
  • 1
Brian Frost
  • 13,334
  • 11
  • 80
  • 154

2 Answers2

6

In XE2 (and indeed all the recent releases) you don't need to do anything. The standard controls support mouse wheel scrolling out of the box. Just get rid of this old code.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Ah I see. Thanks I was confusing scroll selection up with scrolling the background. I tried TListBox in a blank project and it did not seem to support mouse wheel, but I see it does if you overflow the displayed item and get a scrollbar. – Brian Frost Nov 17 '11 at 13:07
3

Delphi components that have a Windows handle (descendents of TWinControl) have the OnMouseWheel, OnMouseWheelUp and OnMouseWheelDown events.

If you want to add a mousewheel event to a control that does not descent from TWinControl, see this article: http://delphi.about.com/od/delphitips2010/qt/timage-handling-mouse-wheel-messages.htm

Johan
  • 74,508
  • 24
  • 191
  • 319