8

I'm porting a Delphi 5 app to D2010, and I've got a bit of a problem. On one form is a TImage component with an OnMouseMove event that's supposed to update a label whenever the mouse is moved over the image. This worked just fine in the original app, but now the OnMouseMove event fires constantly whenever the mouse is over the image, whether it's moving or not, which causes the label to flicker horribly.

Does anyone know what's causing this and how to fix it?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 4
    This might be related to http://blogs.msdn.com/oldnewthing/archive/2003/10/01/55108.aspx and http://blogs.msdn.com/oldnewthing/archive/2009/06/17/9763416.aspx – Joey Dec 31 '09 at 19:56
  • Interesting stuff, but I don't have any "mouse enhancement" programs running. – Mason Wheeler Dec 31 '09 at 20:31
  • Well, apparently some mouse drivers may cause this too. But it was just a guess what *may* apply. I have no clue as for the real reason, though. – Joey Dec 31 '09 at 20:45
  • @Mason, I've performed similar to Ken's test on my Vista machine with Delphi 2010 (no updates) and it works as expected. In other words - I couldn't recreate behaviour which you described. – Wodzu Dec 31 '09 at 20:45
  • If you're getting OnMouseMove events, then it's because the OS is sending wm_MouseMove messages. It's nothing to do with your program. Johannes's second link includes more information in the comments about culprits people have found. Keep digging, and good luck. – Rob Kennedy Dec 31 '09 at 20:49
  • So then maybe it's my mouse driver? Figures... – Mason Wheeler Dec 31 '09 at 20:54
  • I have seen this exact problem, and I am watching this issue to see what you find. Create new Delphi 2010 app, on Vista, create Image, set Picture property (load a 16x16 bmp), add MouseMove event handler to the Image1 component, in event handler outputDebugString(IntToStr(FX)); Inc(FX); and declare field FX:Integer in the private section of the class. Run app, and I get endless mouse move messages. – Warren P Jan 04 '10 at 16:24
  • A running Task Manager can cause this: http://debugandconquer.blogspot.com/2015/08/the-cause-of-spurious-mouse-move.html Note the conclusion though... – Philippe Renon Feb 03 '20 at 16:04

3 Answers3

6

My psychic debugging sense tells me that you are on Windows, the label is a tooltip window and you are updating on every mousemove.

In all seriousness, I've seen this exact thing with tooltip window when we switched to Vista. It seems that more recent versions of the Windows tooltip window somehow generate WM_MOUSEMOVE messages when you update them. The only fix I could find was to only update the label when the text actually changes.

So, If you aren't on windows, Ignore me. But if you are on Windows, try updating the label text only when it actually changes.

John Knoeller
  • 33,512
  • 4
  • 61
  • 92
  • Very lame psychic debugging, as Delphi 2010 is Win32-only. :-) – Ken White Dec 31 '09 at 20:23
  • Should have included: and TLabel is a standard Windows label control and not a tooltip window. – Ken White Dec 31 '09 at 20:29
  • Ah, that sort of kills it then. must be something else. Should I delete this comment? – John Knoeller Dec 31 '09 at 20:32
  • Nah, don't delete it. It might be useful to someone else who runs across it. Just couldn't pass up the chance. :-) You'll notice I made sure to include the grin and smile, though. – Ken White Dec 31 '09 at 21:00
  • 2
    @John : Thanks for not deleting it. I googled "Windows 7 mouse move repeatedly" and this was the first result. I'm programming in .net and I recently had this very problem under Windows 7 and it was solved by not refreshing the tooltip if the text had not changed. – Mathieu Pagé Feb 02 '10 at 16:56
5

Since I couldn't add a comment I'm using the answer section to confirm this behavior change. I have a project that was developed in Delphi 2007 where the OnMouseMove event is only called when the mouse position changes. I found that with XE OnMouseMove is constantly being called for the same code. I don't know why since they're both triggered by WM_MOUSEMOVE.

What I am doing till I get to the bottom of this is to compare the previous XY coordinates and exit if no change:

if ( x = ZoomRect.Right ) and ( y = ZoomRect.Bottom ) then exit ;
kobik
  • 21,001
  • 4
  • 61
  • 121
Mitch
  • 335
  • 1
  • 3
  • 10
  • The difference is that in XE the VCL creates a number of classes such as TScrollbarStyleHook with WMMouseMove methods that I assume are there to support Touch and Gestures which were added in 2010(?). I'm not sure what can be done about it. – Mitch Mar 13 '12 at 21:31
1

Mason, I can't reproduce this is a new D2010 (Update 4 & 5) VCL Forms application on Windows XP SP2. Here's what I did:

  • File|New|VCL Forms Application
  • Dropped a TImage and TLabel on the form
  • Picked a random image out of the default images folder (GreenBar.bmp) for the TImage.Picture
  • Double-clicked the TImage.OnMouseMove event in the Object Inspector, and added the following code:
    procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      Label1.Caption := Format('X: %d Y: %d', [X, Y]);
    end;
  • Ran the application (F9).

The label showed "Label1" (the default caption, of course) until I first moved the mouse over the image. It then updated correctly to show the X and Y coordinates. As soon as I moved the mouse pointer out of the image, the label stopped updating.

It appears to be something in your specific code, or something specific to the version of Windows you're using, and not Delphi 2010 itself.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • This is in Vista, in case it helps. – Mason Wheeler Dec 31 '09 at 20:30
  • @Ken I think that in Mason's example mouse position is updated even when he is not moving it. Therefore, you should test it also by for example incrementing some variable each time the event is fired up cause on a fast graphic card flickerking might be not visible for the same caption values. – Wodzu Dec 31 '09 at 20:44
  • @Wodzu: That was the purpose of using the mouse position (X and Y, passed as parameters to the event handler) and Format(). It takes time to call the function and update the label, and it's done rapidly as the mouse moves quickly across the image. Moving off the image quickly as well, the label stops updating. – Ken White Dec 31 '09 at 20:58
  • @Mason: I don't have a Vista machine here, and don't have D2010 installed on one any longer (upgraded to Win7 on that machine). – Ken White Dec 31 '09 at 21:01
  • @Ken yes, but what if the mouse is NOT moving and still the event is beeing fired? On a quick machine you may not notice the refresh of the label and the caption will stay the same. – Wodzu Jan 01 '10 at 20:51
  • It happens here, and is very easy to reproduce, in a very small demo app I created in about 10 seconds flat. If anyone wants it, I will upload the demo. – Warren P Jan 04 '10 at 16:54
  • Warren, can you post the code you used, and the exact steps to produce the problem? – Ken White Jan 04 '10 at 20:50