|
C# (pronounced "C sharp") is a pretty new (2002), powerful programming language by Microsoft.
Along with the .NET framework it was designed as a direct competition to Java.
It is a component-based, easy to learn language following the tradition of C++ and Java, it even inherited someting from Basic.
C# is the language of the future: experts believe that in 2 years it will be more popular than Java or C++ and the powerful .Net framework will
soon replace the old, complicated to learn and not object oriented Windows API along with the Microsoft class library MFC!
For more information on the subject, read the official Microsoft C# Introduction and Overview.
The C# compiler and the complete .Net class library are free (click here to download it from Microsoft - choose the ".NET Framework - latest version - Software Development Kit" from the list). There are versions in English, French, German, Italian, Spanish and Japanese available. It is a big MB download, but no problem for broadband connection users. You will have to have Windows2000 or WindowsXP on your PC though - older versions will be able to run the compiled programs, but not the compiler. To run the programs on other PCs, the users will have to download and install the .Net Framework Redistributable (a runtime library), which is a 20+ MB file.
New Windows versions (from Vista on) will have it included with the standard installation and since there will be more and more programs designed for .NET, it will be a matter of time when this runtime is installed on most PCs.
Why C# and not Java? To run a Java application (not an applet) on any PC, you need to provide a set of class files or archives.
They end with *.class or *.jar and require a Java virtual machine to be installed on the host PC.
Since many users are not familiar with those files, setup and execution might be a bit difficult.
A C# program has an *.exe suffix, just like any ordinary Windows program. You can include all your resources (pictures, icons etc) into the file, if you like.
It can have an icon associated.
Although the redistributable is not a native Windows program (it consists of an intermediate bytecode, which must be compiled at runtime),
the user will not notice any difference.
For the programmer the language has great advantages over the traditional way of programming Windows with C++/MFC in terms of learnability, ease of use and power.
Microsoft offers their new Visual Studio Express series for free!
Get more info and grab your copy here.
There is another tool available for you, which is and will probably always be free: SharpDevelop, which is constantly being improved.
This development environment was used for all the programs on this page. It is a great tool, but in my opinion the Microsoft tools are better.
So what are you waiting for? Join the growing community of C# programmers!
|
Graphics Programming with GDI+
This example application introduces some of the stunning abilities of the new GDI+ graphics system.
Note that C# (like Java) has built-in support for many different graphics formats: BMP, GIF (with transparent colors),
JPEG, EXIF, PNG, TIFF, ICON, WMF and EMF. Some options not shown here can be seen from comments inside the sourcecode.
This program makes use of antialiased drawing, which is also a powerful feature of the new GDI+ graphics system!
The new Color class defines more than a hundred colors and also supports gradient brushes, texture brushes and (semi-)transparency.
» Download
|
|
Mouse events and Timers
This example introduces Mouse events and Timers.
The program keeps track of the mouse position over the window and mouse clicks.
Clicking the mouse generates a message, which disappears after 1 second.
The background is filled with a diagonal gradient.
» Download
|
|
Keyboard and Sound
Here the handling of key events is shown - some important keys you need for games are used,
and also the way sound events are processed.
Press the "S" key to play a wav sound.
» Download
|
|
Menu, Toolbar and Statusbar
In this example you can learn how to handle a menu, a toolbar and a statusbar.
You can either use the toolbar or the menu to change the background color of the client area.
The change is displayed in the statusbar.
The graphics for the toolbar are BMPs.
» Download
|
|
Standard Controls
In this example program it is shown how to set up the common standard dialog controls like buttons, radiobuttons, checkboxes, text fields and spin buttons.
In C# there is no resource script, everything is right inside your sourcecode.
The buttons feature an image and also the modern "popup" style.
The first version of the program (Controls1) doesn't save any data, while the second one (Controls2) saves and retrieves data from the registry
and the third example (Controls3) uses the powerful feature of object serialization.
» Download
|
|
Animation
Ahh... animation! You can tell from this website how easily I'm impressed with animation :-) ...
with C# you don't have to use separate threads (like in Java) or the OnIdle method of your application - you have fast and precise timers available!
C# has built-in double buffering support, so in many cases you don't need an offscreen buffer.
The example application Animation1 uses that technique as the simplest, yet effective method of flicker free animation.
In the second animation example (Animation2), a separate offscreen buffer class is used, further decreasing flicker and allowing for the animation
to be restricted to a certain area of the window, by using a panel to draw to.
You can also generate different offscreen objects and switch between them during program runtime.
» Download
|
|
Rush Hour
In this example program (RushHour1) we move several cars across the window, using an array.
Position, speed and color are chosen at random, when the program starts.
There are only three different speed values possible here (1, 2 and 3 pixel per timer tick).
The second version of the program (RushHour2) improves the idea by using a separate "Car" class, from which we create an array of objects.
It also uses floating point numbers for the coordinates, allowing for every car to have a different speed.
» Download
|
|
Sprite Animation
At last an example for displaying animated sprites.
I used a separate sprite class here, from which you can create objects.
The sprites are all stored in one single GIF file, which contains 6 frames one over the other.
» Download
|
|
Bubble Game
Finally a complete game!
Move the mouse to the border of two adjacent bubbles and click - they change places and when two or more bubbles with the same color come together,
they vanish slowly, making room for the next ones, falling from above.
Try to remove all 128 bubbles!
» Download
|
|
ColorPicker Control
To get rid of the boring standard Windows color dialog,
I developed a clone of the well-known Color Picker component used in MS Office with almost the same "look and feel" of the original.
The buttons used to call the ColorPicker dialog display the chosen color in the center.
In the example program three instances of the ColorButton class with different sizes were created, using different styles (popup, flat and normal).
The code is open source, so feel free to use it in your own projects!
» Download
|
|