如何使用 DoMouseWheel 一次滚动一行?

发布于 2024-08-10 07:01:33 字数 330 浏览 7 评论 0原文

我已经编写了一个网格控件,并想为其添加对鼠标滚轮的支持。我认为这就像重写 DoMouseWheel 虚拟方法一样简单,但它有一点问题。

您可以在控制面板中设置一次滚动的行数,默认为三行。当滚动浏览文档或网页时,这非常有意义,但在网格上,我认为期望的是一次滚动一行。但似乎 Delphi 的滚轮支持将为我滚动的每个凹口调用 DoMouseWheel 三次,这意味着我只能滚动到网格中的每第三行(或任何全局设置)。

如何让鼠标滚轮每次转动一次滚动一行?

更新:这里的简短答案是在滚动后简单地将 Result 设置为 True - 那么它不会滚动三次,而只会滚动一次。

I've written a grid control and would like to add support for the mouse wheel to it. I thought it would be as simple as overriding the DoMouseWheel virtual method, but there is a bit of a problem with it.

You can set the number of lines to scroll at a time in Control Panel and the default there is three. And this makes perfect sense when scrolling through a document or web page, but on a grid, I think the expectation is rather to scroll a line at a time. But it seems that Delphi's wheel support will call DoMouseWheel three times for every notch that I scroll, meaning that I can only scroll to each third line in the grid (or whatever that global setting is).

How do I go about scrolling a single line at a time for every turn of the mouse wheel?

Update: The short answer here is to simply set Result to True after scrolling - then it doesn't scroll three times, but only once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸩远一方 2024-08-17 07:01:33

只需复制 TCustomGrid 类中的代码即可,该类重写 DoMouseWheelDown()DoMouseWheelUp() 以便一次只滚动一行。

Just copy the code from the TCustomGrid class, which overrides both DoMouseWheelDown() and DoMouseWheelUp() to scroll exactly one line at a time.

热血少△年 2024-08-17 07:01:33

一般来说,反对系统默认值和/或用户偏好并不是一个好主意。在这种情况下意味着您应该尊重系统或用户决定在滚动时间中设置的任何内容。

话虽如此,如果您确实认为多滚动效果是完全错误的,并且对您想要驱动的组件类型具有误导性,那么您可能会想出一种方法来摆脱这种情况。您可以尝试设置一些计时器并忽略在给定时间(毫秒范围内)内发生的除一个 mouseWheel 事件之外的所有事件。您应该做的一件事是在程序中设置一个配置选项,让用户关闭此行为。

In general, it is not a very good idea to fight against the system defaults and/or the user preferences. In this case means that you should respect whatever the system or the user has decided to set in the scrolling time.

Having said so, if you really believe that the multiscroll effect is totally wrong and misleading for the kind of component you want to drive, you might envision a way to get rid of this. You could try to set some timer and ignore all but one of the mouseWheel events that happen in a given lapse of time (in the range of milliseconds). One thing you should do is to set a configuration option in your program, to let the user to turn off this behaviour.

晨与橙与城 2024-08-17 07:01:33

就我而言,我使用了 JVDBGrid 组件,但我认为这也适用于 DbGrid。您可以覆盖以下函数:OnMouseWheelDown 和 OnMouseWheelUp。

例如:

类型声明:

type
  TMyGrid = class(TJvExDBGrid);

实现

procedure TFExample.JvDBGrid1MouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin

  Handled := TMyGrid(Sender).DataLink.DataSet.MoveBy(1) <> 0;

end;

procedure TFExample.JvDBGrid1MouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin

  Handled := TMyGrid(Sender).DataLink.DataSet.MoveBy(-1) <> 0;

end;

In my case, I used the JVDBGrid component, but I think this work for DbGrid too. You may overwrite the following functions: OnMouseWheelDown and OnMouseWheelUp.

E.g.:

Type declaration:

type
  TMyGrid = class(TJvExDBGrid);

Implementation

procedure TFExample.JvDBGrid1MouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin

  Handled := TMyGrid(Sender).DataLink.DataSet.MoveBy(1) <> 0;

end;

procedure TFExample.JvDBGrid1MouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin

  Handled := TMyGrid(Sender).DataLink.DataSet.MoveBy(-1) <> 0;

end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文