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");