コード例 #1
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);
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取复选框的图像
        /// </summary>
        /// <param name="isChecked">true为选中的样式,false为未选中的样式</param>
        /// <returns></returns>
        private Image getCheckBoxImage(bool isChecked)
        {
            if (isChecked && this.checkedCheckBoxImage != null)
            {
                return checkedCheckBoxImage;
            }
            else if (!isChecked && this.uncheckedCheckBoxImage != null)
            {
                return uncheckedCheckBoxImage;
            }

            CheckBox chk = new CheckBox();
            chk.Checked = isChecked;
            chk.Text = "";
            chk.Margin = new Padding(3, 3, 3, 3);
            chk.Width = 14;
            chk.Height = 14;
            chk.BackColor = Color.Transparent;

            Bitmap bitmap = new Bitmap(chk.Width, chk.Height);

            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            chk.DrawToBitmap(bitmap, rect);

            if (isChecked)
            {
                checkedCheckBoxImage = bitmap;
            }
            else
            {
                uncheckedCheckBoxImage = bitmap;
            }

            return bitmap;
        }