Hello,
I have a Ribbon and need to disable/enable tools sometimes. When I disable a tool with icon the icon becomes very dark grey.
I now need to influence this "disabled icon color" (Lighter grey). How can I change the coloring of the icon when disbaled? Or can I change the whole icon when disabled?
Thanks for your help!
Regards, Michael
Hello Michael,
There is no built-in setting to apply a disabled image for when a Tool is disabled. You might be able to achieve this with a DrawFilter but the gray-scaled appearance is considered to be standard behavior.
Let me know if you have any questions regarding this matter.
Hi MIchael,
Thanks for your reply. Is it somehow possible to at least influence the intensity of the grey color to a more lighter grey?
Thanks
MIchael
AppStylist exposes a way to change "Disabled" font color for the text displayed in the tool, but not the image.
Here is a rough example of changing the displayed image via a DrawFilter:
public class DrawFilterforDisabledTool : IUIElementDrawFilter { UIElement mainElement; public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams) { mainElement = drawParams.Element.Parent; Rectangle elementRect = drawParams.Element.Rect; if (mainElement.Parent is ButtonToolUIElement) { UIElement grandParent = mainElement.Parent.GetAncestor(typeof(RibbonGroupUIElement)); if (mainElement.Parent.Parent == grandParent) { UIElement imageTextUI = drawParams.Element.Parent.GetDescendant(typeof(ImageAndTextUIElementEx)); UIElement imageUIElement = drawParams.Element.Parent.GetDescendant(typeof(Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement)); var elementImage = ToolbarTipsnTricks.Properties.Resources.home011; drawParams.Graphics.DrawImage(elementImage, imageUIElement.Rect); } } return true; } public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams) { if (drawParams.Element is Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement) { return DrawPhase.BeforeDrawElement; } else { return DrawPhase.None; } }
public class DrawFilterforDisabledTool : IUIElementDrawFilter { UIElement mainElement;
public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams) { mainElement = drawParams.Element.Parent;
Rectangle elementRect = drawParams.Element.Rect; if (mainElement.Parent is ButtonToolUIElement) { UIElement grandParent = mainElement.Parent.GetAncestor(typeof(RibbonGroupUIElement)); if (mainElement.Parent.Parent == grandParent) { UIElement imageTextUI = drawParams.Element.Parent.GetDescendant(typeof(ImageAndTextUIElementEx));
UIElement imageUIElement = drawParams.Element.Parent.GetDescendant(typeof(Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement)); var elementImage = ToolbarTipsnTricks.Properties.Resources.home011; drawParams.Graphics.DrawImage(elementImage, imageUIElement.Rect); } } return true; }
public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams) { if (drawParams.Element is Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement) { return DrawPhase.BeforeDrawElement; } else { return DrawPhase.None; } }
Let me know if you have any questions.
Hey Michael,
thanks for the answer. I modified your code a little so that I can darken the used image on my own, pls see code below.
That works fine. Now i wanted to generalize the DrawFilter in a way that it works on all tools used in the ribbon, no matter what tool it is. But I ran into problems with EditorToolBaseUIElement. I can not access EditorToolImageUIElement to use the image property. Any ideas???
Regards
public class DrawFilterforDisabledTool : IUIElementDrawFilter { public bool DrawElement( DrawPhase drawPhase, ref UIElementDrawParams drawParams ) { UIElement mainElement = drawParams.Element.Parent; if(mainElement.Parent is ButtonToolUIElement) { //ButtonToolUIElement UIElement imageUIElement = drawParams.Element.Parent.GetDescendant(typeof(ImageAndTextUIElement.ImageAndTextDependentImageUIElement)); // Turn image into darkend grayscale image var elementImage = (imageUIElement as Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement).Image; var grayImage = MakeGrayscale(elementImage); drawParams.Graphics.DrawImage(grayImage, imageUIElement.Rect); } return true; } public DrawPhase GetPhasesToFilter( ref UIElementDrawParams drawParams ) { if(drawParams.Element is Infragistics.Win.ImageAndTextUIElement.ImageAndTextDependentImageUIElement) { if(drawParams.Element.Enabled == false) { return DrawPhase.BeforeDrawElement; } } return DrawPhase.None; } private Image MakeGrayscale( Image original ) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix(new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {-0.35f, -0.35f, -0.35f, 1, 0}, new float[] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); // Draw original image with grayscale matrix onto newly created graphics context g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; } }
I guess I found a solution on my own :-)
I used ILSpy to find the proper base classes. Is anything wrong with this approach?
Her it is:
public class DrawFilterforDisabledTool : IUIElementDrawFilter { public bool DrawElement( DrawPhase drawPhase, ref UIElementDrawParams drawParams ) { if(!(drawParams.Element.Parent is GalleryPreviewButtonInnerUIElement)) { ImageUIElementBase imgUIElement = drawParams.Element as ImageUIElementBase; Image grayImage = MakeGrayscale(imgUIElement.Image); if(imgUIElement.Rect.Width != imgUIElement.ImageRect.Width || imgUIElement.Rect.Height != imgUIElement.ImageRect.Height) { // Calculate center position Rectangle rect = new Rectangle(imgUIElement.Rect.X + (imgUIElement.Rect.Width - imgUIElement.ImageRect.Width) / 2, imgUIElement.Rect.Y + (imgUIElement.Rect.Height - imgUIElement.ImageRect.Height) / 2, imgUIElement.ImageRect.Width, imgUIElement.ImageRect.Height); drawParams.Graphics.DrawImage(grayImage, rect); } else { // Just draw it drawParams.Graphics.DrawImage(grayImage, imgUIElement.Rect); } return true; } else { return false; } } public DrawPhase GetPhasesToFilter( ref UIElementDrawParams drawParams ) { if(drawParams.Element is ImageUIElementBase) { if(drawParams.Element.Enabled == false) { return DrawPhase.BeforeDrawElement; } } return DrawPhase.None; } private Image MakeGrayscale( Image original ) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix(new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {-0.35f, -0.35f, -0.35f, 1, 0}, new float[] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); // Draw original image with grayscale matrix onto newly created graphics context g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; }}