[英]Load an embedded animated Cursor from the Resource
我在資源中有一個動畫光標文件(* .ani),並希望在我的應用程序中將其顯示為光標。 如何從資源中加載它?
我在互聯網上查了一下,但是當你有一個真正的文件並且它沒有嵌入資源時,只有方法可以顯示它。
將ani文件作為資源嵌入,並在完成后使用Windows函數CreateIconFromResource創建它和DestroyIcon。
IntPtr hCursor;
try
{
hCursor = CreateIconFromResource(resource, (uint)resource.Length, false, 0x00030000);
this.Cursor = new Cursor(hCursor);
...
}
finally
{
this.Cursor = Cursors.Normal;
DestroyIcon(hCursor);
}
//來自資源修改這里是:調用中的byte []變量資源
//由Yvan Genesse修改的課程
public class AdvancedCursorsFromEmbededResources
{
// modified by Yvan Genesse November 29 2010
// C# example tested in MS Visual Studio 2010 Ultimate version
// University Student in E-Business @ Laurentian University
// in your form code
/*
try
{
// from file
//this.Cursor = AdvancedCursors.Create(Path.Combine(Application.StartupPath, "flower_anim.ani"));
// from resouces modification here is : byte[] resource in the call
byte[] Embeded_Cursor_Resource = Properties.Resources.flower_anim; // the animate cursor desired
this.Cursor = AdvancedCursorsFromEmbededResources.Create(Embeded_Cursor_Resource);
// or this way also works
this.Cursor = AdvancedCursorsFromEmbededResources.Create(Properties.Resources.flower_anim);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
*/
[DllImport("user32.dll")]
static extern IntPtr CreateIconFromResource(byte[] presbits, uint dwResSize, bool fIcon, uint dwVer);
// modification here is : byte[] resource in the call
public static Cursor Create( byte[] resource)
{
IntPtr myNew_Animated_hCursor;
//byte[] resource = Properties.Resources.flower_anim;
myNew_Animated_hCursor = CreateIconFromResource(resource, (uint)resource.Length, false, 0x00030000);
if (!IntPtr.Zero.Equals(hCursor))
{
// all is good
return new Cursor(myNew_Animated_hCursor);
}
else
{ // resource wrong type or memory error occurred
// normally this resource exists since you had to put Properties.Resources. and a resource would appear and you selected it
// the animate cursor desired is the error generator since this call is not required for simple cursors
throw new ApplicationException("Could not create cursor from Embedded resource ");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.