views:

490

answers:

2

Hi

How can I display a preview (almost like a hint) of an image when I hover the mouse over an item in a listbox of filenames? I've tried showing a form and loading the image, but when the preview form shows, I lose focus for the listbox which means that when I move the mouse, the preview image does not change when I go to the next item in the list.

Thanks, Pieter.


I have, based on the answer from TOndrej, tried to implement a custom THintWindow, but the Canvas.StretchDraw does not draw the bitmap sent as a parameter. Any ideas why not? Text is displayed normally.

procedure TFormMain.DisplayPreview(HintImage: TBitmap);
var
  CustomHint: THintWindow;
  Rect: TRect;
  MousePoint: TPoint;
begin
  *{
    Based on Source: http://www.chami.com/tips/delphi/112996D.html
  }*
  GetCursorPos(MousePoint);
  with Rect do
    begin
      // set the position and size of the hint window
      Left   := MousePoint.X;
      Top    := MousePoint.Y;
      Right  := Left + 50;
      Bottom := Top + 25;
    end;

  CustomHint := THintWindow.Create(Self);
  try
    with CustomHint do
      begin
        // set the background color
        //Color := clNone;
        **Canvas.StretchDraw(Rect, HintImage);**
        ActivateHint(Rect, 'Hint');
        Application.ProcessMessages;
        //
        // perform your tasks here
        // before closing the hint window
        //
        Sleep(500);
        ReleaseHandle;
      end;

  finally
    if Assigned(CustomHint) then
      CustomHint.Free;
  end;
end;
+1  A: 

Hi,
1) Are you displaying your preview form like a dialog (Modal Window) if yes then change it to non modal window.
2) Remember to set focus back to your parent window once the preview form shows up, that way your parent form that has the listbox has the focus and it will pass the mouse move events to the listbox.

Best Regards.

Anand
A: 

To me it seems you want a custom hint window. To do this you should write a new THintWindow descendant and either set it globally to the whole application by assigning your new class to the HintWindowClass global variable in Forms unit, or write your own listbox descendant in which you will handle CM_HINTSHOW message and assign your new hint window class to HintInfo.HintWindowClass. (HintInfo points to a record passed to your control in the CM_HINTSHOW message by the VCL.)

TOndrej