根据旋转的TextBox旋转光标

我有一个TextBox ,我允许我的用户旋转。 但我喜欢我的用户是让他们的Cursor旋转到与TextBox旋转相同的角度。 例如,如果他们将TextBox旋转到28°,那么当Cursor进入TextBoxCursor也应该自己旋转到28°。

任何帮助都将不胜感激。

谢谢 :)

您可以使用WinForms中的System.Drawing.Icon类和WPF的位图旋转function来旋转光标。

这样做的方法是加载图标,将其转换为BitmapSource,使用Image和RenderTargetBitmap旋转它,将其转换回Icon,保存,最后更新使其成为的字节2,10和11。而不是.ico。

这是代码的样子:

 public Cursor GetRotatedCursor(byte[] curFileBytes, double rotationAngle) { // Load as Bitmap, convert to BitmapSource var origStream = new MemoryStream(curFileBytes); var origBitmap = new System.Drawing.Icon(origStream).ToBitmap(); var origSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(origBitmap.GetHBitmap()); // Construct rotated image var image = new Image { BitmapSource = origSource, RenderTransform = new RotateTransform(rotationAngle) }; // Render rotated image to RenderTargetBitmap var width = origBitmap.Width; var height = origBitmap.Height; var resultSource = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); resultSource.Render(image); // Convert to System.Drawing.Bitmap var pixels = new int[width*height]; resultSource.CopyPixels(pixels, width, 0); var resultBitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPargb); for(int y=0; y 

如果你想避免循环,可以用一小段usafe代码替换它来调用获取初始化数据的System.Drawing.Bitmap构造函数:

  fixed(int* bits = pixels) { resultBitmap = new System.Drawing.Bitmap(width, height, width, System.Drawing.Imaging.PixelFormat.Format32bppPargb, new IntPtr(bits)); } 

每次TextBox旋转更改时,您都需要调用它。 这可以通过旋转TextBox的代码完成,也可以通过绑定到TextBox旋转的值的PropertyChangedCallback完成。

mmm我不确定…但由于光标是由Windows管理的,我猜你需要在光标进入文本框时隐藏光标并绘制自己的光标(由于你正在旋转另一个光标,因此很容易旋转控制)。

嘿,谷歌搜索一个方法来做到这一点,第一个结果自然来自SO,你可能想检查接受的答案(如果你使用wpf):

WPF中的自定义光标?