0

我有一个 WinForms 应用程序,其主要组件是通知区域中的一个图标。我注意到,如果我拖动此图标(对其重新排序,或将其移入/移出 Windows 隐藏的图标列表),则与其他图标不同,它的透明像素不会得到正确尊重。

这在下面的动画中进行了说明;拖动时其他图标看起来不错,但我的图标(红色圆圈)却没有(请原谅动画的压缩伪影)。

拖动图标

仔细观察,通常看起来像这样的图标:

恒温器图标

拖动时是这样的:

恒温器图标被拖动

使用一个NotifyIcon控件,动态生成不同颜色的图标,覆盖不同的数字。

为了保持图标边缘的半透明性,使用 PNG 格式(使用来自CodeProject的代码示例)获取 aBitmap并返回Icon由 使用的NotifyIcon

private static readonly byte[] _pngIconHeader = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

using (var bmp = new Bitmap(image, new Size(image.Width, image.Height)))
{
    byte[] png;
    using (var ms = new MemoryStream())
    {
        bmp.Save(ms, ImageFormat.Png);
        ms.Position = 0;
        png = ms.ToArray();
    }

    using (var ms = new MemoryStream())
    {
        _pngIconHeader[6] = (byte)image.Width;
        _pngIconHeader[7] = (byte)image.Height;
        _pngIconHeader[14] = (byte)(png.Length & 255);
        _pngIconHeader[15] = (byte)(png.Length / 256);
        _pngIconHeader[18] = (byte)(_pngIconHeader.Length);

        ms.Write(_pngIconHeader, 0, _pngIconHeader.Length);
        ms.Write(png, 0, png.Length);
        ms.Position = 0;

        return new Icon(ms);
    }
}

是否需要做更多工作以确保 Windows 在拖动时正确处理它?

4

0 回答 0