views:

598

answers:

2

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?

+3  A: 

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
Very lame psychic debugging, as Delphi 2010 is Win32-only. :-)
Ken White
Should have included: and TLabel is a standard Windows label control and not a tooltip window. <g>
Ken White
Ah, that sort of kills it then. must be something else. Should I delete this comment?
John Knoeller
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
@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é
@Mathieu: so I'm a precog too? how cool is that!
John Knoeller
+1  A: 

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
This is in Vista, in case it helps.
Mason Wheeler
@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
@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
@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
@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
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
Warren, can you post the code you used, and the exact steps to produce the problem?
Ken White