PDA

View Full Version : Handling Mouse Events



MarkyD
26-05-2003, 03:25 PM
I'm having trouble regarding mouse down and up events, specifically with the TTreeView control. Delphi offers OnMouseDown and OnMouseUp events for handling mouse events, but when testing them I've found them to be quite buggy. For example, when clicking the right mouse button, an OnMouseDown event is not fired until the mouse is released. It gets worse when clicking several buttons at once: pressing the left mouse button, the right mouse button, releasing the left mouse button and finally the right one, causes two OnMouseDown events to be fired - one for each button, along with *3* OnMouseUp events, *all* for the right mouse button.

To fix this, I've put together my own tree view control, inherited from TTreeView, which contains OnMouseButtonDown and OnMouseButtonUp events, along with 6 procedures used to recieve windows messages:


procedure LMouseDown(var Msg: TMsg); message WM_LBUTTONDOWN;
procedure LMouseUp(var Msg: TMsg); message WM_LBUTTONUP;
procedure MMouseDown(var Msg: TMsg); message WM_MBUTTONDOWN;
procedure MMouseUp(var Msg: TMsg); message WM_MBUTTONUP;
procedure RMouseDown(var Msg: TMsg); message WM_RBUTTONDOWN;
procedure RMouseUp(var Msg: TMsg); message WM_RBUTTONUP;


These procedures go on to fire the OnMouseButtonDown and OnMouseButtonUp events. This seems to fix the old behaviour, however, by doing this I seem to have destroyed the control's own mouse handling --- it no-longer reports regular OnMouseDown and OnMouseUp events, as well as not performing basic things such as selecting nodes when they are clicked on. I'm guessing the 6 procedures above used to handle window's messages are conflicting with other procedures used by one of the classes my control is derived from.

Ok, to the point: Is there anyway to keep my own handlers and events for the mouse, whilst allowing the control to function properly?

If it matters, I'm using Delphi 7 Personal Edition --- no VCL source code.

Harry Hunt
26-05-2003, 03:57 PM
Create a custom WndProc for your TTreeView decendant. Then handle your mouse events manually and before your WndProc's final "end" call "inherited;".

MarkyD
26-05-2003, 04:44 PM
Cheers for the reply. I've done what you suggested, and it seems to be an improvement. :) However now it appears as though the WndProc isn't recieving all mouse messages.

When testing it out, the left and middle buttons function correctly when clicking them. However the right does not. When simply clicking the right mouse button, I recieve a mouse down message, but no mouse up message. But if I were to press the button down, move it, and then release it, I recieve the mouse up message. There are other situations with other combinations of keys which cause this behaviour --- the only way I recieve *every* mouse down and up message is when I move the mouse inbetween.

I've attempted to fix this by faking mouse movement by posting a WM_MOUSEMOVE message to the tree view when any mouse down/up message is recieved, as well as calling SetCursorPos with the current mouse coordinates, but neither of these methods work.

Any ideas? :(

Harry Hunt
26-05-2003, 08:20 PM
weird. I never encountered anything like this. Sorry, I don't think I can help you with that :(

MarkyD
28-05-2003, 09:42 PM
Noone has any suggestions? Other than saving up my money and buying a version of Delphi which contains VCL source code (which won't be happening any time soon), or writing my own TTreeView control from scratch (:shock:), I'm out of ideas. :cry: Owh...