C#中:如何加载光标从资源文件? [英] C#: How to load Cursor from Resource file?

查看:1090
本文介绍了C#中:如何加载光标从资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导入的文件x.ani到资源文件Resources.resx。现在试图加载使用ResourceManager.GetObject该文件(aero_busy.ani)

I have imported a file "x.ani" into Resource file Resources.resx. Now trying to load that file using ResourceManager.GetObject("aero_busy.ani")

Cursor.Current = (Cursor)ResourcesX.GetObject("aero_busy.ani");

但没有奏效。(当然):)

but it didn't work .. (certainly) :)

使用资源的对象我怎样才能改变当前光标?

How can I change the current Cursor using resource object?

推荐答案

我还没有发现任何高于倾销到一个临时文件,并使用Win32负载光标从文件的方法更好的方法。该黑客是这样的(我删除的样板code一大块为清晰起见,在一个临时文件从流写入数据)。此外,所有异常处理等已被删除。

I haven't found any better way than dumping to a temp file and use the Win32 load cursor from file method. The hack goes something like this (I removed a big chunk of boilerplate code for clarity, in which a temp file is written with the data from the stream). Also, all exception handling etc. was removed.

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern IntPtr LoadCursorFromFile(String str);

public static Cursor LoadCursorFromResource(string resourceName)
{         
     Stream cursorStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);        

     // Write a temp file here with the data in cursorStream

     Cursor result = new Cursor(LoadCursorFromFile(tempFile));
     File.Delete(tempFile);

     return result.
}

您会用这个作为(加载嵌入的资源时记得命名空间)。

You would use this as (remember namespaces when loading embedded resources).

Cursors.Current = LoadCursorFromResource("My.Namespace.Filename");

这篇关于C#中:如何加载光标从资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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