c# - C# 动态改变光标大小

标签 c# wpf cursor cursors custom-cursor

我创建了一个 .cur 文件,其中包含一个简单的光标填充椭圆。

我希望这个光标像“画笔光标”一样工作,这意味着如果我改变画笔的粗细,光标的大小也会改变(我也会改变)例如更改光标的颜色)。

这是我正在使用的代码:

var customCursor = new Cursor(@"CustomCursor.cur");
Mouse.OverrideCursor = currentCursor;

这样的事情可以做吗?有没有更好的办法?

最佳答案

我以前用过这个,而且很有效。

Cursor CreateCursor(double rx, double ry, SolidColorBrush brush, Pen pen)
{
    var vis = new DrawingVisual();
    using (var dc = vis.RenderOpen())
    {
        dc.DrawRectangle(brush, new Pen(Brushes.Black, 0.1), new Rect(0, 0, rx, ry));
        dc.Close();
    }
    var rtb = new RenderTargetBitmap(64, 64, 96, 96, PixelFormats.Pbgra32);
    rtb.Render(vis);

    using (var ms1 = new MemoryStream())
    {
        var penc = new PngBitmapEncoder();
        penc.Frames.Add(BitmapFrame.Create(rtb));
        penc.Save(ms1);

        var pngBytes = ms1.ToArray();
        var size = pngBytes.GetLength(0);

        //.cur format spec http://en.wikipedia.org/wiki/ICO_(file_format)
        using (var ms = new MemoryStream())
        {
            {//ICONDIR Structure
                ms.Write(BitConverter.GetBytes((Int16)0), 0, 2);//Reserved must be zero; 2 bytes
                ms.Write(BitConverter.GetBytes((Int16)2), 0, 2);//image type 1 = ico 2 = cur; 2 bytes
                ms.Write(BitConverter.GetBytes((Int16)1), 0, 2);//number of images; 2 bytes
            }

            {//ICONDIRENTRY structure
                ms.WriteByte(32); //image width in pixels
                ms.WriteByte(32); //image height in pixels

                ms.WriteByte(0); //Number of Colors in the color palette. Should be 0 if the image doesn't use a color palette
                ms.WriteByte(0); //reserved must be 0

                ms.Write(BitConverter.GetBytes((Int16)(rx / 2.0)), 0, 2);//2 bytes. In CUR format: Specifies the horizontal coordinates of the hotspot in number of pixels from the left.
                ms.Write(BitConverter.GetBytes((Int16)(ry / 2.0)), 0, 2);//2 bytes. In CUR format: Specifies the vertical coordinates of the hotspot in number of pixels from the top.

                ms.Write(BitConverter.GetBytes(size), 0, 4);//Specifies the size of the image's data in bytes
                ms.Write(BitConverter.GetBytes((Int32)22), 0, 4);//Specifies the offset of BMP or PNG data from the beginning of the ICO/CUR file
            }

            ms.Write(pngBytes, 0, size);//write the png data.
            ms.Seek(0, SeekOrigin.Begin);
            return new Cursor(ms);
        }
    }
}

然后,要设置它,您可以调用:

  Mouse.OverrideCursor = CreateCursor(50,50, Brushes.Gold, null);

来源:https://gist.github.com/kip9000/4201899

关于c# - C# 动态改变光标大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35296030/

相关文章:

html - 将鼠标悬停在元素上时光标没有改变

sql - 谁能帮我用我的 pl/sql 脚本更新/合并数据库

mysql - 循环遍历 information_schema mysql 的结果

c# - 无法将类型 System.DirectoryServices.AccountManagement.Principal 隐式转换为字符串

c# - 从 Azure 表中删除数据

c# - 无法将 json 解析为自定义对象列表

c# - 绑定(bind)到两个值

c# - 以编程方式创建 ListView 项的绑定(bind)

c# - 在 SQL 命令上将相等性更改为其他内容

c# - 如何在 Canvas 上旋转动态创建的 ListBoxItem?