Page 1 of 1

Button and disabled icon

Posted: 12 Sep 2012, 07:47
by Mex
HI again,

If a use icon with commandbutton and set the enabled property to false then icon with index 3 is used from the image list?
So I must create disabled "gray" icon my self and add it to the image list with index 3?


Regards,
Meelis

Re: Button and disabled icon

Posted: 12 Sep 2012, 21:18
by TiKu
Hi,

yes, this is correct.

Regards
TiKu

Re: Button and disabled icon

Posted: 10 Mar 2017, 13:29
by Cube8
I don't get it.
The definition is CommandButton.IconIndex([controlState As Long = -1]) As Long.

I thought it goes like this:
  • There is a single hImageList for the control.
  • IconIndex (without arguments, controlState = -1) uses the index provided as the default control icon.
  • IconIndex(3) means that it will use the index provided as the disabled icon.
Am I missing something? I tried setting IconIndex(3) = someindex but the icon doesn't change when I set .Enabled = False.

Re: Button and disabled icon

Posted: 10 Mar 2017, 20:36
by TiKu
Are you setting the UseImprovedImageListSupport property to True? I had to implement it this way to not break existing applications.

Re: Button and disabled icon

Posted: 13 Mar 2017, 08:50
by Cube8
Yes.
The icon changes if I change IconIndex for the default controlState. It doesn't work for controlState = 3 and Enabled = False. Instead, it shows the default icon a bit fainted.

Here is a sample, to make it clearer:

Code: Select all

With CommandButton1
    .hImageList = <some image list handle>
    .IconIndex = <default icon>
    .IconIndex(3) = <disabled icon>  'This is ignored
End With
Omitting the .IconIndex = <default icon> statement will make the button use the icon at index 0.

Re: Button and disabled icon

Posted: 13 Mar 2017, 08:59
by TiKu
Try this code:

Code: Select all

With CommandButton1
    .UseImprovedImageListSupport = True
    .hImageList = <some image list handle>
    .IconIndex = <default icon>
    .IconIndex(3) = <disabled icon>
End With
This way it works for me (Windows 7 SP1)

Re: Button and disabled icon

Posted: 13 Mar 2017, 15:15
by Cube8
Hmm. This is very interesting.
I was working with classic theme. After switching to aero, it worked as expected.
This is definitely a bug, although I don't know if it is your code or windows fault.
Also, while on classic theme, I noticed that, if a button has icon+text and Enabled = True, the icon is "almost" hidden:
enabled-disabled.png
(1.27 KiB) Not downloaded yet

Re: Button and disabled icon

Posted: 13 Mar 2017, 20:24
by TiKu
Interesting. You are right. I never noticed this. I'll try to find out whether anything can be done about it.

Re: Button and disabled icon

Posted: 13 Mar 2017, 23:21
by TiKu
The problem is that the native button control does not query for the disabled image if classic theme is used. Instead it always queries for the normal image. It would be quite difficult to work around this behavior in a reliable way.

Re: Button and disabled icon

Posted: 14 Mar 2017, 08:23
by Cube8
Ok. So this is a windows-related bug. I could write a procedure that changes the default icon whenever I want to enable/disable a button, instead of just setting Enabled = True/False. This is an inconvenience but I want to cover the classic theme scenario.

Also, what about the almost-hidden-icon bug? Is there a workaround for this?

Re: Button and disabled icon

Posted: 14 Mar 2017, 08:29
by TiKu
Cube8 wrote: 14 Mar 2017, 08:23 Also, what about the almost-hidden-icon bug? Is there a workaround for this?
So far I could not reproduce this problem. Do you have a sample project? It might have to do with the color of the icon's top-left pixel.

Re: Button and disabled icon

Posted: 16 Mar 2017, 08:55
by Cube8
I found out why I had this problem.
I did a hack, in order to show the icon to the left of the text (not the left edge of the button).
Specifically, I set IconAlignment = 4 (Center), I calculate the IconMarginRight, based on the button's text and then I pad some spaces to the left of the text.
With themes it works fine. Without themes the icon seems almost blank (like in the picture a few posts ago).
Apparently, when using themes, the text is drawn transparent and my hack works as expected, while on classic theme, the text covers the icon entirely.
I don't have WinXP to try it, btw.