C# C:如何从资源文件加载游标?
我已将文件x.ani导入资源文件Resources.resx。现在尝试使用ResourceManager.GetObjectaero_busy.ani加载该文件C# C:如何从资源文件加载游标?,c#,.net,C#,.net,我已将文件x.ani导入资源文件Resources.resx。现在尝试使用ResourceManager.GetObjectaero_busy.ani加载该文件 Cursor.Current = (Cursor)ResourcesX.GetObject("aero_busy.ani"); 但是它不起作用。。当然可以: 如何使用资源对象更改当前光标?我没有找到比转储到临时文件并使用Win32 load Cursor from file方法更好的方法。黑客是这样做的,为了清晰起见,我删除了一大块样
Cursor.Current = (Cursor)ResourcesX.GetObject("aero_busy.ani");
但是它不起作用。。当然可以:
如何使用资源对象更改当前光标?我没有找到比转储到临时文件并使用Win32 load Cursor from file方法更好的方法。黑客是这样做的,为了清晰起见,我删除了一大块样板代码,其中一个临时文件是用流中的数据编写的。此外,所有异常处理等都已删除
[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.
}
在加载嵌入式资源时,您可以将其用作记住名称空间
Cursors.Current = LoadCursorFromResource("My.Namespace.Filename");
我认为问题在于游标必须具有.cur扩展名才能用作游标 //下面将从嵌入式资源生成一个游标 //要添加自定义光标,请创建或使用现有的16x16位图 // 1. 将新光标文件添加到项目中: //文件->添加新项目->本地项目项目->光标文件 // 2. 选择16x16图像类型: //图像->当前图标图像类型->16x16 以上内容摘自 更新:找到原因的答案 附注
游标类不支持动画游标.ani文件或非黑白颜色的游标
找到我是通过将cursor.cur文件添加到我使用Visual Studio的项目的资源部分来完成的。我不确定它是否必须是.cur,只要开发程序可以加载它 在代码的变量声明部分中,我从游标文件创建了一个MemoryStream:
private static System.IO.MemoryStream cursorMemoryStream = new System.IO.MemoryStream(myCurrentProject.Properties.Resources.myCursorFile);
…然后您可以从MemoryStream创建光标:
private Cursor newCursor = new Cursor(cursorMemoryStream);
然后,您可以在程序中根据需要指定光标,例如
pictureBox1.Cursor = newCursor;
新光标是作为程序的一部分编译的。经过几轮讨论后,我发现优雅的解决方案是:
internal static Cursor GetCursor(string cursorName)
{
var buffer = Properties.Resources.ResourceManager.GetObject(cursorName) as byte[];
using (var m = new MemoryStream(buffer))
{
return new Cursor(m);
}
}
把这一切放在一起 这是VisualStudio现在提供的强类型资源以及Win32 LoadCursorFromFile(通过使用清单资源加载)的组合 我还加入了一个实例化游标的缓存,因为这适合我的应用程序。如果你不需要,就用核弹
namespace Draw
{
/// <summary>
/// Controls use of all the cursors in the app, supports loading from strongly typed resources, and caches all references for the lifetime of the app.
/// </summary>
public static class Cursors
{
// Cache of already loaded cursors
private static ConcurrentDictionary<byte[], Cursor> cache = new ConcurrentDictionary<byte[], Cursor>();
/// <summary>
/// Returns a cursor given the appropriate id from Resources.Designer.cs (auto-generated from Resources.resx). All cursors are
/// cached, so do not Dispose of the cursor returned from this function.
/// </summary>
/// <param name="cursorResource">The resource of the cursor to load. e.g. Properties.Resources.MyCursor (or byte array from .cur or .ani file)</param>
/// <returns>The cursor. Do not Dispose this returned cursor as it is cached for the app's lifetime.</returns>
public static Cursor GetCursor(byte[] cursorResource)
{
// Have we already loaded this cursor? Use that.
if (cache.TryGetValue(cursorResource, out Cursor cursor))
return cursor;
// Get a temporary file
var tmpFile = Utils.GetTempFile();
try
{
// Write the cursor resource to temp file
File.WriteAllBytes(tmpFile, cursorResource);
// Read back in from temp file as a cursor. Unlike Cursor(MemoryStream(byte[])) constructor,
// the Cursor(Int32 handle) version deals correctly with all cursor types.
cursor = new Cursor(Win32.LoadCursorFromFile(tmpFile));
// Update dictionary and return
cache.AddOrUpdate(cursorResource, cursor, (key, old) => cursor);
return cursor;
}
finally
{
// Remove the temp file
Utils.TryDeleteFile(tmpFile);
}
}
}
}
将文件写入磁盘,然后将其作为游标导入是不切实际的,有一种更简单的解决方案。将.cur转换为.ico并将光标作为图标资源导入。然后你可以简单地做:
Cursor c = new Cursor(Properties.Resources.mycursor.Handle);
这将正确支持32位颜色。游标类不支持动画游标。正如我下面的回答。关于Jethro的评论和回答,我只在这个方法中使用了.cur非动画游标。这个方法应该允许使用彩色光标。谢谢你宝贵的回复:我只是想让我的程序成为一个可移植的程序,也可以调用存根程序。我的意思是只需要复制一个Exe文件。不过,您的建议值得实现:再次感谢。我确认此代码适用于彩色光标。然而,将字节放入内存流而不是文件是不起作用的。@InfantPro'Aravind'-这个答案只需要一个exe文件。游标作为资源嵌入到该文件中。您只需要一个文件夹,允许您在其中创建临时文件。例如,Environment.GetFolderPathEnvironment.SpecialFolder.ApplicationData感谢宝贵的时间和响应:+1重要提示:更新:尽管文档中的注释今天仍显示在.Net 4.7.2文档中,Cursor类现在部分支持颜色光标。看,我觉得这条路比其他的好!我确认大猩猩的评论。此答案使用内存流,不适用于32位颜色。对于颜色,请使用。请只写一个答案,如果它显示了一些新的东西,而不是其他答案所涵盖的。这项技术在7年前的公认答案中已经展示。注意:所有基于MemoryStream的答案都适用于黑白光标,但不支持32位彩色光标。希望微软有一天会纠正这一点,这样我们就可以回到这样的技术上来。在此之前,对于颜色游标,必须使用new CursorLoadCursorFromFile。。而不是记忆流。
Cursor = Draw.Cursors.GetCursor(Properties.Resources.Cursor_ArrowBoundary);
Cursor c = new Cursor(Properties.Resources.mycursor.Handle);