点击控制,并使其树荫 [英] Clicking a control and causing it to shade

查看:141
本文介绍了点击控制,并使其树荫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在窗口,当你点击桌面上的图标,与基于当前所用的Windows主题阴影的图标变暗。

In windows when you click on an icon on your desktop, the icon darkens with a shade that is based on your windows theme that is currently used.

我有一个自定义的控件来显示图像。我想有相同的功能的Windows图标点击。我该如何选择我的自定义控件获得的WinForms同样的结果?

I have a custom control that displays an image. I would like to have the same functionality as the windows icon click. How do I obtain the same result in WinForms by selecting my custom control?

推荐答案

现在,我用下面的code ......如果任何人有更好的东西,我会很高兴去改变它!

Right now I'm using the following code... if anyone has something better, I'll be glad to change it!

  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);            
     }
  }

这篇关于点击控制,并使其树荫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