コード例 #1
0
        public PrintGridItem(ExDataGridView dgv, bool isFit, PrintPageEventArgs e)
        {
            _grid           = dgv;
            _newPage        = true;
            _fitToPageWidth = true;
            _pageNo         = 1;
            _headerTitle    = String.Empty;
            _headerPrinter  = String.Empty;
            _rowPos         = 0;
            _rowsPerPage    = 0;
            _totalWidth     = 0;

            int CellWidth = 0;
            int PageLeft  = e.MarginBounds.Left;

            foreach (DataGridViewColumn GridCol in dgv.Columns)
            {
                if (!GridCol.Visible)
                {
                    continue;
                }

                _totalWidth += GridCol.Width;
            }

            foreach (DataGridViewColumn GridCol in dgv.Columns)
            {
                if (!GridCol.Visible)
                {
                    continue;
                }

                if (isFit)
                {
                    CellWidth = (int)(Math.Floor((double)((double)GridCol.Width / (double)TotalWidth * (double)TotalWidth * ((double)e.MarginBounds.Width / (double)TotalWidth))));
                }
                else
                {
                    CellWidth = GridCol.Width;
                }

                HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, CellWidth).Height) + 11;

                ColumnLefts.Add(PageLeft);
                ColumnWidths.Add(CellWidth);
                ColumnTypes.Add(GridCol.GetType());
                PageLeft += CellWidth;
            }
        }
