4

When I set the imageindex and images property of a Button (from a imagelist component/pngs), start the program and click the button, the image is blinking slowly/ fading in and out. How to prevent this and what seems to be the problem?

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Can you show the .dfm and form code? When you say "set the imageindex and images property", what does that mean exactly (in the Object Inspector, or in code)? I've never seen anything like this, and can't reproduce it in a quick test with either D2010 or XE. – Ken White Jul 01 '11 at 23:52
  • I choose one image from imeagelist to be on a button. Button is on a pagecontrol on tab1. When I run the app the image fades in and out. –  Jul 02 '11 at 00:45
  • Again, **please post your code and dfm**. Without it, you're giving nothing to work from, and your question probably will end up being closed. Saying "I choose one image" doesn't help. **How** do you **choose** it? Post your **code**. – Ken White Jul 02 '11 at 01:31
  • I assign a png image from imagelist to be on a tbutton. Excuse my English. storm below has the same problem. –  Jul 02 '11 at 15:49
  • So this is on vista or win7 and the aero glass theme is enabled? – Warren P Jul 03 '11 at 23:29
  • Win 7, aero enabled. Answer updated. –  Jul 04 '11 at 03:41
  • For an example of this behaviour, see http://privat.rejbrand.se/buttoniconflashing.mp4 – Andreas Rejbrand Sep 17 '14 at 01:04

2 Answers2

9

Reviving an old topic...

After searching for a solution on internet and found nothing, I took a look in the TCustomButton code.

It happens that, internaly, a button control on Windows has an imagelist with 6 images, as follows:

index 0: normal image
index 1: hot image (when mouse is moving over button)
index 2: pressed image (while you hold the mouse button d own)
index 3: disabled image
index 4: selected image (when the button has focus, but is not pressed nor with mouse over it)
index 5: (the one that we need and can't be specified in TButton control; we'll talk about it)

In the TButton control in Delphi, you can set an ImageList to the "Images" property, and you can set "ImageIndex", "HotImageIndex", "PressedImageIndex", "DisabledImageIndex" and "SelectedImageIndex".

With this properties set, the TButton control creates ANOTHER image list, and copy the indexes you specified in the properties from the image list in the "Images" property to that new created image list, in the order I specified above.

The problem is, when you focus the control, Win 7 Aero has that effect that it fades in and out the highlight color (a little animation), and it used the 6th image from it's internal image list to fade in and out to also, but it is IMPOSSIBLE to supply that "FADE" image index to TButton control, so I have created a simple solution that is working for myself, but I have to call in RunTime. (you could derive a new class from TCustomButton and create a new control that you can set a new SelectedFadeImageIndex for example, but I didnt).

I created this procedure:

    procedure MakeButtonImageStopBlinking(AButton: TCustomButton);
    var
      ButtonImageList: TButtonImageList;
      Icon: HICON;
    begin
      SendMessage(AButton.Handle, BCM_GETIMAGELIST, 0, LPARAM(@ButtonImageList));
      Icon := ImageList_GetIcon(ButtonImageList.himl, 0, ILD_NORMAL);
      ImageList_AddIcon(ButtonImageLIst.himl, Icon);
      DestroyIcon(Icon);
    end;


so, when the window is created (on OnCreate event), i just call MakeButtonImageStopBlinking suppling each button that has image as it's parameter, and it all now works.

Sry for revving such an old topic, but it seems to be no answer for that anyware (or I wasn't able to search properly).

Edit: Setting DoubleBufferd to True will work, but it will stop the little animation from the button with focus. With the solution above, you can leave DoubleBuffered to False and you'll get it all (animation from aero and no fading out image).

Marcio
  • 153
  • 2
  • 5
3

It appears to be a doubleBuffered property of a Tbutton. When set to false, the image blinks, when set to true it's working. This occurs on Win 7 with aero enabled.