Image in ImageList in ToolBar: enable/disable

  • Thread starter Thread starter MaxGruven
  • Start date Start date
M

MaxGruven

How do I get an image to be displayed as grayed out when I place it in a
button, in an ImageList in a ToolBar and the button is disabled?

When the button is enabled, the image looks great.

When the button is disabled, the image does not display.

TIA,
George
 
MaxGruven said:
How do I get an image to be displayed as grayed out when I place it in a
button, in an ImageList in a ToolBar and the button is disabled?

When the button is enabled, the image looks great.

When the button is disabled, the image does not display.

TIA,
George

Hi George,

I'm not sure if I can reproduce your problem. Setting the image property of
a ToolStripButton and setting it to disabled appeared to gray out the image
correctly. Tested with images from disk, resource files and imagelists. I
tested on VS 2005 and VS 2008.

If you are talking about the WPF ToolBar, then I'm afraid I cannot help as
my WPF experience is limited. I do know that project resources are supposed
to take over the use of ImageLists, so you may have better luck using a
resource.
 
I'm sure that that is done automatically (may be a Visual Styles related
problem).

Although saying that, Toolbar was a VS2002/2003 control, in VS2005/2008 we
more commonly use a Toolstrip, although Toolbar is still available. If you
are programming in VS2002/2003 then there were several issues with
ImageLists. The solution to most of the issues was to leave the imagelist
empty at design time, add the images as resources and load them into the
imagelist at runtime.

I had a requirement to draw Disabled images from an imagelist in custom
controls back in VS2003. I'm not sure if it will help in your scenario but
here's a link to my solution:
http://dotnetrix.co.uk/misc.html --> Draw 32 bit images from an Imagelist.
 
Dear George,

Based on my understanding, you're using a ToolBar control(.NET 1.x ), the
ToolBarButton shows an image when it is enabled, but when the ToolBarButton
is disabled, the image is not displayed.

As far as I know this is a by design feature with the old ToolBar
control(.NET 1.x), you can refer to this document:

ToolBarButton.Enable Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolbarbutton.e
nabled.aspx

It says:

[MSDN]
If the image or text has multiple colors, they display in a monochromatic
gray.
[/MSDN]

So, if the image you used is colorful, when a ToolBarButton is disabled, a
monochromatic gray would be displayed on it, resulting the disappear of the
image.

For .NET 2.0 and above, there's a new control named "ToolStrip", which can
show a nice gray image when the ToolStripButton is disabled. If you're
working on Visual Studio 2005/2008, I would recommend you use this
ToolStrip control instead.

However, if you're using Visual Studio 2003, you can try my resolution
below to show a gray image on the ToolBarButton when it is disabled,

Sample code for your information:

public class ToolBarEx : ToolBar
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

if (m.Msg == 0x000F) //WM_PAINT
{
foreach (ToolBarButton b in this.Buttons)
{
if (!b.Enabled)
{
using (Graphics g = this.CreateGraphics())
{
Bitmap bmp =
this.GetGrayImage(this.ImageList.Images[b.ImageIndex]);
g.DrawImage(bmp, b.Rectangle.X + 4,
b.Rectangle.Y + 3);
}
}
}
}
}

private Bitmap GetGrayImage(Image ImgSource)
{
Bitmap bmp = new Bitmap(ImgSource.Width, ImgSource.Height);
Bitmap img = ImgSource as Bitmap;

for (int h = 0; h < bmp.Height; h++)
{
for (int w = 0; w < bmp.Width; w++)
{
Color c = img.GetPixel(w, h);

Color newColor = Color.FromArgb(
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B));

bmp.SetPixel(w, h, newColor);
}
}
return bmp;
}
}


If anything is unclear, please don't hesitate to let me know. I'm glad to
be of assistance.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Yes indeed, it is ToolBar from the 1.1 days and I'm not sure we have the
bandwidth to change to ToolStrip at this point.

What I do notice is that the *.ico images show a grayed out image and the
*.bmp and jpg images just show a light tan box when disabled.

All images are 16 x 16 with a resolution of 96.

I suspect the quickest fix is to convert the bmp's and jpg's to ico's.

Any suggestions on the best way to accomplish this or in fact, this might be
the issue?

Thanks,
George

Zhi-Xin Ye said:
Dear George,

Based on my understanding, you're using a ToolBar control(.NET 1.x ), the
ToolBarButton shows an image when it is enabled, but when the ToolBarButton
is disabled, the image is not displayed.

As far as I know this is a by design feature with the old ToolBar
control(.NET 1.x), you can refer to this document:

ToolBarButton.Enable Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolbarbutton.e
nabled.aspx

It says:

[MSDN]
If the image or text has multiple colors, they display in a monochromatic
gray.
[/MSDN]

So, if the image you used is colorful, when a ToolBarButton is disabled, a
monochromatic gray would be displayed on it, resulting the disappear of the
image.

For .NET 2.0 and above, there's a new control named "ToolStrip", which can
show a nice gray image when the ToolStripButton is disabled. If you're
working on Visual Studio 2005/2008, I would recommend you use this
ToolStrip control instead.

However, if you're using Visual Studio 2003, you can try my resolution
below to show a gray image on the ToolBarButton when it is disabled,

Sample code for your information:

public class ToolBarEx : ToolBar
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

if (m.Msg == 0x000F) //WM_PAINT
{
foreach (ToolBarButton b in this.Buttons)
{
if (!b.Enabled)
{
using (Graphics g = this.CreateGraphics())
{
Bitmap bmp =
this.GetGrayImage(this.ImageList.Images[b.ImageIndex]);
g.DrawImage(bmp, b.Rectangle.X + 4,
b.Rectangle.Y + 3);
}
}
}
}
}

private Bitmap GetGrayImage(Image ImgSource)
{
Bitmap bmp = new Bitmap(ImgSource.Width, ImgSource.Height);
Bitmap img = ImgSource as Bitmap;

for (int h = 0; h < bmp.Height; h++)
{
for (int w = 0; w < bmp.Width; w++)
{
Color c = img.GetPixel(w, h);

Color newColor = Color.FromArgb(
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B),
(int)(0.5 * c.R + 0.3 * c.G + 0.2 * c.B));

bmp.SetPixel(w, h, newColor);
}
}
return bmp;
}
}


If anything is unclear, please don't hesitate to let me know. I'm glad to
be of assistance.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Dear George,

I think it's not the image format that differs the gray effect.
If you have an icon file whose background color is not transparent or
white, for example, the background color is black, then a monochromatic
gray rectangle will still be shown when the ToolBarButton disabled.
On the other hand, if a bmp/jpg/gif/png file has a transparent or white
background, then it would be shown as a nice gray out image when the
ToolBarButton disabled.

So, I would suggest you make the background color of the image files you
used to transparent. You can use picture edit tools to do so.

If you need further help on this, please let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top