【问题标题】:Clicking a control and causing it to shade单击控件并使其着色
【发布时间】:2009-05-29 18:00:39
【问题描述】:

在 Windows 中,当您单击桌面上的图标时,该图标会变暗,并带有基于您当前使用的 Windows 主题的阴影。

我有一个显示图像的自定义控件。我希望具有与单击 Windows 图标相同的功能。如何通过选择我的自定义控件在 WinForms 中获得相同的结果?

【问题讨论】:

    标签: c# .net winforms windows-themes


    【解决方案1】:

    自 Windows XP 以来,Windows 对选定图标实施了 alpha 混合。如果您想获得相似的外观,您必须使用 alpha 混合绘制图像:

    public static void DrawBlendImage(Graphics canvas, Image source, Color blendColor, float blendLevel, int x, int y)
    {
      Rectangle SourceBounds = new Rectangle(x, y, source.Width, source.Height);
    
      ColorMatrix MaskMatrix = new ColorMatrix();
      MaskMatrix.Matrix00 = 0f;
      MaskMatrix.Matrix11 = 0f;
      MaskMatrix.Matrix22 = 0f;
      MaskMatrix.Matrix40 = (float)blendColor.R / byte.MaxValue;
      MaskMatrix.Matrix41 = (float)blendColor.G / byte.MaxValue;
      MaskMatrix.Matrix42 = (float)blendColor.B / byte.MaxValue;
      ImageAttributes MaskAttributes = new ImageAttributes();
      MaskAttributes.SetColorMatrix(MaskMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    
      ColorMatrix TransparentMatrix = new ColorMatrix();
      TransparentMatrix.Matrix33 = blendLevel;
      ImageAttributes TransparentAttributes = new ImageAttributes();
      TransparentAttributes.SetColorMatrix(TransparentMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    
      canvas.DrawImage(source, SourceBounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, MaskAttributes);
      canvas.DrawImage(source, SourceBounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, TransparentAttributes);
    }
    

    在您的情况下,您可以使用 SystemColors.Highlight 作为 blendColor。

    【讨论】:

    • 还没有机会尝试这个(目前正在处理代码的不同区域)。我会在测试后接受答案。感谢您的帮助!
    • 我玩过这段代码...我没有看到示例中如何使用 KnownColor.Highlight。
    • @Billy,小错误,我的意思是 SystemColors.Highlight 不是 KnownColor.Hightlight。
    【解决方案2】:

    您可以使用System.Drawing.KnownColor 为用户的主题获取正确的颜色。

    【讨论】:

      【解决方案3】:

      现在我正在使用以下代码...如果有人有更好的东西,我会很高兴改变它!

        private void drawAndShadeTheImage(Graphics g)
        {
           //if the image is null then there is nothing to do.
           if (Image != null)
           {
              Bitmap bitMap = new Bitmap(Image);
      
              //if this control is selected, shade the image to allow the user to
              //visual identify what is selected.
              if (ContainsFocus)
              {               
                 //The delta is the percentage of change in color shading of 
                 //the image.
                 int delta = 70;
      
                 //zero is the lowest value (0 - 255) that can be represented by
                 //a color component.
                 int zero = 0;
      
                 //Get each pixel in the image and shade it.
                 for (int y = 0; y < bitMap.Height; y++)
                 {
                    for (int x = 0; x < bitMap.Width; x++)
                    {
                       Color oColor = bitMap.GetPixel(x, y);
      
                       //Lime is the background color on the image and should
                       //always be transparent, if this check is removed the
                       //background will be displayed.
                       if (oColor.ToArgb() != Color.Lime.ToArgb())
                       {
                          int oR = oColor.R - delta < zero ? zero : 
                             oColor.R - delta;
                          int oG = oColor.G - delta < zero ? zero : 
                             oColor.G - delta;
                          int oB = oColor.B - delta < zero ? zero : 
                             oColor.B - delta;
                          int oA = oColor.A - delta < zero ? zero : 
                             oColor.A - delta;
      
                          Color nColor = Color.FromArgb(oA, oR, oG, oB);
      
                          bitMap.SetPixel(x, y, nColor);
                       }
                    }
                 }
              }
      
              //Make the background of the image transparent.
              bitMap.MakeTransparent();
      
              g.DrawImage(bitMap, this.ClientRectangle);            
           }
        }
      

      【讨论】:

        猜你喜欢
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        • 2014-12-20
        • 2014-02-28
        • 1970-01-01
        • 2011-03-10
        • 2018-09-14
        • 2011-04-27
        相关资源
        最近更新 更多