public void PrintCard(Point pt, Size sz, BingoTable table, Color color, ref Graphics graphicsObj, BingoLicense licenseInfo) { if (this.numCardsToPrint > 0) { if (licenseInfo.isLicensed == true) { Random rand = new Random(); bingoTable.RandSeed = rand.Next() % 9999; } else { bingoTable.RandSeed += 1; } DrawBingoCard(pt, sz, bingoTable, Color.Black, ref graphicsObj); //return (true); } this.numCardsToPrint -= 1; }
public void DrawBingoCard(Point pt, Size sz, BingoTable table, System.Drawing.Color tableColor, ref Graphics graphicsObj) { List<string> data = new List<string>(table.items); data.Sort(); Pen myPen = new Pen(tableColor, 2); Brush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); Brush blackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); Rectangle rect = new Rectangle(pt, sz); graphicsObj.FillRectangle(whiteBrush, rect); int startX = pt.X; int startY = pt.Y; int endX = sz.Width + pt.X; int endY = sz.Height + pt.Y; //If free space is enabled, then we have to find the mean and median of rows and cols so we know where to put the free space int rowMedian = -1; int colMedian = -1; bool thisIsAFreeSpace = false; if (table.printFreeSpace == true) { rowMedian = (int)((float)table.rowSize / 2 - 0.5); colMedian = (int)((float)table.colSize / 2 - 0.5); } int squareWidth = sz.Width / table.colSize; int squareHeight; if (table.printTitle == true) { squareHeight = sz.Height / (table.rowSize + 1); //Adjust the square height if we have a title graphicsObj.DrawRectangle(myPen, pt.X, pt.Y, sz.Width, squareHeight); //Draw the title rectangle int titleNumChars = table.titleText.Length; int titleOneCharWidth = sz.Width / titleNumChars; //Figure out how much space one character of the title occupies char[] titleCharArray = table.titleText.ToCharArray(); int counter = 0; foreach (char item in titleCharArray) //For each character in the title, go through the loop { float itemFontSize = 150; //Starting font size Font itemFont = new Font(FontFamily.GenericMonospace, itemFontSize); SizeF itemSize = graphicsObj.MeasureString(item.ToString(), itemFont); //Initial graphic size while (((int)itemSize.Width > titleOneCharWidth) || ((int)itemSize.Height > squareHeight)) //Loop while the size is greater than the box size (titleOneCharWidth) { itemFontSize -= 1; if (itemFontSize <= 0) { itemFontSize = 1; break; } //Avoid badness itemFont = new Font(FontFamily.GenericMonospace, itemFontSize); //Decrease font size by 1 and then create new font item itemSize = graphicsObj.MeasureString(item.ToString(), itemFont); //Re-measure } //1) Decrease itemFontSize, re-measure the height and width, if new height and width are bigger than the box then repeat float offset = (titleOneCharWidth - itemSize.Width) / 2; //The offset is the amount I have to move the start point in order to center the text within the box (titleOneCharWidth) graphicsObj.DrawString(item.ToString(), itemFont, blackBrush, (float)pt.X + offset + counter * titleOneCharWidth, (float)(pt.Y + ((squareHeight - itemSize.Height)/2))); counter += 1; //Counter keeps track of which titleOneCharWidth box we are in. } pt.Y = pt.Y + squareHeight; //Move the Y point down one box size in order to allow for the title } //Square height needs to account for the title else { squareHeight = sz.Height / table.rowSize; } //Square height doesn't need to account for the title for (int rows = 0; rows < table.rowSize; rows++) { for (int cols = 0; cols < table.colSize; cols++) { int xPos = pt.X + cols * squareWidth; int yPos = pt.Y + rows * squareHeight; Rectangle temp = new Rectangle(xPos, yPos, squareWidth, squareHeight); // Rectangle rect2 = new Rectangle(pt.X, pt.Y, squareWidth, squareHeight); graphicsObj.DrawRectangle(myPen, temp); //TextTime! //At this point, we are in a box that has point x, y = (pt.X + cols * squareWidth), (pt.Y + rows * squareHeight) //The box has dimensions of squareWidth and squareHeight. //So, for each word passed to us we must //0) Split on the spaces if a input string is made up entirely of words //1) find the minimum size that will fit in the box //2) Find the vertical and horizontal center //3) Paint the word in the center of the box //This code will take the word list, randomize it, and print the first row * col items Random rand = new Random(table.RandSeed); string itemString = ""; int randomIndex = 0; if (data.Count > 0) { randomIndex = rand.Next() % data.Count; itemString = data[randomIndex]; } if ((table.printFreeSpace == true) && (rows == rowMedian) && (cols == colMedian)) { itemString = table.freeSpaceText; //itemString = itemString.Replace(' ', '\n'); thisIsAFreeSpace = true; } int itemFontSize = 15; Font itemFont = new Font(FontFamily.GenericMonospace, itemFontSize); SizeF itemSize = graphicsObj.MeasureString(itemString.ToString(), itemFont); //Initial graphic size while ((squareWidth < itemSize.Width) || (squareHeight < itemSize.Height)) { itemFontSize -= 1; if (itemFontSize <= 0) { itemFontSize = 1; break; } //We must seek to avoid badness in advance. Such is the way of the programmer. itemFont = new Font(FontFamily.GenericMonospace, itemFontSize); itemSize = graphicsObj.MeasureString(itemString.ToString(), itemFont); } //Now, we must find the center of the box so that we can put the text in a proper place //ItemY location is yPos + ((BoxHeight - StringHeight) / 2) //itemX location is xPos + ((BoxWidth - StringWidth) / 2) graphicsObj.DrawString(itemString, itemFont, blackBrush, (float)(xPos + (squareWidth - itemSize.Width) / 2), (float)(yPos + (squareHeight - itemSize.Height) / 2)); if ((thisIsAFreeSpace == true)) { thisIsAFreeSpace = false; } else if (data.Count > 0) { data.RemoveAt(randomIndex); } } } //graphicsObj.DrawRectangle(myPen, rect); //graphicsObj.DrawRectangle(myPen, rect2); }
public void printBingoCallList(Point pt, Size sz, BingoTable table, Color color, ref Graphics graphicsObj, BingoLicense licenseInfo, string Message, bool printMessage) { Font titleFont = new Font(FontFamily.GenericMonospace, 14, FontStyle.Bold); Font itemFont = new Font(FontFamily.GenericMonospace, 12); Brush blackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); string wordList = ""; foreach (string item in table.items) { wordList += item + "\n"; } if (printMessage == true) { wordList += "\n\n" + Message + "\n"; } //Draw title string listTitle = "Call List"; SizeF itemSize = graphicsObj.MeasureString(listTitle.ToString(), titleFont); Point titlePoint = new Point(pt.X + (int)((sz.Width - itemSize.Width) / 2), pt.Y); graphicsObj.DrawString(listTitle, titleFont, blackBrush, titlePoint); pt.Y += (int)(itemSize.Height + 0.5); //Draw words graphicsObj.DrawString(wordList, itemFont, blackBrush, pt); return; }
public bool BingoPrintDocument(BingoTable table) { bingoTable = table; printBingoDocument.DefaultPageSettings = PrintPageSettings; //Tell printDocument what the printer page settings are so when printDialog is called, these page settings will be updated for us to pick up later // printCallList.DefaultPageSettings = PrintPageSettings; printBingoDialog.Document = printBingoDocument; //Tell the print dialog what the print document is so it can show settings for it before printing numCardsToPrint = bingoTable.numCardsToPrint; //Tell the fcn how many cards to print if (printBingoDialog.ShowDialog() == DialogResult.OK) //Show the print dialog { try { printCallListOnNextPage = false; printBingoDocument.Print(); //Call print after the print document is closed with an OK // printCallList.Print(); } catch { MessageBox.Show("Unable to print!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return (true); } return (false); }