( Index )
Month

 

Brief Information about the February 18th CSIG Meeting

TREES - Creating A Christmas Card Image   by B. Arnold

Reincarnated from C++ Dec. 2010.  Redesigned code for C#, C-sharp, Visual Studio 2013.

Christmas Card
Download app
Download snow(Save target as...)
Download tree (Save target as...)

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs. (Some are FREE!)

New for this month is the following.

The weather was a problem in December and January so the program demonstration is being repeated for February. This months program creates a "Christmas Card" like image using the GDI+ graphics that are included with the Microsoft Visual Studio. This is a CLI DOT NET program styled after our September 2010 program of the same name. However, it is now coded using C-sharp (C#) instead of C++. (Most of the conversion to C# was aided by last month's utility program called cppTOsharp.) It still uses the same DOT NET libraries and is virtually identical to the original program.

One of the changes is that the images are now separate data files. ImageSnow.png and ImageTree.png should be in the same location as the .exe file. The program draws a background of stars, a foreground of snow, and then 50 ramdomly located Christmas trees complete with lights.

Visual Studio C++ includes many Graphics functions. The object of this program is to create a simple holiday image. The program uses some of the most sophisticated Dot Net Library functions including the following:

  Image(), Timer(), ClientRectangle, Random(), DrawImage(), FillEllipse(), and others.


There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 4.5
C++ 9.0, etc.
.Net 4.5, etc.
CLI
Common Language Infrastructure

Sample Code

// OUTLINE FOR RESOURCES ... // 1. Open project, create C# Windows Form. // 2. Open Form1 design page set background color to dark slate blue. // 3. Modify C++ code from Dec. 2010 using cppTOsharp. namespace Trees { public partial class Form1 : Form { public const int MAXTREES = 50; public const int MAXSTARS = 750; Image SnowImg; Image TreeImg; bool Resizing, Firstime; public Form1() { InitializeComponent(); this.DoubleBuffered = true; // Change to false to see the trees created. Resizing = false; Firstime = true; } private void Form1_Paint(object sender, PaintEventArgs e) { if (Firstime) // Prevent trees from jumping due to window initialization. { Firstime = false; return; // skip first paint. } if (TreeImg != null && SnowImg != null && Resizing != true) { Rectangle windowRect = ClientRectangle; Graphics g = e.Graphics; int x, y, cx, cy, starsize, landHeight, skyHeight; Random mRand = new Random(); landHeight = (windowRect.Height * 3) / 4; skyHeight = windowRect.Height - landHeight; // STARS **** STARS **** STARS **** STARS **** STARS **** STARS **** for (int i = 0; i < MAXSTARS; i++) { starsize = mRand.Next(5); x = mRand.Next(windowRect.Width); y = mRand.Next(windowRect.Height); g.FillEllipse(Brushes.White, x, y, starsize, starsize); } // SNOW **** SNOW **** SNOW **** SNOW **** SNOW **** SNOW **** cx = windowRect.Width; cy = landHeight; g.DrawImage(SnowImg, 0, skyHeight, cx, cy); // TREES **** TREES **** TREES **** TREES **** TREES **** TREES **** for (int i = 0; i < MAXTREES; i++) { cx = TreeImg.Width / 4; // scale image cy = TreeImg.Height / 4; int scale = mRand.Next(2) + 1; // scale again cx /= scale; cy /= scale; if (cx >= windowRect.Width) continue; if (cy >= landHeight) continue; // User has shrunk window. x = mRand.Next(windowRect.Width - cx); y = mRand.Next(landHeight - cy); y += skyHeight; g.DrawImage(TreeImg, x, y, cx, cy); // COLORED LIGHTS **** COLORED LIGHTS **** COLORED LIGHTS **** COLORED LIGHTS int deltaX, deltaY; cx /= 2; cy /= 2; for (int a = 0; a < 10; a++) { deltaX = (mRand.Next(cx) - (cx / 2)); deltaY = (mRand.Next(cy + 10) - (cy / 2)); switch (mRand.Next(3)) { case 0: g.FillEllipse(Brushes.Red, deltaX + x + cx, deltaY + y + cy, 6, 6); break; case 1: g.FillEllipse(Brushes.Yellow, deltaX + x + cx, deltaY + y + cy, 6, 6); break; case 2: g.FillEllipse(Brushes.Blue, deltaX + x + cx, deltaY + y + cy, 6, 6); break; } } } } } private void timer1_Tick(object sender, EventArgs e) // EVERY 5 SECONDS MOVE TREES { Refresh(); } private void Form1_SizeChanged(object sender, EventArgs e) // MOVE TREES ACCORDINGLY { timer1.Stop(); Refresh(); timer1.Start(); // Start timer back at the beginning. } private void Form1_Load(object sender, EventArgs e) // LOAD IMAGES AT START { try { SnowImg = Image.FromFile("ImageSnow.png"); TreeImg = Image.FromFile("ImageTree.png"); } catch (System.IO.FileNotFoundException err) { MessageBox.Show("ImageSnow.png\n\n and/or \n\nImageTree.png", "FILE(S) NEEDED"); this.Close(); } } } }

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page