发光按钮-一个发光按钮控制

screen.png 只是一个匆匆的…… 最近,我偶然发现了KMP播放器软件,并查看了一些图形元素(如果你想看到一些真正好的图形工作,我建议你去看看)。我在一个项目中需要的元素之一是一个简单的发光按钮控件,用于播放器控件,没有太花哨的东西,只有一个图像,当鼠标悬停时,它会改变颜色和发光。 获得一幅改变颜色的图像是相当琐碎的,它是通过修改ImageAttribute的颜色矩阵来完成的: 隐藏,复制Code

private void DrawColoredImage(Graphics g, Image img, Rectangle bounds, Color clr)
{
     using (ImageAttributes ia = new ImageAttributes())
     {
         ColorMatrix cm = new ColorMatrix();
         // convert and refactor color palette
         cm.Matrix00 = ParseColor(clr.R);
         cm.Matrix11 = ParseColor(clr.G);
         cm.Matrix22 = ParseColor(clr.B);
         cm.Matrix33 = ParseColor(clr.A);
         cm.Matrix44 = 1f;
         // set matrix
         ia.SetColorMatrix(cm);
         // draw
         g.DrawImage(img, bounds, 0, 0, img.Width, 
                     img.Height, GraphicsUnit.Pixel, ia);
     }
}

示例代码中的ParseColor只是将字节转换为浮点值。 glow.jpg 获取辉光效果也可以使用ImageAttributes类完成。图像首先膨胀,然后改变调色板和Alpha值,然后在上面绘制干净的图像。所有这一切都是通过首先绘制控制元素到一个缓冲区位图: 隐藏,复制Code

private void DrawButton()
{
    if (this.Image == null)
        return;

    Rectangle bounds = new Rectangle(0, 0, this.Width, this.Height);
    Rectangle imageBounds = GetImageBounds(bounds, this.Image);

    // draw into a buffer
    using (Graphics g = Graphics.FromImage(_bmpBuffer))
    {
        ...
    }
    // draw the buffer
    using (Graphics g = Graphics.FromHwnd(this.Handle))
        DrawImage(g, _bmpBuffer, bounds);
}

这个控件带有大部分暴露的属性,比如各种颜色状态,以及可选的焦点遮罩和边框。下面是属性列表: 检查:复选框状态CheckedBorderColor:检查边框颜色CheckStyle:复选框风格FocusedMask:画一个集中面具ImageDisabledColor:图像颜色ImageFocusedColor:禁用图像集中颜色ImageGlowColor:边境发光颜色ImageHoverColor:图像盘旋颜色ImagePressedColor:图像按颜色ImageGlowFactor:发光因素深度FocusOnHover:关注按钮悬停 历史 2010年2月24日:最初的post27年3月27日:文章截图和源代码 本文转载于:http://www.diyabc.com/frontweb/news516.html

原文地址:https://www.cnblogs.com/Dincat/p/13450934.html