旋转光标据旋转文本框 [英] Rotating Cursor According to Rotated TextBox

查看:142
本文介绍了旋转光标据旋转文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我让我的用户旋转。但我很想为我的用户是有自己的光标旋转相同的角度,该文本框在被旋转。例如,如果它们旋转文本框 28°,那么当光标进入该文本框光标也应该本身旋转到28°。

I have a TextBox that I allow my users to rotate. But what I would LOVE for my users is to have their Cursor rotate to the same angle that the TextBox was rotated at. For example, if they rotated the TextBox to 28°, then when the Cursor enters that TextBox the Cursor should also rotate itself to 28°.

任何帮助都将大大AP preciated。

Any help at all will be greatly appreciated.

感谢您:)

推荐答案

您可以利用结合WPF的位图旋转能力的WinForms的System.Drawing.Icon类旋转光标。

You can rotate your cursor using the System.Drawing.Icon class from WinForms in combination with WPF's bitmap rotation ability.

要做到这一点的方法是加载图标,将其转换为一个的BitmapSource,使用图像和RenderTargetBitmap转动它,将其转换回为一个图标,保存它,最后更新字节2,10,11,使它的.cur,而不是.ICO。

The way to do this is to load the icon, convert it to a BitmapSource, use Image and RenderTargetBitmap to rotate it, convert it back to an Icon, save it, and finally update bytes 2, 10, and 11 that make it a .cur instead of a .ico.

这里的code样子:

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<height; y++)
    for(int x=0; x<width; x++)
      resultBitmap.SetPixel(x, y, Color.FromArgb(pixels[y*width+x]));

  // Save to .ico format
  var resultStream = new MemoryStream();
  new System.Drawing.Icon(resultBitmap.GetHIcon()).Save(resultStream);

  // Convert saved file into .cur format
  resultStream.Seek(2); resultStream.WriteByte(curFileBytes, 2, 1);
  resultStream.Seek(10); resultStream.WriteByte(curFileBytes, 10, 2);
  resultStream.Seek(0);

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

如果你想避免循环,你可以用USAFE code的一个小一点调用System.Drawing.Bitmap构造函数初始化数据替换它:

If you want to avoid the loop, you can replace it with a small bit of usafe code to call the System.Drawing.Bitmap constructor that takes initialization data:

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

您将需要每次都这样打电话给你的文本框的旋转变化。这可以从code,它旋转你的文本框,或从绑定到TextBox的旋转值的PropertyChangedCallback来完成。

You'll need to call this every time your TextBox rotation changes. This can be done either from the code that rotates your TextBox, or from a PropertyChangedCallback on a value that is bound to the TextBox's rotation.

这篇关于旋转光标据旋转文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