コード例 #2
0
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // Skip if the current column not selected
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        // Save width & height of headres and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // Draw Header
                            e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font,
                                                                                                FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                            e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                                                                                        e.Graphics.MeasureString(s, new Font(dgv.Font,
                                                                                                                             FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
                                                                                                         FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            // Draw Columns
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                     (int)ColumnWidths[i], HeaderHeight), StrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                     (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                            }
                            // For the Button Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                           bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the ComboBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                             bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // For the Image Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                                  tmpTop, (int)ColumnWidths[i], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                   tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                   ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                            }

                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                               tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #3
0
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i, j;
            int eMarginBoundsTop   = e.MarginBounds.Top;
            int eMarginBoundsLeft  = e.MarginBounds.Left;
            int eMarginBoundsWidth = e.MarginBounds.Width;
            int tmpTop             = e.MarginBounds.Top;
            int tmpLeft            = e.MarginBounds.Left;

            string sTemp = "";

            try
            {
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    if (PrintXH) //有序号
                    {
                        eMarginBoundsLeft  += WidthXH;
                        eMarginBoundsWidth -= WidthXH;
                        TopXH    = e.MarginBounds.Top;
                        LeftXH   = e.MarginBounds.Left;
                        tmpLeft += WidthXH;
                    }
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // Skip if the current column not selected
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)eMarginBoundsWidth / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        // Save width & height of headres and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    //CellHeight = GridRow.Height;
                    CellHeight = GridRow.Height + 8;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // 单据汇总,表头
                            string[] sTitle = PrintTitle.Split(new char[1] {
                                ';'
                            });

                            for (j = 0; j < sTitle.Length; j++)
                            {
                                if (j <= 1)
                                {
                                    if (j == 0)
                                    {
                                        e.Graphics.DrawString(sTitle[j], _Font12, Brushes.Black, new System.Drawing.RectangleF(e.MarginBounds.Left, tmpTop, e.MarginBounds.Width, (int)(e.Graphics.MeasureString(sTitle[j], _Font12, e.MarginBounds.Width).Height)), StrFormat);
                                    }
                                    else
                                    {
                                        e.Graphics.DrawString(sTitle[j], _Font, Brushes.Black, new System.Drawing.RectangleF(e.MarginBounds.Left, tmpTop, e.MarginBounds.Width, (int)(e.Graphics.MeasureString(sTitle[j], _Font12, e.MarginBounds.Width).Height)), StrFormatR);
                                    }
                                }
                                else
                                {
                                    e.Graphics.DrawString(sTitle[j], _Font, Brushes.Black, e.MarginBounds.Left, tmpTop);
                                }

                                if (j == 0)
                                {
                                    continue;
                                }

                                if (j == 1)
                                {
                                    tmpTop += (int)(e.Graphics.MeasureString(sTitle[j], _Font12, e.MarginBounds.Width).Height);
                                }
                                else
                                {
                                    tmpTop += (int)(e.Graphics.MeasureString(sTitle[j], _Font, e.MarginBounds.Width).Height);
                                }
                            }

                            /*
                             * e.Graphics.DrawString(PrintTitle, new System.Drawing.Font(dgv.Font, FontStyle.Bold),
                             *      Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                             * e.Graphics.MeasureString(PrintTitle, new System.Drawing.Font(dgv.Font,
                             *      FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                             *
                             * String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
                             *
                             * //e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                             * //        Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                             * //        e.Graphics.MeasureString(s, new Font(dgv.Font,
                             * //        FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                             * //        e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
                             * //        FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                             *
                             * e.Graphics.DrawString(s, new System.Drawing.Font(_Font, FontStyle.Bold),
                             *      Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                             *      e.Graphics.MeasureString(s, new System.Drawing.Font(_Font,
                             *      FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                             *      e.Graphics.MeasureString(PrintTitle, new System.Drawing.Font(new System.Drawing.Font(_Font,
                             *      FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                             */

                            // Draw Columns
                            //tmpTop = e.MarginBounds.Top;
                            i = 0;

                            //表头
                            if (PrintXH) //有序号
                            {
                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new System.Drawing.Rectangle(LeftXH, tmpTop,
                                                                                      WidthXH, HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new System.Drawing.Rectangle(LeftXH, tmpTop,
                                                                                      WidthXH, HeaderHeight));

                                e.Graphics.DrawString("序号", _Font,
                                                      new SolidBrush(Color.Black),
                                                      new System.Drawing.RectangleF(LeftXH, tmpTop,
                                                                                    WidthXH, HeaderHeight), StrFormatC);
                            }
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new System.Drawing.Rectangle((int)ColumnLefts[i], tmpTop,
                                                                                      (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new System.Drawing.Rectangle((int)ColumnLefts[i], tmpTop,
                                                                                      (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new System.Drawing.RectangleF((int)ColumnLefts[i], tmpTop,
                                                                                    (int)ColumnWidths[i], HeaderHeight), StrFormatC);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents
                        if (PrintXH) //有序号
                        {
                            e.Graphics.DrawString((RowPos + 1).ToString(), _Font, new SolidBrush(Color.Black), new RectangleF(LeftXH, (float)tmpTop, WidthXH, (float)CellHeight), StrFormatC);
                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new System.Drawing.Rectangle(LeftXH, tmpTop, WidthXH, CellHeight));
                        }
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                if (PrintWarn)
                                {
                                    //e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                    //        new SolidBrush(Cel.InheritedStyle.ForeColor),
                                    //        new RectangleF((int)ColumnLefts[i], (decimal)tmpTop,
                                    //        (int)ColumnWidths[i], (decimal)CellHeight), StrFormat);

                                    //确定背景
                                    if (Cel.Style.BackColor == Color.LightPink)
                                    {
                                        e.Graphics.FillRectangle(new SolidBrush(Color.LightPink), new System.Drawing.Rectangle((int)ColumnLefts[i], (int)tmpTop, (int)ColumnWidths[i], (int)CellHeight));
                                    }
                                }

                                if (Cel.Value.GetType() == typeof(System.DateTime))
                                {
                                    if (Cel.Value.ToString() != "")
                                    {
                                        sTemp = Convert.ToDateTime(Cel.Value.ToString()).ToString("yyyy年M月dd日");
                                    }
                                    else
                                    {
                                        sTemp = "";
                                    }

                                    e.Graphics.DrawString(sTemp, _Font, new SolidBrush(Cel.InheritedStyle.ForeColor), new RectangleF((int)ColumnLefts[i], (float)tmpTop, (int)ColumnWidths[i], (float)CellHeight), StrFormatC);
                                }
                                else
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString(), _Font,
                                                          new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                          new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                         (int)ColumnWidths[i], (float)CellHeight), StrFormatC);
                                }
                            }
                            // For the Button Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0,
                                                                                          bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new System.Drawing.Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new System.Drawing.Rectangle(0, 0,
                                                                                                      bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new System.Drawing.Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                                       (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                                       CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new System.Drawing.Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the ComboBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0,
                                                                                            bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new System.Drawing.Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // For the Image Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                System.Drawing.Rectangle CelSize = new System.Drawing.Rectangle((int)ColumnLefts[i],
                                                                                                tmpTop, (int)ColumnWidths[i], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new System.Drawing.Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                                  tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                                  ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                            }

                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new System.Drawing.Rectangle((int)ColumnLefts[i],
                                                                                              tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #4
0
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;

            tmpTop = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    int extra_width = (e.MarginBounds.Width - TotalWidth) / AvailableColumns.Count;
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // Skip if the current column not selected
                        //if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;

                        tmpWidth     = GridCol.Width + extra_width;
                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 12;

                        // Save width & height of headers and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow)
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        print_footer(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // Draw Header
                            print_title(e);

                            // Draw Columns
                            print_column_header(e);
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            //if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            //continue;

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                string str = Cel.Value == null?"":Cel.Value.ToString();
                                e.Graphics.DrawString(str, new Font("SimSun", font_size),
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)(ColumnLefts[i]), (float)tmpTop,
                                                                     (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                                //e.Graphics.DrawString(str, new Font("SimSun", font_size),
                                //      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                //      new RectangleF((int)(ColumnLefts[i]), (float)tmpTop,
                                //      (int)ColumnWidths[i], (float)HeaderHeight), StrFormat);
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }

                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                               tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                print_footer(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #5
0
ファイル: PrintDGV.cs プロジェクト: sinyu1012/CNT-DEMO1
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop = e.MarginBounds.Top;
            //int tmpLeft = e.MarginBounds.Left;

            int tmpLeft = 30;//页面的左边距

            try
            {
                // 从第一页开始,之前保存标题和CoulmnType的宽度和高度
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // 如果当前的列不选择跳过
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        //保存headres和ColumnType的宽度和高度

                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                //打印当前页,行
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            //画头
                            e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font,
                                                                                                FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                            e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                                                                                        e.Graphics.MeasureString(s, new Font(dgv.Font,
                                                                                                                             FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
                                                                                                         FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            // 画列
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                     (int)ColumnWidths[i], HeaderHeight), StrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // 画列内容
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                     (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                            }
                            // For the Button Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                           bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(0, 0);
                                CellCheckBox.Checked = (bool)Cel.Value;

                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the ComboBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                             bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // For the Image Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                                  tmpTop, (int)ColumnWidths[i], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                e.Graphics.SmoothingMode     = SmoothingMode.HighQuality;
                                e.Graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2) - 50, //X
                                                                   tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),                 //Y
                                                                   ((Image)(Cel.FormattedValue)).Width + 100,                             //宽
                                                                   ((Image)(Cel.FormattedValue)).Height));                                //高
                            }

                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                               tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // 计算第一页每页行数
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // 写页脚(页码)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #6
0
        private static void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                // Antes de indicar a primeira página, grava a altura e largura do cabeçalho e o tipo de coluna
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }

                        // Ignora a coluna que não está seleccionada
                        if (!printDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Determina se as colunas estão ajustadas à página
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width / (double)TotalWidth * (double)TotalWidth * ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Imprime a página linha por linha
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];

                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // Desenha o cabeçalho
                            e.Graphics.DrawString(PrintTitle,
                                                  new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black,
                                                  e.MarginBounds.Left,
                                                  e.MarginBounds.Top - e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                            e.Graphics.DrawString(s,
                                                  new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black,
                                                  e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(s, new Font(dgv.Font, FontStyle.Bold), e.MarginBounds.Width).Width),
                                                  e.MarginBounds.Top - e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            // Desenha as colunas
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;

                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!printDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.SteelBlue), new Rectangle((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, new SolidBrush(GridCol.InheritedStyle.ForeColor), new RectangleF((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], HeaderHeight), StrFormat);

                                i++;
                            }

                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Desenha o conteúdo das colunas
                        i = 0;

                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            // Para a coluna da TextBox
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" || ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, new SolidBrush(Cel.InheritedStyle.ForeColor), new RectangleF((int)ColumnLefts[i], (float)tmpTop, (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                            }

                            // Para a coluna do botão
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // Para a coluna do CheckBox
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp, new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2), (int)((bmp.Height - CellCheckBox.Height) / 2), CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // Para a coluna do ComboBox
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, new SolidBrush(Cel.InheritedStyle.ForeColor), new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i] - 16, CellHeight), StrFormatComboBox);
                            }

                            // Desenha as margens das células
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }

                        tmpTop += CellHeight;
                    }

                    RowPos++;

                    // Se for a primeira página, calcula o número de linhas por página
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Desenha rodapé (número de página)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #7
0
ファイル: PrintDGV.cs プロジェクト: lnxmxy/IntelligentPress
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop    = e.MarginBounds.Top;
            int lbctmpTop = e.MarginBounds.Top;
            int tmpLeft   = e.MarginBounds.Left;

            try
            {
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // Skip if the current column not selected
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 35;

                        // Save width & height of headres and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }
                //计算每页显示行数
                while (lbcRowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow1 = dgv.Rows[lbcRowPos];
                    if (GridRow1.IsNewRow || (!PrintAllRows && !GridRow1.Selected))
                    {
                        lbcRowPos++;
                        continue;
                    }

                    CellHeight = GridRow1.Height * 2;

                    if (lbctmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        lbcNewPage = true;
                        lbcPageNo++;
                        e.HasMorePages = true;
                    }
                    else
                    {
                        if (lbcNewPage)
                        {
                            lbctmpTop  = e.MarginBounds.Top;
                            lbcNewPage = false;
                            lbctmpTop += HeaderHeight;
                        }
                        lbctmpTop += CellHeight;
                    }

                    lbcRowPos++;
                    if (lbcPageNo == 1)
                    {
                        lbcRowsPerPage++;
                    }
                }
                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height * 2;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // Draw Header
                            e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font,
                                                                                                FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            //String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                            //e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                            //        Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                            //        e.Graphics.MeasureString(s, new Font(dgv.Font,
                            //        FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                            //        e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
                            //        FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            // Draw Columns
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                     (int)ColumnWidths[i], HeaderHeight), StrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents 打印时合并单元格
                        switch (MergeNum)
                        {
                        case 4:
                            #region 合并单元格
                            i = 0;
                            foreach (DataGridViewCell Cel in GridRow.Cells)
                            {
                                if (!Cel.OwningColumn.Visible)
                                {
                                    continue;
                                }
                                if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                                {
                                    continue;
                                }

                                #region 合并第一列
                                if ((Cel.ColumnIndex == 0) && Cel.RowIndex != -1)
                                {
                                    //当前单元格与下一行单元格内容不相同时,画左竖线、下横线和右竖线
                                    if (Cel.RowIndex < dgv.Rows.Count - 1 && dgv.Rows[Cel.RowIndex + 1].Cells[Cel.ColumnIndex].Value.ToString() != Cel.Value.ToString())
                                    {
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop + CellHeight, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop, (int)ColumnLefts[i], tmpTop + CellHeight);
                                    }
                                    //当前单元格与下一行单元格内容相同时,画左竖线和右竖线
                                    else
                                    {
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop, (int)ColumnLefts[i], tmpTop + CellHeight);
                                    }
                                    //当前单元格为每页的最后行时,画下横线
                                    if ((Cel.RowIndex + 1) % lbcRowsPerPage == 0 || Cel.RowIndex == dgv.Rows.Count - 1)
                                    {
                                        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop + CellHeight, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                    }
                                    //当前单元格与下一行单元格内容相同时,不填写文本
                                    if (Cel.RowIndex > 0 && dgv.Rows[Cel.RowIndex - 1].Cells[Cel.ColumnIndex].Value.ToString() == Cel.Value.ToString())
                                    {
                                        i++;
                                        continue;
                                    }
                                }
                                #endregion

                                //#region 根据第一列合并其它列
                                //else if ((Cel.ColumnIndex == 1 || Cel.ColumnIndex == 2 || Cel.ColumnIndex == 3 || Cel.ColumnIndex == 4) && Cel.RowIndex != -1)
                                //{
                                //    //当前单元格与下一行单元格内容不相同时,画下横线和右竖线
                                //    if (Cel.RowIndex < dgv.Rows.Count - 1 && dgv.Rows[Cel.RowIndex + 1].Cells[0].Value.ToString() != dgv.Rows[Cel.RowIndex].Cells[0].Value.ToString())
                                //    {
                                //        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop + CellHeight, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                //        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                //    }
                                //    //当前单元格与下一行单元格内容相同时,画右竖线
                                //    else
                                //    {
                                //        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                //    }
                                //    //当前单元格为每页的最后行时,画下横线
                                //    if ((Cel.RowIndex + 1) % lbcRowsPerPage == 0 || Cel.RowIndex == dgv.Rows.Count - 1)
                                //    {
                                //        e.Graphics.DrawLine(Pens.Black, (int)ColumnLefts[i], tmpTop + CellHeight, (int)ColumnLefts[i] + (int)ColumnWidths[i], tmpTop + CellHeight);
                                //    }
                                //    //当前单元格与下一行单元格内容相同时,不填写文本
                                //    if (Cel.RowIndex > 0 && dgv.Rows[Cel.RowIndex - 1].Cells[0].Value.ToString() == dgv.Rows[Cel.RowIndex].Cells[0].Value.ToString())
                                //    {
                                //        i++;
                                //        continue;
                                //    }
                                //}
                                //#endregion

                                #region 非合并列
                                else
                                {
                                    e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], CellHeight));
                                }
                                #endregion

                                // For the TextBox Column
                                if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                    ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString().Replace("\n", ""), Cel.InheritedStyle.Font,
                                                          new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                          new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                         (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                                }
                                i++;
                            }
                            #endregion
                            break;

                        default:
                            #region 非合并单元格
                            i = 0;
                            foreach (DataGridViewCell Cel in GridRow.Cells)
                            {
                                if (!Cel.OwningColumn.Visible)
                                {
                                    continue;
                                }
                                if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                                {
                                    continue;
                                }

                                // For the TextBox Column
                                if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                    ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString().Replace("\n", ""), Cel.InheritedStyle.Font,
                                                          new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                          new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                         (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                                }
                                #region 其它形式单元格
                                //// For the Button Column
                                //else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                                //{
                                //    CellButton.Text = Cel.Value.ToString();
                                //    CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                //    Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                //    CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                //            bmp.Width, bmp.Height));
                                //    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                //}
                                //// For the CheckBox Column
                                //else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                                //{
                                //    CellCheckBox.Size = new Size(14, 14);
                                //    CellCheckBox.Checked = (bool)Cel.Value;
                                //    Bitmap bmp = new Bitmap((int)ColumnWidths[i], CellHeight);
                                //    Graphics tmpGraphics = Graphics.FromImage(bmp);
                                //    tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                //            bmp.Width, bmp.Height));
                                //    CellCheckBox.DrawToBitmap(bmp,
                                //            new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                //            (int)((bmp.Height - CellCheckBox.Height) / 2),
                                //            CellCheckBox.Width, CellCheckBox.Height));
                                //    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                //}
                                //// For the ComboBox Column
                                //else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                                //{
                                //    CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                //    Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                //    CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                //            bmp.Width, bmp.Height));
                                //    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                //    e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                //            new SolidBrush(Cel.InheritedStyle.ForeColor),
                                //            new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                //            - 16, CellHeight), StrFormatComboBox);
                                //}
                                //// For the Image Column
                                //else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                                //{
                                //    Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                //            tmpTop, (int)ColumnWidths[i], CellHeight);
                                //    Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                //    e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                //            new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                //            tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                //            ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));

                                //}
                                #endregion
                                // Drawing Cells Borders
                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                                   tmpTop, (int)ColumnWidths[i], CellHeight));

                                i++;
                            }
                            #endregion
                            break;
                        }

                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #8
