2010-08-17 12 views
1

J'ai un fichier Cursor animé (* .ani) dans les ressources et je veux l'afficher en tant que curseur dans mon application. Comment puis-je le charger à partir des ressources? J'ai cherché dans Internet, mais il n'y a que des façons de le montrer quand on a un vrai fichier et s'il n'est pas intégré dans les ressources.Charger un curseur animé incorporé à partir de la ressource

+0

s'il vous plaît ne pas utiliser "Salut", " Merci ", ou des signatures ici sur SO. Ce n'est pas un forum de discussion. –

+0

@John: Salut, John! Vous voulez dire que vous ne voulez pas discuter? Merci d'avoir lu ça! –

+0

@ John: Ok, désolé, je ne le savais pas – Wonderwhy

Répondre

2

// modification de ressources ici est: octet [] ressource variable dans l'appel

// classe modifiée par 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 "); 
     }   
}  


} 
+0

Merci Yvan :) – Wonderwhy

2

Intégrez le fichier ani en tant que ressource et utilisez les fonctions Windows CreateIconFromResource pour le créer et DestroyIcon lorsque vous avez terminé.

IntPtr hCursor; 
try 
{ 
    hCursor = CreateIconFromResource(resource, (uint)resource.Length, false, 0x00030000); 
    this.Cursor = new Cursor(hCursor); 
    ... 
} 
finally 
{ 
    this.Cursor = Cursors.Normal; 
    DestroyIcon(hCursor); 
} 
Questions connexes