我正在尝试将光标动态更改为半透明的矩形。我已经解决了所有问题,只是透明度的颜色太淡了。这几乎就像我需要提高饱和度。我想要一种半透明的高强度黄色。如果我将 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;
}