( Index )


Brief Information about the June 2015 CSIG Meeting

PrintDemo.cs - An Introduction to DotNet Printing ......    by B. Arnold

LATE BREAKING NEWS:   http://www.techsupportalert.com/ content/ microsoft-visual-studio- now-free-non-commercial-use.htm

(General Csig Info)    This month's application is a Microsoft C-sharp program that deals with printing a simple text document. Although programmed in C# it is easily converted to C++ since it uses the DOT NET libraries. See below for further information.


Sample of PrintDemo

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

At the meeting we will be discussing this word and how it relates to the printing methods.

This is a CLI DOT NET program is coded using C-sharp (C#) and is very similar to C++.  It also still uses the same DOT NET libraries.

The program uses a number of DOT NET Library functions including the following:
PrintDemo CanDuplex Collate Copies
Duplex FromPage InstalledPrinters IsDefaultPrinter
IsPlotter IsValid MaximumCopies MaximumPage
PaperSizes PrinterName PrintRange PrinterSettings
Graphics StreamReader Print PrintDocument
printFont leftMargin topMargin Graphics
DrawString Brushes GetHeight MarginBounds
HasMorePages Graphics.Draw  Format String
MessageBox Show System.IO Drawing.Printer

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

Sample Code

public partial class Form1 : System.Windows.Forms.Form { private Font printFont; private StreamReader streamToPrint; private string path = "../../Form1.cs"; private int pageNumber = 1; public Form1() { // Init the Windows Forms Designer. InitializeComponent(); } // The Click event is raised when the user clicks the Print button. // private void printButton_Click(object sender, EventArgs e) { try { streamToPrint = new StreamReader(path); // Print this source code. try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); MessageBox.Show(String.Format( "File to be Printed is \n{0}\n\nPrinted output will go to \n{1}\n", Path.GetFullPath(path), pd.PrinterSettings.PrinterName.ToString())); pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); pd.Print(); // Starts the printing process. } finally { streamToPrint.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /* Further discussion on the above pd.Print() call * ------------------------------------------------- * This method starts the actual printing by AUTO-MAGICALLY initiating 5 events. * The events are 1. BeginPrint; 2. QueryPageSettings; 3. PrintPage; 4. EndPrint; 5. Disposed */ // The PrintPage event is raised for each page to be printed. // private void pd_PrintPage(object sender, PrintPageEventArgs ev) { float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; // Calculate the number of lines per page. // linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print a header line with the filename and page number. // line = Path.GetFullPath(path); yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; line = String.Format("Page - {0}", pageNumber++); yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; count++; // Print each line of the file. // while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null)) { yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page. // if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } }

Sample Code from Past Meetings

// FORM1.H ACGNJ CSIG MEETING December 16, 2014 - WRITTEN BY BRUCE ARNOLD using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections; namespace Circles { public partial class Form1 : Form { const int FIFTY_CIRCLES = 50; const int DIAMETER = 250; const int TIMER_INTERVAL = 2000; // milliseconds static ArrayList CircleArray = new ArrayList(); public Form1() { InitializeComponent(); this.DoubleBuffered = true; // for better, smoother screen updates. this.WindowState = FormWindowState.Maximized; } ~Form1() { CircleArray.RemoveRange(0, CircleArray.Count); // Clear memory. } // Here is where we create a brand new circle. The default constructor of "Circle" // then calls the constructors of its "base" classes. Remember: "It's turtles all // the way down!" A loop creates 50 circles at a time. private void timer1_Tick(object sender, EventArgs e) { this.timer1.Interval = TIMER_INTERVAL; int windowWidth = this.ClientRectangle.Width; int windowHeight = this.ClientRectangle.Height; Random autoRand = new Random(); int xorg, yorg; CircleArray.RemoveRange(0, CircleArray.Count); // Clear memory. for (int loop = 0; loop < FIFTY_CIRCLES; ++loop) { Circle cc = new Circle(); // Create new circle. cc.Diameter(windowHeight / 4); int bkg = autoRand.Next(9); switch (bkg) { case 0: cc.BkGround(Brushes.SandyBrown); break; // Random case 1: cc.BkGround(Brushes.Red); break; // color case 2: cc.BkGround(Brushes.Orange); break; // pick. case 3: cc.BkGround(Brushes.Yellow); break; case 4: cc.BkGround(Brushes.Green); break; case 5: cc.BkGround(Brushes.Blue); break; case 6: cc.BkGround(Brushes.Violet); break; case 7: cc.BkGround(Brushes.LightGray); break; case 8: cc.BkGround(Brushes.White); break; } xorg = windowWidth - cc.cx + 200; if (xorg < 0) xorg = 0; // Calculate limits. yorg = windowHeight - cc.cy + 200; if (yorg < 0) yorg = 0; xorg = autoRand.Next(xorg) - 100; // Randomize values yorg = autoRand.Next(yorg) - 100; cc.Position(xorg, yorg); CircleArray.Add(cc); } Invalidate(); } private void Form1_Paint(object sender, PaintEventArgs e) { foreach (Circle obj in CircleArray) { obj.Paint(e); } }


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