Custom Cursor from Bytes
-
Anyone know how to get a cusor to load from an array of bytes? I have a byte[] in a resorce file that is a custom cursor. I need to load a System.Windows.Forms.Cursor from it. I don't have to use a resource file for it, but it needs to be embeded in the exe some how and not on disk. Thanks in advance.
-
Anyone know how to get a cusor to load from an array of bytes? I have a byte[] in a resorce file that is a custom cursor. I need to load a System.Windows.Forms.Cursor from it. I don't have to use a resource file for it, but it needs to be embeded in the exe some how and not on disk. Thanks in advance.
It's easier if you don't embed it in a ResX file. Instead, add the .cur file to your project and change it's Build Action (in the properties displayed when you click on the file) to "Embedded Resource". To get the Cursor (Icons work this way, too), either use
Assembly.GetManifestResourceStream
or - easier - use this particularCursor
constructor:Cursor cur = new Cursor(typeof(SomeClass), "MyCursor.cur");
...where
SomeClass
is aType
that scopes the resource (provides both the assembly reference and the namespace to find the embedded resource by name). For more information, see the aforementioned constructor. Basically, ifSomeClass
exists in the namespaceMyCompany.MyProject
, the example would look for an embedded resource named "MyCompany.MyProject.MyCursor.cur".-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
It's easier if you don't embed it in a ResX file. Instead, add the .cur file to your project and change it's Build Action (in the properties displayed when you click on the file) to "Embedded Resource". To get the Cursor (Icons work this way, too), either use
Assembly.GetManifestResourceStream
or - easier - use this particularCursor
constructor:Cursor cur = new Cursor(typeof(SomeClass), "MyCursor.cur");
...where
SomeClass
is aType
that scopes the resource (provides both the assembly reference and the namespace to find the embedded resource by name). For more information, see the aforementioned constructor. Basically, ifSomeClass
exists in the namespaceMyCompany.MyProject
, the example would look for an embedded resource named "MyCompany.MyProject.MyCursor.cur".-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks Heath. I ended up doing it this way. I added it to the project and used a resourcemanager to pull it out. It works great this way. Resources are one spot in .net that I find lacking. MS didn't even provide a good resource editor. All I can find on the net don't work with custom cursors. I had one that was an adding for vs02 but lost it and can't find it again. It worked great.
-
Thanks Heath. I ended up doing it this way. I added it to the project and used a resourcemanager to pull it out. It works great this way. Resources are one spot in .net that I find lacking. MS didn't even provide a good resource editor. All I can find on the net don't work with custom cursors. I had one that was an adding for vs02 but lost it and can't find it again. It worked great.
Why did you use the
ResourceManager
? You simply grab the manifest resource stream. TheResourceManager
is for ResX files. VS.NET has a great resource editor, far better than VS6. It still handles cursors, icons, and bitmaps internally. Instead of string resources, you're supposed to use ResX files. The great thing is that you can embed any file as an embedded resource. If you right-click, you can select "Open with..." to open ANY resource with ANY program (and keep a cached list of programs you use). In VS6, if the resource wasn't understood / handled by the IDE, you had to export the resource, browse to it and open it with the associated application (or open the application then open the file). That was a pain. In any case, 'tis far easier to just embed the .cur file as an embedded resource and use theCursor
constructor I mentioned, not theResourceManager
. Besides, binary files are stored as bse64 encodings by default. TheResourceManager
has overhead for setting things up, then has to find the resource and decode it. The other way is quick and is closer to how it would be done in Win32 when loading a cursor from a persistent file.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----