0
ファイル: Theme.cs プロジェクト: masums/NetCoreGUI
        private void DrawGridCol(GridCol control)
        {
            var colProp = control.GetProperties(this);

            GraphicsContext.DrawRect(colProp.Position.x, colProp.Position.y, colProp.Size.Width, colProp.Size.Height, colProp.BackColor);
        }
コード例 #9
0
        private void PrintDoc_PrintPage(object sender,
                                        System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;

            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                double pageWidth = e.PageBounds.Width - e.MarginBounds.Left * 2;
                if (pageWidth < 0)
                {
                    MessageBox.Show(PromptMessageXmlHelper.Instance.GetPromptMessage("printlist", EnumPromptMessage.warning), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    _printPreviewDialog.Close();
                    return;
                }
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            ColumnWidths.Add(GridCol.Name, 0);
                            continue;
                        }
                        // Skip if the current column not selected
                        if (!SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            ;
                            if (pageWidth < TotalWidth)
                            {
                                tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                     (double)TotalWidth * (double)pageWidth)));
                            }
                            else
                            {
                                tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                     (double)TotalWidth * (double)TotalWidth *
                                                                     ((double)e.MarginBounds.Width / (double)TotalWidth))));
                            }
                        }
                        else
                        {
                            if (pageWidth < TotalWidth)
                            {
                                tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                     (double)TotalWidth * (double)pageWidth)));
                            }
                            else
                            {
                                tmpWidth = GridCol.Width;
                            }
                        }
                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 20;

                        // Save width & height of headres and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(GridCol.Name, tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }
                    // CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        Font font = new Font(dgv.Font,
                                             FontStyle.Bold);
                        if (NewPage)
                        {
                            tmpTop = e.MarginBounds.Top;

                            // Draw Header
                            //这里开始渲染标题
                            SizeF titleSize = e.Graphics.MeasureString(PrintTitle, TitleFont, e.MarginBounds.Width);
                            e.Graphics.DrawString(PrintTitle, TitleFont,
                                                  FontColor, (int)((pageWidth + e.MarginBounds.Left * 2 - titleSize.Width) / 2), e.MarginBounds.Top -
                                                  titleSize.Height);

                            int topH = 0;
                            // 用于记录BarCode有传入的情况下,记录高度
                            Size barCodeSize = new Size(300, 80);

                            if (!string.IsNullOrEmpty(barCodeValue))
                            {
                                //渲染出BarCode在界面上
                                Image barImage = BarCodeHelper.GetBarcodeImage(barCodeValue, barCodeValue, 300, 80);
                                barCodeSize = barImage.Size;
                                e.Graphics.DrawImage(barImage, (int)((pageWidth + e.MarginBounds.Left * 2 - barCodeSize.Width) / 2), tmpTop, barCodeSize.Width, barCodeSize.Height);

                                tmpTop += barCodeSize.Height;
                            }

                            if (PageNo == 1)
                            {
                                //显示内容部分
                                if (!string.IsNullOrEmpty(PrintContent))
                                {
                                    SizeF contentSize = e.Graphics.MeasureString(PrintContent, ConFont, e.MarginBounds.Width);
                                    e.Graphics.DrawString(PrintContent, ConFont,
                                                          ConFontColor, e.MarginBounds.Left - 18, tmpTop);

                                    tmpTop += (int)contentSize.Height;
                                }
                            }

                            string printTime = string.Format("时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                            SizeF  timeSize  = e.Graphics.MeasureString(printTime, ConFont, e.MarginBounds.Width);
                            e.Graphics.DrawString(printTime, ConFont,
                                                  ConFontColor, (int)(pageWidth + e.MarginBounds.Left * 2 - e.MarginBounds.Left - timeSize.Width - 10), tmpTop);
                            tmpTop += (int)timeSize.Height;

                            // Draw Columns
                            i = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[GridCol.Name], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[GridCol.Name], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, DgvFont,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                     (int)ColumnWidths[GridCol.Name], HeaderHeight), StrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            int j = 0;
                            foreach (DataGridViewCell cel in GridRow.Cells)
                            {
                                SizeF maxSize = new SizeF((int)ColumnWidths[cel.OwningColumn.Name], CellHeight);

                                string celValue = cel.Value != null?cel.Value.ToString() : "";

                                SizeF celSize = e.Graphics.MeasureString(celValue, DgvFont, e.MarginBounds.Width);

                                double celHeight = celSize.Height * Math.Ceiling((double)(celSize.Width / maxSize.Width));
                                if (maxSize.Height < Math.Ceiling(celHeight) && !double.IsInfinity(celHeight))
                                {
                                    maxSize    = new SizeF(maxSize.Width, (float)Math.Ceiling(celHeight));
                                    CellHeight = (int)maxSize.Height + 10;
                                }

                                j++;
                            }
                            string cellValue = Cel.Value != null?Cel.Value.ToString() : "";

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                e.Graphics.DrawString(cellValue, DgvFont,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                     (int)ColumnWidths[Cel.OwningColumn.Name], (int)CellHeight), StrFormat);
                            }
                            // For the Button Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = cellValue;
                                CellButton.Size = new Size((int)ColumnWidths[Cel.OwningColumn.Name], (int)CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                           bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Boolean.Parse(cellValue);
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[Cel.OwningColumn.Name], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the ComboBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[Cel.OwningColumn.Name], (int)CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, (int)CellHeight);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                             bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(cellValue, Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[Cel.OwningColumn.Name]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // For the Image Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                                  tmpTop, (int)ColumnWidths[Cel.OwningColumn.Name], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                   tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                   ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                            }
                            // For the Decimal
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewDecimalColumn")
                            {
                                e.Graphics.DrawString(cellValue, DgvFont,
                                                      new SolidBrush(Color.Black),
                                                      new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                     (int)ColumnWidths[Cel.OwningColumn.Name], (int)CellHeight), StrFormat);
                            }
                            // Drawing Cells Borders
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                               tmpTop, (int)ColumnWidths[Cel.OwningColumn.Name], (int)CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #10
0
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                // Before starting first page, it saves Width & Height of Headers and CoulmnType
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // Skip if the current column not selected
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }

                        // Detemining whether the columns are fitted to page or not.
                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        // Save width & height of headres and ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // Printing Current Page, Row by Row
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // Draw Header
                            Font  font  = new Font(dgv.Font, FontStyle.Bold);
                            Brush brush = Brushes.Black;
                            int   pageW = e.MarginBounds.Width;
                            SizeF size  = e.Graphics.MeasureString(PrintTitle, font, pageW);
                            float X     = e.MarginBounds.Left;
                            if (pageW > size.Width)
                            {
                                X += (pageW - size.Width) / 2;
                            }
                            float Y = e.MarginBounds.Top - e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13;

                            //Busca Dados do Registro
                            EMPRESAProvider EMPRESAP  = new EMPRESAProvider();
                            EMPRESAEntity   EMPRESATy = new EMPRESAEntity();
                            EMPRESATy = EMPRESAP.Read(1);
                            string CPFCNPJ = EMPRESATy.CNPJCPF.Length > 15 ? " CNPJ: " : " CPF: ";
                            e.Graphics.DrawString(EMPRESATy.NOMECLIENTE + CPFCNPJ + EMPRESATy.CNPJCPF, font, brush, 30, Y - 20);

                            e.Graphics.DrawString(PrintTitle, font, brush, X, Y);
                            //e.Graphics.DrawString(PrintTitle, font, brush, X, Y, HdrFormat);


                            if (mbPrintDate)
                            {
                                String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                                e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                                                      Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                                                                                            e.Graphics.MeasureString(s, new Font(dgv.Font,
                                                                                                                                 FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                                                      e.Graphics.MeasureString(PrintTitle + "3", new Font(new Font(dgv.Font,
                                                                                                                   FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                            }
                            // Draw Columns
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }

                                if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }



                                if (PrintBorder)
                                {
                                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                             new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                           (int)ColumnWidths[i], HeaderHeight));

                                    e.Graphics.DrawRectangle(Pens.Black,
                                                             new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                           (int)ColumnWidths[i], HeaderHeight));
                                }

                                e.Graphics.DrawString(GridCol.HeaderText
                                                      , GridCol.InheritedStyle.Font
                                                      , new SolidBrush(GridCol.InheritedStyle.ForeColor)
                                                      , new RectangleF((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], HeaderHeight)
                                                      , HdrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // Draw Columns Contents
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            // For the TextBox Column
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                //Rafael 19/02/2013 - Condição para alinhamento na visualização
                                if (Cel.InheritedStyle.Alignment.ToString() == "MiddleLeft")
                                {
                                    StrFormat.Alignment = StringAlignment.Near;
                                }
                                else if (Cel.InheritedStyle.Alignment.ToString() == "MiddleRight")
                                {
                                    StrFormat.Alignment = StringAlignment.Far;
                                }
                                else if (Cel.InheritedStyle.Alignment.ToString() == "MiddleCenter")
                                {
                                    StrFormat.Alignment = StringAlignment.Center;
                                }

                                if (Cel.Value != null)
                                {
                                    e.Graphics.DrawString(Cel.EditedFormattedValue.ToString(), Cel.InheritedStyle.Font,
                                                          new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                          new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                         (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                                }
                            }
                            // For the Button Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                           bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the CheckBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // For the ComboBox Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                             bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // For the Image Column
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                                  tmpTop, (int)ColumnWidths[i], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                   tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                   ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                            }

                            // Drawing Cells Borders
                            if (PrintBorder)
                            {
                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                                   tmpTop, (int)ColumnWidths[i], CellHeight));
                            }


                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // For the first page it calculates Rows per Page
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // Write Footer (Page Number)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;

                //Imprimir somatorio
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #11
0
        private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            // Before starting first page, it saves Width & Height of Headers and CoulmnType
            if (PageNo == 1)
            {
                foreach (DataGridViewColumn GridCol in dgv.Columns)
                {
                    if (!GridCol.Visible)
                    {
                        continue;
                    }
                    // Skip if the current column not selected
                    if (!this.m_SelectedColumns.Contains(GridCol.HeaderText))
                    {
                        continue;
                    }
                    // Detemining whether the columns are fitted to page or not.
                    if (m_FitToPageWidth)
                    {
                        tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                             (double)TotalWidth * (double)TotalWidth *
                                                             ((double)e.MarginBounds.Width / (double)TotalWidth))));
                    }
                    else
                    {
                        tmpWidth = GridCol.Width;
                    }

                    int tempHH = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
                    if (HeaderHeight < tempHH)
                    {
                        HeaderHeight = tempHH;
                    }
                    // Save width & height of headres and ColumnType
                    ColumnLefts.Add(tmpLeft);
                    ColumnWidths.Add(tmpWidth);
                    ColumnTypes.Add(GridCol.GetType());
                    tmpLeft += tmpWidth;
                }
            }
            // Printing Current Page, Row by Row
            while (RowPos <= dgv.Rows.Count - 1)
            {
                DataGridViewRow GridRow = dgv.Rows[RowPos];
                if (GridRow.IsNewRow || (!m_PrintAllRows && !GridRow.Selected))
                {
                    RowPos++;
                    continue;
                }
                CellHeight = GridRow.Height;
                if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                {
                    DrawFooter(e, RowsPerPage);
                    NewPage = true;
                    PageNo++;
                    e.HasMorePages = true;
                    return;
                }
                else
                {
                    if (NewPage)
                    {
                        // Draw Header
                        e.Graphics.DrawString(m_PrintTitle, this.HeaderFont, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                              e.Graphics.MeasureString(m_PrintTitle, this.HeaderFont, e.MarginBounds.Width).Height - 13);
                        String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
                        e.Graphics.DrawString(s, this.HeaderFont,
                                              Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                                                                                    e.Graphics.MeasureString(s, this.HeaderFont, e.MarginBounds.Width).Width), e.MarginBounds.Top -
                                              e.Graphics.MeasureString(m_PrintTitle, this.HeaderFont, e.MarginBounds.Width).Height - 13);
                        // Draw Columns
                        tmpTop = e.MarginBounds.Top;
                        i      = 0;
                        foreach (DataGridViewColumn GridCol in dgv.Columns)
                        {
                            if (!GridCol.Visible)
                            {
                                continue;
                            }
                            if (!this.m_SelectedColumns.Contains(GridCol.HeaderText))
                            {
                                continue;
                            }
                            e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro),
                                                     new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                   (int)ColumnWidths[i], HeaderHeight));
                            e.Graphics.DrawRectangle(Pens.Gray,
                                                     new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                   (int)ColumnWidths[i], HeaderHeight));
                            e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                  new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                  new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                 (int)ColumnWidths[i], HeaderHeight), StrFormat);
                            i++;
                        }
                        NewPage = false;
                        tmpTop += HeaderHeight;
                    }
                    // Draw Columns Contents
                    i = 0;
                    foreach (DataGridViewCell cell in GridRow.Cells)
                    {
                        if (cell.Visible)
                        {
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" || ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                string value = String.Empty;
                                if (cell.Value is DateTime && String.IsNullOrEmpty(this.DateTimeStringFormat) == false)
                                {
                                    value = ((DateTime)cell.Value).ToString(this.DateTimeStringFormat);
                                }
                                else
                                {
                                    value = (cell.Value ?? cell.InheritedStyle.NullValue).ToString();
                                    if (String.IsNullOrEmpty(cell.InheritedStyle.Format) == false)
                                    {
                                        value = String.Format("{0:" + cell.InheritedStyle.Format + "}", cell.Value ?? cell.InheritedStyle.NullValue);
                                    }
                                }
                                SizeF tempSF = e.Graphics.MeasureString(value, this.BodyFont, (int)ColumnWidths[i]);
                                if (tempSF.Height > CellHeight)
                                {
                                    CellHeight = (int)tempSF.Height;
                                }
                            }
                            i++;
                        }
                    }
                    i = 0;
                    foreach (DataGridViewCell cell in GridRow.Cells)
                    {
                        if (!cell.OwningColumn.Visible)
                        {
                            continue;
                        }
                        if (!m_SelectedColumns.Contains(cell.OwningColumn.HeaderText))
                        {
                            continue;
                        }
                        // For the TextBox Column
                        if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" || ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                        {
                            string value = String.Empty;
                            if (cell.Value is DateTime && String.IsNullOrEmpty(this.DateTimeStringFormat) == false)
                            {
                                value = ((DateTime)cell.Value).ToString(this.DateTimeStringFormat);
                            }
                            else
                            {
                                value = (cell.Value ?? cell.InheritedStyle.NullValue).ToString();
                                if (String.IsNullOrEmpty(cell.InheritedStyle.Format) == false)
                                {
                                    value = String.Format("{0:" + cell.InheritedStyle.Format + "}", cell.Value ?? cell.InheritedStyle.NullValue);
                                }
                            }
                            e.Graphics.DrawString(value, this.BodyFont,
                                                  new SolidBrush(cell.InheritedStyle.ForeColor),
                                                  new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                 (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                        }
                        // For the Button Column
                        else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                        {
                            CellButton.Text = (cell.Value ?? "").ToString();
                            CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                            Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                            CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                       bmp.Width, bmp.Height));
                            e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                        }
                        // For the CheckBox Column
                        else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                        {
                            CellCheckBox.Size = new Size(14, 14);
                            if (cell.Value != null)
                            {
                                CellCheckBox.Checked = (bool)cell.Value;
                            }
                            else
                            {
                                CellCheckBox.Checked = false;
                            }
                            Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                            Graphics tmpGraphics = Graphics.FromImage(bmp);
                            tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                   bmp.Width, bmp.Height));
                            CellCheckBox.DrawToBitmap(bmp,
                                                      new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                    (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                    CellCheckBox.Width, CellCheckBox.Height));
                            e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                        }
                        // For the ComboBox Column
                        else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                        {
                            CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                            Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                            CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                         bmp.Width, bmp.Height));
                            e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            e.Graphics.DrawString(cell.Value.ToString(), this.BodyFont,
                                                  new SolidBrush(cell.InheritedStyle.ForeColor),
                                                  new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                 - 16, CellHeight), StrFormatComboBox);
                        }
                        // For the Image Column
                        else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                        {
                            Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                              tmpTop, (int)ColumnWidths[i], CellHeight);
                            Size ImgSize = ((Image)(cell.FormattedValue)).Size;
                            e.Graphics.DrawImage((Image)cell.FormattedValue,
                                                 new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                               tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                               ((Image)(cell.FormattedValue)).Width, ((Image)(cell.FormattedValue)).Height));
                        }
                        // Drawing Cells Borders
                        e.Graphics.DrawRectangle(Pens.Gray, new Rectangle((int)ColumnLefts[i], tmpTop, (int)ColumnWidths[i], CellHeight));
                        i++;
                    }
                    tmpTop += CellHeight;
                }
                RowPos++;
                // For the first page it calculates Rows per Page
                if (PageNo == 1)
                {
                    RowsPerPage++;
                }
            }
            if (RowsPerPage == 0)
            {
                return;
            }
            // Write Footer (Page Number)
            DrawFooter(e, RowsPerPage);
            e.HasMorePages = false;
        }
