Most of the mouse now has a mouse wheel, for example, when we browse the Web page, the mouse wheel move up and down, the page will automatically scroll up and down. The mouse wheel does bring a lot of convenience to our operation, but in most of Delphi control only support Mousedown,mouseup,mousemove events, and not directly support MouseWheel events, We have this problem when we help customers design a viewing program. This viewer data is placed in a DBGrid, the data is more than the entire screen, if you move up and down with the mouse wheel, the DBGrid cursor only in the visible range of movement, beyond the screen data must use the keyboard or the right scroll bar, causing great inconvenience to users, The customer strongly requests support for mouse wheel operation. But what about viewing the DBGrid event properties without support for the mouse wheel?
We know that the Windows operating system is message driven, so if the mouse wheel scrolling up and down, there must be a corresponding event, after looking for information, we learned that when the mouse wheel scrolling up and down occurs is the WM_MouseWheel event, so if we capture this event, Can you handle the mouse wheel event?
Just do it, so let's add a OnMouseWheel event for DBGrid and create a new DBGrid component that supports the mouse wheel.
Let's create a new application, call Mydbgrid bar, select Menu File-new Application, and then select Menu File-new-component
Because our new component is inherited from DBGrid, Ancestor type chooses Tdbgrid,class name to fill in our component names Tmydbgrid, the resulting component is placed on the Samples page, and the component's frame is generated when you click OK.
Now we start to do the most critical part. When the mouse wheel scrolls up and down, a WM_MOUSEWHEEL message is sent, MouseWheel message has several parameters,
1.fwkeys= LoWord (WParam), which indicates whether various virtual keys are pressed, has the following values:
Value |
Description |
Mk_control |
Press the CTRL key |
Mk_lbutton |
Press the left mouse button |
Mk_mbutton |
Press the middle mouse button |
Mk_rbutton |
Press the right mouse button |
Mk_shift |
Press the SHIFT key |
2.zDelta = (short) hiword (WParam)
The distance the mouse wheel scrolls, if forward is positive, and backward is negative.
3. Xpos = (short) loword (LParam)
ypos= (short) hiword (LParam)
The position of the mouse.