Wpf中鼠标样式的修改,作用点修改

时间:2022-03-28 07:03:07

可是,所有的改完以后,调试运行,结果发现自己制作的图标的作用点总是在左上角,而不是在“手形”图标的食指上。。

所以,问题来了,怎么修改鼠标样式的作用点呢?百度半天无果,自己琢磨半天,cursor这个东东没有什么可以使用的其它属性。

后来,在逛*的时候,偶然发现了一个提问,找到点有用的东西:

原地址:

感谢万能的*,顺便吐槽一下百度。

其实文章中我找到的是一个词:hotspot,即“热点”。阅读代码可以发现这就是我们要的。

文中提出了制作鼠标样式的方法,这些方法都好说,我们关注的是修改作用点,观察文中提到的方法,都是通过向stream中写字节的方法实现,个人觉得比较麻烦且费时。于是想,大家改stream,那么是不是.cur中可以直接对其进行设置呢

public Cursor ConvertToCursor(FrameworkElement visual, Point hotSpot) { int width = (int)visual.Width; int height = (int)visual.Height; // Render to a bitmap var bitmapSource = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); bitmapSource.Render(visual); // Convert to System.Drawing.Bitmap var pixels = new int[width*height]; bitmapSource.CopyPixels(pixels, width, 0); var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); for(int y=0; y<height; y++) for(int x=0; x<width; x++) bitmap.SetPixel(x, y, Color.FromArgb(pixels[y*width+x])); // Save to .ico format var stream = new MemoryStream(); System.Drawing.Icon.FromHandle(resultBitmap.GetHicon()).Save(stream); // Convert saved file into .cur format stream.Seek(2, SeekOrigin.Begin); stream.WriteByte(2); stream.Seek(10, SeekOrigin.Begin); stream.WriteByte((byte)(int)(hotSpot.X * width)); stream.WriteByte((byte)(int)(hotSpot.Y * height)); stream.Seek(0, SeekOrigin.Begin); // Construct Cursor return new Cursor(stream); }

于是,便回到百度键入刚刚知道的关键词 hotspot cursor ,又是一通搜索,终于,功夫不负有心人,找到一篇有用的文章:

在此感谢原文作者。所以说为什么写博客,一方面提高自己,一方面可能会在意想不到的地方给他人启发。

最终解决方案:

其实说起来太简单,就是下了一款软件:ArtCursor(直接上地址:),软件中,,有一项设置sethotspot。修改就好了。。。。。。

另外,悄悄告诉你,其实Vs中,点击选中.cur文件中在属性栏直接就可以看得到hotspot,但不知为啥设置不了,晕!我之前都在干啥(吐血)

Wpf中鼠标样式的修改,作用点修改