( Index )

Brief Information about the December '09 CSIG Meeting

Clock2009Saver - A Small Clock Screen Saver for your Desktop   by B. Arnold

Clock2009     Clock2009     Clock2009

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!)

Here's a brief synopsis of the coming meeting:

New for this month is the following. The code for the Clock2009 program has been left intact but extra code has been added to create a screen saver that uses the same graphics as before. This is a normal sequence of events. When a programmer writes code, the concept of the graphics must be developed. Then, if the application is to become a screen saver, the code can be modified.

The new screen saver has a number of nice features. When it is first triggered, the opacity of the graphic is set at zero. This means that whatever is on the desktop is still seen and the clock and background are completely transparent. Over the next 10 seconds, the opacity is gradually increased to 100 percent at which time the screen is completely black except for the clock. This gradual dimming is very pleasing to the eyes. For example, as I am reading this text I notice that the screen is starting to dim as the screen saver tries to take command. I can then touch the mouse to continue working without missing a word in the text. Other features include a random positioning of the clock every 10 minutes.

Visual Studio C++ includes many Graphics functions. The object of this program is to create a simple clock that can be placed on your desktop. It has the visual appearance of the Clock Gadget included with Windows Vista and Windows 7, but it is not a "Gadget". It's a simple program designed to be useful and to show how simple programs can be developed. The program uses some of the most sophisticated Dot Net Library functions including the following:

  ScaleTransform(), TranslateTransform(), RotateTransform(), DrawPolygon(), FillPolygon(), FillEllipse(), SmoothingMode and PixelOffsetMode

These functions make the mathematics and geometry much simplier. For a comparison see the clock program from March 2001.

Other features

In this modern age with company computers on secure networks as well as home computers running Vista or Windows 7 there are special issues. For example, how do you save and restore the screen position of the clock so that it always starts at the same place on the screen? This is a good question if you want to put it in your startup folder. In the old days you could put an ".ini" file in the Windows folder, but this is now write protected. Another idea is to place the data in the Registry, but this is also write protected in most cases. The clock program provides a modern solution to this dilemma. Code has been added to save the location upon closing and to restore it when the program is opened again.

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
.Net 3.5
Common Language Infrastructure

Sample Code

       void hand(double size, double angle, System::Windows::Forms::PaintEventArgs^  e)
         // angle is in degrees from the 3 o'clock position moving clockwise.
         //  P1 +--------------------------+ P2
         //  P0 +                          |    P0 is at the center of the clock
         //  P4 +--------------------------+ P3

         array ^Points  = gcnew array  (5);
         Points[0].X = 0.0F;        Points[0].Y = 0.0F;
         Points[1].X = 0.0F;        Points[1].Y = (float)(HANDWIDTH / 2.0F);
         Points[2].X = (float)(size);   Points[2].Y = (float)(HANDWIDTH / 2.0F);
         Points[3].X = (float)(size);   Points[3].Y = (float)(-HANDWIDTH / 2.0F);
         Points[4].X = 0.0F;        Points[4].Y = (float)(-HANDWIDTH / 2.0F);

         Graphics ^g = e->Graphics;
         g->TranslateTransform(ORIGIN, ORIGIN);
         g->RotateTransform( (float)angle);
         g->SmoothingMode = Drawing::Drawing2D::SmoothingMode::AntiAlias;
         g->PixelOffsetMode = Drawing::Drawing2D::PixelOffsetMode::HighQuality;
         g->DrawPolygon(System::Drawing::Pens::Gray, Points);           // Shadow.
         g->FillPolygon(System::Drawing::Brushes::White, Points);
         // Now draw dot at the center of the clock.
         g->DrawEllipse(System::Drawing::Pens::Gray,  -6.0F, -6.0F, 12.0F, 12.0F);  // Shadow.
         g->FillEllipse(System::Drawing::Brushes::White,  -6.0F, -6.0F, 12.0F, 12.0F);

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
       sender; e;
       this->Refresh(); // paint the whole clock when the tick occurs.


#define   DATASUBDIRECTORY  "Clock2009"
#define   DATALOCATIONFILE  "Location.txt"

     //            Data is saved and read from %appdata% folder.
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
       sender; e;
       Point m_Loc = this->Location;
       if (WindowState == FormWindowState::Minimized) return; // m_Loc= -32000,-32000 !!!
       String ^p  = Environment::GetFolderPath(System::Environment::SpecialFolder::ApplicationData);
       IO::DirectoryInfo^ di = gcnew IO::DirectoryInfo(p);
       if (di->Exists)
         IO::DirectoryInfo ^dis = nullptr;
         IO::StreamWriter ^sw = nullptr;
           dis = di->CreateSubdirectory(DATASUBDIRECTORY);
           if (dis->Exists)
             String ^mFile = gcnew String(dis->FullName) + "\\" + DATALOCATIONFILE;
             sw = gcnew IO::StreamWriter(mFile);
             sw->WriteLine(m_Loc.ToString()); // "{X=nnn,Y=nnn}"
         catch (...)
           if (sw != nullptr) sw->Close();


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