0

I'm trying to handle OnMouseMove in Delphi (XE3) and even when I don't move the mouse I get endless stream of OnMouseMove events, one after another.

Ex.: start new VCL Forms Application. Add OnMouseMove handler to the form with this code:

var s: string;
begin
  s := IntToStr(GetTickCount()) + ': MouseMove';
  OutputDebugString(PChar(s));
end;

Place the mouse over the form, observe endless OnMouseMoves in IDE's Messages log.

I would expect MouseMove messages to only arrive when I actually move the cursor (and maybe at some other exceptional situations). In fact, i've always though OnMouseMove worked this way.

Am I doing something wrong? Is this correct from a Delphi standpoint (i.e. by design)? Is this correct by WinAPI? What to do about it?

himself
  • 4,806
  • 2
  • 27
  • 43

2 Answers2

5

That's just an interaction with the Delphi debugger's handling of OutputDebugString. But your diagnostic does not reflect how OnMouseMove messages actually arrive in a real program.

For example, consider this variant of your program:

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; 
  X, Y: Integer);
begin
  Caption := IntToStr(GetTickCount()) + ': MouseMove';
end;

You will notice that the form's caption only changes when you move the mouse.

Or take your program, and use a different debug string monitor. For example, DbgView from SysInternals. With that external debug string monitor you find that debug messages only arrive when the mouse really moves.

I'm not sure what the Delphi debugger is doing that leads to the behaviour that you observe. However, be assured that OnMouseMove messages in your actual program will only arrive if the mouse really is moving.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

I can add a tip, OnMouseMove events arrive endlessly also when you show a Hint over the control.

I discovered the hard way: I was trying to show a hint when mouse moves over each element of a ListBox showing some 'extra' information about that element.

As soon as i call HintWindow.ActivateHint(...,HintWindow.Hint); the OnMouseMove is set to be triggered.

Since the first thing io do inside the OnMouseMove is to free and recreate the HintWindow, the Hint flickers a lot because OnMouseMove events arrive endlessly.

If i set OnMouseMove to Nil prior to .ActivateHint, the flicker ends and no more OnMouseMove events arrive.

Now i am trying to 'save' the event into a variable and restore after .ActivateHint, but i am not sure how to do it and what effect will have ... saving OnMouseLeave is easy, but OnMouseMove raises a compiler error Parameter List Differs.

Laura
  • 1
  • Good tip, but afaict the question isnt answered. Consider putting this "answer" in the commnt section instead. – H.Hasenack May 21 '22 at 20:04