创建鼠标指针图标

using System.Runtime.InteropServices;
private struct IconInfo
{
    public bool fIcon;
    public Int32 xHotspot;
    public Int32 yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;        
}

[DllImport("user32.dll")]
private static extern IntPtr CreateIconIndirect(ref IconInfo iconInfo);

public Cursor RectCursor(Color AForeColor, Color ATransparent, int ASize)
{
    if (ASize <= 0) return null;
    Bitmap vBitmap = new Bitmap(32, 32,
        System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    Graphics vGraphics = Graphics.FromImage(vBitmap);
    vGraphics.FillRectangle(new SolidBrush(ATransparent), 0, 0, 32, 32);
    vGraphics.FillRectangle(new SolidBrush(AForeColor), 0, 0, ASize, ASize);
    vGraphics.Dispose();
    vBitmap.MakeTransparent(ATransparent);

    IconInfo vIconInfo = new IconInfo();
    vIconInfo.fIcon = false;
    vIconInfo.xHotspot = ASize / 2;
    vIconInfo.yHotspot = ASize / 2;
    vIconInfo.hbmMask = vBitmap.GetHbitmap();
    vIconInfo.hbmColor = vBitmap.GetHbitmap();
    IntPtr vHandle = CreateIconIndirect(ref vIconInfo);
    return new Cursor(vHandle);
}

private void button1_Click(object sender, EventArgs e)
{
    Cursor = RectCursor(Color.Red, Color.Black, 16);
}
posted @ 2009-04-16 11:40  XiaoK  阅读(484)  评论(0编辑  收藏  举报