Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绘制自定义十字光标仅绘制光标的一半_C#_Wpf_Cursor - Fatal编程技术网

C# 绘制自定义十字光标仅绘制光标的一半

C# 绘制自定义十字光标仅绘制光标的一半,c#,wpf,cursor,C#,Wpf,Cursor,我正在尝试绘制自定义十字光标 这是我创建“十字光标”的函数 问题在于它只绘制正坐标。 所以我得到了十字架的一半。 所以当我画十字架时,十字架上的红色是看不见的。 一个解决方案是使十字都具有正坐标,但我不希望这样。 有什么解决办法吗 private Cursor crossCursor(System.Drawing.Pen pen, int x, int y) { var pic = new Bitmap(x, y); var gr = Graphics.FromImage(pi

我正在尝试绘制自定义十字光标

这是我创建“十字光标”的函数

问题在于它只绘制正坐标。 所以我得到了十字架的一半。

所以当我画十字架时,十字架上的红色是看不见的。 一个解决方案是使十字都具有正坐标,但我不希望这样。

有什么解决办法吗

private Cursor crossCursor(System.Drawing.Pen pen, int x, int y)
{
    var pic = new Bitmap(x, y);
    var gr = Graphics.FromImage(pic);

    //gr.Clip = new Region(new RectangleF(-x / 2, -y / 2, x, y));

    var pathX = new GraphicsPath();
    var pathY = new GraphicsPath();
    pathY.AddLine(new Point(0, -y), new Point(0, y));
    pathX.AddLine(new Point(-x, 0), new Point(x, 0));
    gr.DrawPath(pen, pathX);
    gr.DrawPath(pen, pathY);

    var stream = new MemoryStream();
    Icon.FromHandle(pic.GetHicon()).Save(stream);

    // Convert saved file into .cur format
    stream.Seek(2, SeekOrigin.Begin);
    stream.WriteByte(2);
    stream.Seek(10, SeekOrigin.Begin);
    stream.Seek(0, SeekOrigin.Begin);

    // Construct Cursor
    return new Cursor(stream);
}

如果参数
x
y
各为10,则可以在代码中创建一个10x10的位图

所以这一行:

pathY.AddLine(new Point(0, -y), new Point(0, y));
翻译为:

pathY.AddLine(new Point(0, -10), new Point(0, 10));
点(0,-10)位于位图之外,因此不可见


如果坚持使用负值,则无法在位图内部绘制并显示所有内容…

为什么不能使用正坐标?如果使用正坐标,问题是光标的实际命中点将位于十字的左上方,而不是中间。命中点与绘图代码无关,或者,如果我画了十字,那么问题就解决了。你有没有其他建议来解决我的生命点问题?我最初的代码是-y/2和y/2,所以应该是相同的。这并不能解决问题如果我使用正坐标,问题是我的光标的实际命中点应该在十字的左上角,而不是中间。你可以在.NET中更改自定义光标的热点。看见