( Index )
Month

Brief Information about the December '10 CSIG Meeting

TREES - Creating A Christmas Card Image   by B. Arnold

          Christmas Card
Download

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.

This month's program creates a "Christmas Card" like image using the GDI+ graphics that are included with the Microsoft Visual Studio C++ 8. This is a CLI DOT NET program. The program draws a background of stars, a foreground of snow, and then 50 randomly 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:

  ResourceManager(), GetExecutingAssembly(), Bitmap(), 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 3.5
C++ 9.0, etc.
.Net 3.5, etc.
CLI
Common Language Infrastructure
Managed

Sample Code

// OUTLINE FOR RESOURCES ... // 1. Open project, create CLR. // 2. Open Form1 design page set background color. // 3. In Solution Explorer, select top of list labeled "TREE". Rt-click to add new item, // "Assembly Resource File", name it "imageResources", click "add". A tab will open // called imageResources.resx and select "images" in the pull-down. This will be empty. // 4. Drag and Drop your jpg, png, etc. file into this empty images screen. It is automatically // given the name from the filename, "ImageTree". Do the same for any other pictures. // 5. Open Form1.h in the code screen and add the following code to assign images to Bitmap // variables which will be created in the next step. // 6. Note: the syntax if the ResourceManager() function is special: the first arguement uses the name // of the project, "Trees", then a dot, then the name of the resx file, "imageResources". // 7. Create variables SnowImg and TreeImg by going to Solution Explorer - Class View. // 8. Select Form1, rt-click, add variable, Bitmap ^ (bitmap pointer). // Create a resource manager to retrieve resources. ResourceManager^ rm = gcnew ResourceManager("Trees.imageResources",Assembly::GetExecutingAssembly() ); SnowImg = gcnew Bitmap(cli::safe_cast<Image^ >(rm->GetObject("ImageSnow"))); TreeImg = gcnew Bitmap(cli::safe_cast<Image^ >(rm->GetObject("ImageTree"))); } // // Render the Images on the screen at Paint Time. // private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { e; sender; // avoid warning C4100 message. Rectangle windowRect = ClientRectangle; Graphics ^g = e->Graphics; if (TreeImg != nullptr && SnowImg != nullptr) { int x, y, cx, cy, starsize, landHeight, skyHeight; Random^ mRand = gcnew 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; } } } } } // // Timer Tick every 5 seconds. // private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { e; sender; // avoid warning C4100 message. Refresh(); }

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