Allow Delphi DBGrid to support mouse wheel

Source: Internet
Author: User
Most of the current mouse has a mouse wheel. For example, when we browse a webpage, the mouse wheel moves up and down, and the webpage automatically scrolls up and down. The mouse wheel does bring a lot of convenience to our operations. However, in most Delphi controls, only events such as mousedown, mouseup, and mousemove are supported, rather than mousewheel events, we encountered this problem when helping customers design a viewing program. The data of this viewing program is placed in a DBGrid, with a large amount of data that exceeds the entire screen. If you move up and down with a mouse wheel, the DBGrid cursor moves only within the visible range, the data that exceeds the screen must use a keyboard or a scroll bar on the right, causing great inconvenience to the user. The customer strongly requires that the mouse wheel operation be supported. However, viewing the event attribute of DBGrid does not support the mouse wheel. What should I do?

We know that the Windows operating system is message-driven. Therefore, if the mouse wheel scroll up or down, a corresponding event will inevitably occur, we know that the wm_mousewheel event occurs when the mouse wheel scroll up or down. In this case, if we capture this event, can we handle the mouse wheel event?

Let's add an onmousewheel event for DBGrid and create a new DBGrid component that supports the mouse wheel.

Let's create an application named mydbgrid. Choose file-new application from the menu, and select File-New-component from the menu.

Because our new component is inherited from DBGrid, select TDBGrid for Ancestor Type and set the class name to tmydbgrid. The generated component is placed on the samples page and click OK, the component framework is generated.

Now let's start with the most important part. When the mouse wheel scroll up or down, the message wm_mousewheel is sent. The mousewheel message has several parameters,

1. fwkeys = loword (wparam) indicates whether various virtual keys are pressed, with the following values:

Value Description
Mk_control Press ctrl
Mk_lbutton Press the left mouse button
Mk_mbutton Right-click
Mk_rbutton Right-click
Mk_shift Press shift

2. zdelta = (short) hiword (wparam)

The scroll distance of the mouse wheel. If it is positive, it is negative.

3. xpos = (short) loword (lparam)

Ypos = (short) hiword (lparam)

The cursor position.

Delphi has defined two events related to the mouse wheel, called tmousewheelevent and tmousewheelupdownevent, which respectively represent the mouse wheel event and the top and bottom scroll events of the mouse wheel. Therefore, we first define three private event variables in tmydbgrid:

Fmousewheel: tmousewheelevent;

Fmousewheelup: tmousewheelupdownevent; // a rolling event

Fmousewheeldown: tmousewheelupdownevent; // a rolling down event

Then define the event attributes in publised:

Property onmousewheel: tmousewheelevent read fmousewheel write fmousewheel;

Property onmousewheelup: tmousewheelupdownevent read fmousewheelup write fmousewheelup;

Property onmousewheeldown: tmousewheelupdownevent read fmousewheeldown write fmousewheeldown;

Then, reload the wndproc function of TDBGrid,

Procedure wndproc (var msg: tmessage); override;

We capture the mouse wheel message in wndproc, as shown below:

VaR

Mousepoint: tpoint;

Handled: Boolean;

Shift: tshiftstate;

Begin

If (msg. MSG = wm_mousewheel) Then // capture the mouse wheel event

Begin

Mousepoint. X: = loword (msg. lparam );

Mousepoint. Y: = hiword (msg. lparam );

Handled: = false;

If (msg. wparam> 0) Then // roll up

Fmousewheelup (self, shift, mousepoint, handled)

Else // rollback

Fmousewheeldown (self, shift, mousepoint, handled );

Fmousewheel (self, shift, hiword (msg. wparam), mousepoint, handled );

If handled then exit;

End;

Inherited;

End;

In this way, we can basically handle the mouse wheel event, compile our components, select the menu components-inatll component, and install our components on the samples page.

Let's test our components, create an application, and select the mydbgrid component under samples. Other usage is exactly the same as dbgrrid, but you can see that, in your mydbgrid event, the onmousewheel, onmousewheelup, and onmousewheeldown options are available.

In this way, you can process your actions in onmousewheel, onmousewheelup, and onmousewheeldown. We have also completed the customer's requirements. When the customer moves the mouse wheel, we add the corresponding code to onmousewheelup, successfully solved the customer's requirements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.