コード例 #1
0
        /// <summary>
        /// Print the selected table.
        /// </summary>
        public void Print()
        {
            // Used to get/set the grid cell height.
            int cellHeight = 0;

            try {
                // Set the left page margin.
                int leftMargin = page.MarginBounds.Left;

                // Holds the vertical distance from the top margin.
                int offsetY = page.MarginBounds.Top;

                // If the first page is being printed se the cell width and header height.
                if (firstPage)
                {
                    int leftDistance = leftMargin;
                    int tempWidth    = 0;

                    foreach (GridColumnDescriptor col in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
                    {
                        totalWidth += col.Width;
                    }

                    // Calculate and set the cell width and header height.
                    foreach (GridColumnDescriptor GridCol in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
                    {
                        tempWidth = (int)(Math.Floor(GridCol.Width /
                                                     (double)totalWidth * totalWidth *
                                                     (page.MarginBounds.Width / (double)totalWidth)));

                        headerHeight = (int)(page.Graphics.MeasureString(GridCol.HeaderText,
                                                                         headerFont, tempWidth).Height) + 11;

                        // Save width and height of headers.
                        headersX.Add(leftDistance);
                        columnWidths.Add(tempWidth);

                        leftDistance += tempWidth;
                    }
                }

                // Print all grid rows.
                while (rowCounter <= db.Tables[tableName].Rows.Count - 1)
                {
                    // Set the cell height.
                    cellHeight = databaseGrid.TableOptions.RecordRowHeight + 5;
                    int index = 0;

                    if (newPage)
                    {
                        #region Header & Page Number Drawing
                        // Draw the FEC logo in the page header.
                        page.Graphics.DrawImage(logo, new Point(50, 15));

                        // Draw the table name in the page header.
                        page.Graphics.DrawString(tableName,
                                                 headerFont,
                                                 Brushes.Black, page.MarginBounds.Left,
                                                 page.MarginBounds.Top - page.Graphics.MeasureString(tableName,
                                                                                                     headerFont,
                                                                                                     page.MarginBounds.Width).Height - 13);

                        // Draw the current date and time in the page header.
                        page.Graphics.DrawString(strDate,
                                                 headerFont, Brushes.Black,
                                                 page.MarginBounds.Left +
                                                 (page.MarginBounds.Width - page.Graphics.MeasureString(strDate,
                                                                                                        headerFont,
                                                                                                        page.MarginBounds.Width).Width),
                                                 page.MarginBounds.Top - page.Graphics.MeasureString(tableName,
                                                                                                     headerFont, page.MarginBounds.Width).Height - 13);

                        // If this is not the first page draw the page number.
                        if (!firstPage)
                        {
                            page.Graphics.DrawString(pageNumber.ToString(),
                                                     drawFont,
                                                     Brushes.Black,
                                                     pageWidth / 2,
                                                     pageHeight - page.MarginBounds.Top / 2);
                        }
                        #endregion

                        offsetY = page.MarginBounds.Top;

                        #region Table Column Drawing
                        // Draw table columns.
                        foreach (GridColumnDescriptor GridCol in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
                        {
                            // Draw table top.
                            page.Graphics.FillRectangle(new SolidBrush(Color.DeepSkyBlue),
                                                        new Rectangle((int)headersX[index], offsetY,
                                                                      (int)columnWidths[index], headerHeight));

                            // Draw table borders.
                            page.Graphics.DrawRectangle(Pens.SkyBlue,
                                                        new Rectangle((int)headersX[index], offsetY,
                                                                      (int)columnWidths[index], headerHeight));

                            // Draw column headers.
                            page.Graphics.DrawString(GridCol.HeaderText,
                                                     colHeaderFont,
                                                     new SolidBrush(Color.White),
                                                     new RectangleF((int)headersX[index], offsetY,
                                                                    (int)columnWidths[index], headerHeight), format);

                            index++;
                        }
                        #endregion

                        offsetY += headerHeight;
                        newPage  = false;
                    }
                    // Holds the x coordinate of current the cell to be printed.
                    int xValue = leftMargin;

                    // Holds the width of the current cell to be printed.
                    int width;

                    #region Table Column Content Drawing
                    //Draw the contents of the table columns.
                    for (; rowIndex < db.Tables[tableName].Rows.Count; rowIndex++)
                    {
                        for (int colIndex = 0; colIndex < databaseGrid.GetTable(tableName).TableDescriptor.Columns.Count; colIndex++)
                        {
                            width = (int)(Math.Floor(databaseGrid.GetTable(tableName).TableDescriptor.Columns[colIndex].Width /
                                                     (double)totalWidth * totalWidth *
                                                     (page.MarginBounds.Width / (double)totalWidth)));


                            // Draw cell contents.
                            page.Graphics.DrawString(db.Tables[tableName].Rows[rowCounter][colIndex].ToString(),
                                                     drawFont,
                                                     new SolidBrush(Color.Black),
                                                     new RectangleF(xValue,
                                                                    offsetY,
                                                                    width, cellHeight),
                                                     format);

                            // Draw cell borders.
                            page.Graphics.DrawRectangle(Pens.SkyBlue,
                                                        new Rectangle(xValue, offsetY,
                                                                      width, cellHeight));

                            // Add the current cell width to the xValue, so that next cell gets printed next to the current cell.
                            xValue += width;
                        }

                        // Reset the xValue.
                        xValue = leftMargin;

                        // Add to topMargin the current cellHeight so that the next row is printed below the current one.
                        offsetY += cellHeight;

                        rowCounter++;

                        // Check top see whether the current page settings allows more rows to print.
                        if (offsetY >= pageHeight - page.MarginBounds.Top)
                        {
                            newPage          = true;
                            morePagesToPrint = true;
                            rowIndex++;

                            // If this is the first page draw the page number.
                            if (firstPage)
                            {
                                page.Graphics.DrawString(pageNumber.ToString(),
                                                         drawFont,
                                                         Brushes.Black,
                                                         pageWidth / 2,
                                                         pageHeight - page.MarginBounds.Top / 2);
                            }

                            firstPage = false;

                            pageNumber++;

                            break;
                        }
                    }
                    #endregion

                    if (morePagesToPrint)
                    {
                        break;
                    }
                }

                // If more lines exist, print another page. Recalls the PrintPage event.
                if (morePagesToPrint && rowCounter != db.Tables[tableName].Rows.Count)
                {
                    page.HasMorePages = true;
                    morePagesToPrint  = false;
                }
                else
                {
                    page.HasMorePages = false;
                }
            }
            catch (Exception ex) {
                MessageBoxAdv.Show(ex.Message, "Error");
            }
        }
コード例 #2
0
        /// <summary>
        /// Print the selected table
        /// </summary>
        public void Print()
        {
            int  cellHeight   = 0;                          //Used to get/set the grid cell height
            int  totalWidth   = 0;
            int  rowCounter   = 0;                          //Used as a row counter
            bool isFirstPage  = true;                       // Used to check whether the first page is being printed
            bool isNewPage    = true;                       // Used to check whether a new page is being printed
            int  headerHeight = 0;                          // Used to store the header height

            StringFormat strFormat    = new StringFormat(); //Used to format the grid rows.
            ArrayList    columnsLeft  = new ArrayList();    //Used to save left coordinates of columns
            ArrayList    columnWidths = new ArrayList();    //Used to save column widths

            Font   drawFont   = databaseGrid.Font;
            Font   headerFont = new Font(drawFont, FontStyle.Bold);
            Bitmap image      = new Bitmap(Properties.Resources.fecLogo);


            strFormat.Alignment     = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;
            strFormat.Trimming      = StringTrimming.EllipsisCharacter;

            // Calculate total table width
            totalWidth = 0;
            foreach (GridColumnDescriptor col in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
            {
                totalWidth += col.Width;
            }

            try {
                // Set the left page margin
                int leftMargin = e.MarginBounds.Left;

                // Set the top page margin
                int topMargin = e.MarginBounds.Top;

                // Used to check whether there are more pages to print
                bool morePagesToPrint = false;

                int tempWidth = 0;

                // For the first page to be printed, set the cell width and header height
                if (isFirstPage)
                {
                    foreach (GridColumnDescriptor GridCol in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
                    {
                        tempWidth = (int)(Math.Floor(GridCol.Width /
                                                     (double)totalWidth * totalWidth *
                                                     (e.MarginBounds.Width / (double)totalWidth)));

                        headerHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      drawFont, tempWidth).Height) + 11;

                        // Save width and height of headers
                        columnsLeft.Add(leftMargin);
                        columnWidths.Add(tempWidth);
                        leftMargin += tempWidth;
                    }
                }

                // Print all grid rows
                while (rowCounter <= db.Tables[tableName].Rows.Count - 1)
                {
                    // Set the cell height
                    cellHeight = databaseGrid.TableOptions.RecordRowHeight + 5;
                    int iCount = 0;

                    // Check top see whether the current page settings allows more rows to print
                    if (topMargin + cellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        isNewPage        = true;
                        isFirstPage      = false;
                        morePagesToPrint = true;
                        break;
                    }
                    else
                    {
                        if (isNewPage)
                        {
                            e.Graphics.DrawImage(image, new Point(50, 15));

                            //Draw report header
                            e.Graphics.DrawString(tableName,
                                                  headerFont,
                                                  Brushes.Black, e.MarginBounds.Left,
                                                  e.MarginBounds.Top - e.Graphics.MeasureString(tableName,
                                                                                                headerFont,
                                                                                                e.MarginBounds.Width).Height - 13);

                            string strDate = DateTime.Now.ToLongDateString() + " " +
                                             DateTime.Now.ToShortTimeString();

                            // Draw the current date and time in the header
                            e.Graphics.DrawString(strDate,
                                                  headerFont, Brushes.Black,
                                                  e.MarginBounds.Left +
                                                  (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
                                                                                                   headerFont,
                                                                                                   e.MarginBounds.Width).Width),
                                                  e.MarginBounds.Top - e.Graphics.MeasureString(tableName,
                                                                                                headerFont, e.MarginBounds.Width).Height - 13);

                            // Draw table columns
                            topMargin = e.MarginBounds.Top;
                            foreach (GridColumnDescriptor GridCol in databaseGrid.GetTable(tableName).TableDescriptor.Columns)
                            {
                                // Draw table top
                                e.Graphics.FillRectangle(new SolidBrush(Color.DeepSkyBlue),
                                                         new Rectangle((int)columnsLeft[iCount], topMargin,
                                                                       (int)columnWidths[iCount], headerHeight));

                                // Draw table borders
                                e.Graphics.DrawRectangle(Pens.SkyBlue,
                                                         new Rectangle((int)columnsLeft[iCount], topMargin,
                                                                       (int)columnWidths[iCount], headerHeight));

                                // Draw column headers
                                e.Graphics.DrawString(GridCol.HeaderText,
                                                      drawFont,
                                                      new SolidBrush(Color.White),
                                                      new RectangleF((int)columnsLeft[iCount], topMargin,
                                                                     (int)columnWidths[iCount], headerHeight), strFormat);
                                iCount++;
                            }
                            isFirstPage = false;
                            topMargin  += headerHeight;
                        }

                        // Holds the x coordinate of current the cell to be printed
                        int xValue = (int)columnsLeft[0];

                        // Holds the width of the current cell to be printed
                        int width;

                        //Draw the contents of the table columns
                        for (int rowIndex = 0; rowIndex < db.Tables[tableName].Rows.Count; rowIndex++)
                        {
                            for (int colIndex = 0; colIndex < databaseGrid.GetTable(tableName).TableDescriptor.Columns.Count; colIndex++)
                            {
                                width = (int)(Math.Floor(databaseGrid.GetTable(tableName).TableDescriptor.Columns[colIndex].Width /
                                                         (double)totalWidth * totalWidth *
                                                         (e.MarginBounds.Width / (double)totalWidth)));


                                // Draw cell contents
                                e.Graphics.DrawString(db.Tables[tableName].Rows[rowCounter][colIndex].ToString(),
                                                      drawFont,
                                                      new SolidBrush(Color.Black),
                                                      new RectangleF(xValue,
                                                                     topMargin,
                                                                     width, cellHeight),
                                                      strFormat);

                                // Draw cell borders
                                e.Graphics.DrawRectangle(Pens.SkyBlue,
                                                         new Rectangle(xValue, topMargin,
                                                                       width, cellHeight));

                                // Add the current cell width to the xValue, so that next cell gets printed next to the current cell
                                xValue += width;
                            }

                            // Reset the xValue
                            xValue = (int)columnsLeft[0];

                            rowCounter++;

                            // Add to topMargin the current cellHeight so that the next row is printed below the current one
                            topMargin += cellHeight;
                        }
                    }
                }

                //I f more lines exist, print another page.
                if (morePagesToPrint)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                }
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }