So, I was going through Head First C# and doing the programming exercise using Gtk#/MonoDevelop instead of WinForms/Visual Studio. Of course, then it takes some workarounds(or I should say, different ways) to do certain things in Gtk#.
1. Embedding resources
Pretty straightforward to add resources in Visual Studio. But how to do that in MonoDevelop?
http://stackoverflow.com/questions/4383298/how-do-i-use-a-resources-file-in-monodevelop
2. Calling resources
Visual Studio auto-generates some Properties.Resources thingy. I was told that I have to do it the .NET way. Never heard of that. (Btw, still confused about what are assemblies.)
Using System.Reflection; //for Assembly
Using System.IO; //for Stream
Assembly a = Assembly.GetExecutingAssembly();
Stream s = a.GetManifestResourceStream("BeeHive.Resources.Bee animation 1.png");
Wow, looks pretty complicated, but it works. (I need to find out how it works)
3. Creating a custom PictureBox control
Well, no PictureBox in GTK. Closest thing is Gtk.Image. So that I'll use.
Apparently, if you extend a control class in Visual Studio it'll just magically appear in your toolbox. Nope, it does not work in MonoDevelop.
Found this video:
http://monodevelop.com/Creating_custom_widgets_with_MonoDevelop
Although, seems a bit overkill for my purpose. I just want to extend Gtk.Image. Well, turns out, I just have to add
[System.ComponentModel.ToolboxItem(true)]
to my widget class. And NOW it appears in my toolbox. YES!
4. Animating the control
I'm just following the textbook here. Sounds like a poor way to animate a control when Gtk.Image supports animated image, I think. But never mind. So, I set a timer that changes the Pixbuf property at certain intervals. Conveniently, the Pixbuf constructor allows a Stream as an argument. But wait, even MORE conveniently,
Pixbuf (System.Reflection.Assembly, string)
Pixbuf (System.Reflection.Assembly assembly, string resource, int width, int height)
| So I don't have to do all that GetManifestResourceStream crap. Wow, the latter could be quite convenient for future use.
Conclusion
Not bad, I learned a lot today. Almost wanted to give up and just use Visual Studio instead.
|