原文:从FrameworkElement对象创建Cursor对象

Normal
0

false
false
false

EN-US
ZH-CN
X-NONE

MicrosoftInternetExplorer4

 

理论上可以从任何派生自 Visual的对象创建光标对象,但是由于 FrameworkElement拥有 ActualWidth和 ActualHeight属性,这就省去了指定光标大小的麻烦。创建过程很简单,可运行代码如下:

public static class CursorHelper

  {

    // 表示每英寸的像素点数

    private const int DPI = 96;

 

    public static Cursor CreateCursorFromFrameworkElement(FrameworkElement e, Point hotSpot)

{

       // 把界面元素呈现到位图上

      RenderTargetBitmap renderTargetBitmap =

        new RenderTargetBitmap ((int )e.ActualWidth, (int )e.ActualHeight, DPI, DPI, PixelFormats .Pbgra32);

      renderTargetBitmap.Render(e);

 

      PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder ();

      BitmapFrame bitmapFrame = BitmapFrame .Create(renderTargetBitmap);

      bitmapEncoder.Frames.Add(bitmapFrame);

 

      using (var stream = new System.IO.MemoryStream ())

      {

        // 把位图保存到一个 Stream 中

        bitmapEncoder.Save(stream);

 

        stream.Seek(0, System.IO.SeekOrigin .Begin);

        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (stream);

        // 光标文件 .cur 文件其实是基于图标文件的( .ico ) , 所以先将位图转换为图标

        IntPtr iconHandle = bitmap.GetHicon();

        System.Drawing.Icon icon = System.Drawing.Icon .FromHandle(iconHandle);

 

        using (System.IO.Stream iconStream = new System.IO.MemoryStream ())

        {

          icon.Save(iconStream);

 

     // 第二个字节表示该图形是图标还是光标, 2 表示光标

           iconStream.Seek(2, System.IO.SeekOrigin .Begin);

          iconStream.WriteByte(2);

     // 第十个字节处存放热点坐标的 x 坐标,热点就是用来点击屏幕的点,该点的坐标就是其在位图中的坐标,此处参数用的是相对坐标。此处转换为绝对坐标

          iconStream.Seek(10, System.IO.SeekOrigin .Begin);

          iconStream.WriteByte((byte )(int )(e.ActualWidth * hotSpot.X));

Normal
0

false
false
false

EN-US
ZH-CN
X-NONE

MicrosoftInternetExplorer4


<![endif]-->

   // 第十二个字节处存放热点坐标的 y 坐标

          iconStream.Seek(12, System.IO.SeekOrigin .Begin);

          iconStream.WriteByte((byte )(int )(e.ActualHeight * hotSpot.Y));

          iconStream.Seek(0, System.IO.SeekOrigin .Begin);

          return new Cursor (iconStream);

        }

      }

    }

最新文章

  1. servlet开发中遇到的问题集合
  2. dataTables添加序号和行选中框
  3. Ext JS 6 入门学习资料大全(2016-12-14)
  4. [Python] Keep efficient by vim in Pycharm
  5. Tips7:Unity中 Scene视图 和 Game视图 中 视角(Camera)的控制
  6. 音乐ID3 中 专辑封面解析(APIC帧)
  7. opengl模板缓冲区
  8. Colorful Image Colorization 的环境配置
  9. jquery和javascript的区别(常用方法比较)
  10. 解决 ImportError: No module named _internal
  11. 菲律宾Globe/TM卡最省钱的上网方案
  12. Git(使用码云)
  13. Aizu - 2200 Mr. Rito Post Office
  14. 高性能MySQL(第3版) 中文PDF带目录清晰版
  15. 【6集iCore3_ADP触摸屏驱动讲解视频】6-2 基于FSMC总线的ARM与FPGA通信
  16. Spark学习笔记——构建基于Spark的推荐引擎
  17. vmware虚拟网络
  18. (转)C#如何加载程序运行目录外的程序集
  19. php开发工具,zendstudio13使用方法补丁
  20. [转]python数据持久存储:pickle模块的基本使用

热门文章

  1. (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text
  2. SourceInsight打开的工程中中文字体显示乱码的问题
  3. 【BZOJ 2119】股市的预测
  4. embed-it_Integrator memory compile工具使用之三
  5. 【Android开源框架】使用andbase开发框架实现绘制折线图
  6. js中数组如何使用
  7. mha &amp;&amp; 脚本拉起
  8. 【30.93%】【codeforces 558E】A Simple Task
  9. [Recompose] Add Local State to a Functional Stateless Component using Recompose
  10. java的对象锁和对象传递