使用C#矩形作为具有透明度的光标

使用C#矩形作为具有透明度的光标,c#,bitmap,cursor,transparent,C#,Bitmap,Cursor,Transparent,我试图动态地将光标更改为半透明的矩形。我把一切都安排好了,只是透明度太浅了。我几乎需要提高饱和度。我想要一个高强度的半透明黄色。如果我将matrix33浮点值增加到1.0,它将变得更加不透明。有没有办法做到这一点 这是我目前(几乎)工作的代码 private Cursor RecCursor(int x, int y) { //Cursor CustomCursor = RecCursor(200, 200); Cursor c = null;

我试图动态地将光标更改为半透明的矩形。我把一切都安排好了,只是透明度太浅了。我几乎需要提高饱和度。我想要一个高强度的半透明黄色。如果我将matrix33浮点值增加到1.0,它将变得更加不透明。有没有办法做到这一点

这是我目前(几乎)工作的代码

    private Cursor RecCursor(int x, int y)
    {
        //Cursor CustomCursor = RecCursor(200, 200);

        Cursor c = null;
        try
        {
            var pic = new Bitmap(x, y);
            Graphics gr = Graphics.FromImage(pic);

            // Create pen.
            Pen blackPen = new Pen(Color.Black, 3);

            // Create location and size of rectangle.
            float a = 0.0F;
            float b = 0.0F;
            float width = x;
            float height = y;

            Color tColor = Color.FromArgb(255, 255, 255, 0);
            SolidBrush brush = new SolidBrush(tColor);

            //create a Bitmap the size of the image provided  
            Bitmap bmp = new Bitmap(pic.Width, pic.Height);

            //create a graphics object from the image  
            using (Graphics gfx = Graphics.FromImage(bmp))
            {
                //create a color matrix object  
                ColorMatrix matrix = new ColorMatrix();

                //set the opacity  
                matrix.Matrix33 = 0.5f;

                //create image attributes  
                ImageAttributes attributes = new ImageAttributes();

                //set the color(opacity) of the image  
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);

                gr.FillRectangle(brush, a, b, width, height);
                gr.DrawRectangle(blackPen, a, b, width, height);

                //now draw the image  
                gfx.DrawImage(pic, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, pic.Width, pic.Height, GraphicsUnit.Pixel, attributes);
            }

            IntPtr ptr = bmp.GetHicon();

            Bitmap bitonalBMP = new Bitmap((int)width, (int)height, PixelFormat.Format1bppIndexed);
            //This changes the hotspot of the cursor
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.hbmColor = bmp.GetHbitmap();
            tmp.hbmMask = bitonalBMP.GetHbitmap();
            tmp.xHotspot = 0;
            tmp.yHotspot = 0;
            tmp.fIcon = false;
            ptr = CreateIconIndirect(ref tmp);

            c = new Cursor(ptr);

            //if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor);
            //if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask);
            //if (ptr != IntPtr.Zero) DestroyIcon(ptr);

            pic.Dispose();
            gr.Dispose();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        return c;
    }

我不明白你为什么需要矩阵X33——你能不能把你的黄色设置成像Color tColor=Color.FromArgb(128,255,255,0)?这将使其变为黄色,透明度为0.5。颜色矩阵矩阵33控制不透明度。所以0.5f是50%透明的。它与您的颜色相同。来自argb示例。我用了两种方法,它们都得到了相同的输出,即半透明矩形。你只能看到一半,所以不像我想的那么清楚。我想让它看起来像你面前有一块完美的黄色玻璃。您可以完全看穿它,但它是亮黄色,而不是您的示例和我的示例提供的半透明淡黄色。非常有用,但我认为您需要在调用
GetIconInfo
后立即使用
destroicon(ptr)