문제

Windows에서 데스크탑에서 아이콘을 클릭하면 아이콘이 현재 사용되는 Windows 테마를 기반으로하는 그늘로 어두워집니다.

이미지를 표시하는 사용자 정의 컨트롤이 있습니다. Windows 아이콘 클릭과 동일한 기능을 갖고 싶습니다. 사용자 정의 컨트롤을 선택하여 Winforms에서 동일한 결과를 얻는 방법은 무엇입니까?

도움이 되었습니까?

해결책 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);            
     }
  }

다른 팁

Windows는 Windows XP 이후 선택된 아이콘에 대한 알파 블렌딩을 구현합니다. 비슷한 모양을 달성하려면 알파 블렌딩으로 이미지를 그려야합니다.

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로.

당신이 사용할 수있는 System.Drawing.KnownColor 사용자 테마에 적절한 색상을 얻으려면

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top