コード例 #12
0
ファイル: DocumentPrint.cs プロジェクト: hezhangxi/rms
        private static void PrintDoc_PrintPage(object sender,
                                               System.Drawing.Printing.PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop  = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;

            try
            {
                // 在开始的第一页,这样可以节省的宽度和高度页眉和CoulmnType
                if (PageNo == 1)
                {
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible)
                        {
                            continue;
                        }
                        // 跳过如果当前列未选中
                        if (!DocumentPrint.SelectedColumns.Contains(GridCol.HeaderText))
                        {
                            continue;
                        }


                        if (FitToPageWidth)
                        {
                            tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                                                 (double)TotalWidth * (double)TotalWidth *
                                                                 ((double)e.MarginBounds.Width / (double)TotalWidth))));
                        }
                        else
                        {
                            tmpWidth = GridCol.Width;
                        }

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                                                      GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;

                        // 保存的宽度与高度headres和ColumnType
                        ColumnLefts.Add(tmpLeft);
                        ColumnWidths.Add(tmpWidth);
                        ColumnTypes.Add(GridCol.GetType());
                        tmpLeft += tmpWidth;
                    }
                }

                // 打印(&P)当前页,由一排排
                while (RowPos <= dgv.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = dgv.Rows[RowPos];
                    if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                    {
                        RowPos++;
                        continue;
                    }

                    CellHeight = GridRow.Height;

                    if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(e, RowsPerPage);
                        NewPage = true;
                        PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (NewPage)
                        {
                            // 绘制标题
                            e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font,
                                                                                                FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();

                            e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
                                                  Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                                                                                        e.Graphics.MeasureString(s, new Font(dgv.Font,
                                                                                                                             FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                                                  e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
                                                                                                         FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            // 绘制列
                            tmpTop = e.MarginBounds.Top;
                            i      = 0;
                            foreach (DataGridViewColumn GridCol in dgv.Columns)
                            {
                                if (!GridCol.Visible)
                                {
                                    continue;
                                }
                                if (!DocumentPrint.SelectedColumns.Contains(GridCol.HeaderText))
                                {
                                    continue;
                                }

                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                                         new Rectangle((int)ColumnLefts[i], tmpTop,
                                                                       (int)ColumnWidths[i], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                                      new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], tmpTop,
                                                                     (int)ColumnWidths[i], HeaderHeight), StrFormat);
                                i++;
                            }
                            NewPage = false;
                            tmpTop += HeaderHeight;
                        }

                        // 绘制列内容
                        i = 0;
                        foreach (DataGridViewCell Cel in GridRow.Cells)
                        {
                            if (!Cel.OwningColumn.Visible)
                            {
                                continue;
                            }
                            if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                            {
                                continue;
                            }

                            //为TextBox列
                            if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                            {
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                                                     (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                            }
                            //为 Button 列
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                            {
                                CellButton.Text = Cel.Value.ToString();
                                CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                           bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            // 为 CheckBox 列
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                            {
                                CellCheckBox.Size    = new Size(14, 14);
                                CellCheckBox.Checked = (bool)Cel.Value;
                                Bitmap   bmp         = new Bitmap((int)ColumnWidths[i], CellHeight);
                                Graphics tmpGraphics = Graphics.FromImage(bmp);
                                tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                                                                       bmp.Width, bmp.Height));
                                CellCheckBox.DrawToBitmap(bmp,
                                                          new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                                                        (int)((bmp.Height - CellCheckBox.Height) / 2),
                                                                        CellCheckBox.Width, CellCheckBox.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                            }
                            //为 ComboBox列
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                            {
                                CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                                                             bmp.Width, bmp.Height));
                                e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                                      new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                                                     - 16, CellHeight), StrFormatComboBox);
                            }
                            // 为 Image列
                            else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                            {
                                Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                                                  tmpTop, (int)ColumnWidths[i], CellHeight);
                                Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                   tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                   ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                            }


                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                                                               tmpTop, (int)ColumnWidths[i], CellHeight));

                            i++;
                        }
                        tmpTop += CellHeight;
                    }

                    RowPos++;
                    // 对于它计算的第一页每页行
                    if (PageNo == 1)
                    {
                        RowsPerPage++;
                    }
                }

                if (RowsPerPage == 0)
                {
                    return;
                }

                // 写页脚(页码)
                DrawFooter(e, RowsPerPage);

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }