Exemple #1
0
        private static void DrawFooter(PrintGridItem pg, PrintPageEventArgs e)
        {
            double cnt = 0;

            if (pg.Grid.Rows[pg.Grid.Rows.Count - 1].IsNewRow)
            {
                cnt = pg.Grid.Rows.Count - 1;
            }
            else
            {
                cnt = pg.Grid.Rows.Count;
            }

            string PageNum = "第 " + pg.PageNo.ToString() + " 页/共 " + Math.Ceiling((double)(cnt / pg.RowsPerPage)).ToString() + " 页";

            e.Graphics.DrawString(PageNum, pg.Grid.Font, Brushes.Black,
                                  e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(PageNum, pg.Grid.Font, e.MarginBounds.Width).Width) / 2,
                                  e.MarginBounds.Top + e.MarginBounds.Height + 31);
        }
Exemple #2
0
        public static void PrintBankCash(PrintGridItem pg, PrintPageEventArgs e)
        {
            if (pg == null)
            {
                return;
            }

            int  CellHeight;
            int  NowTop        = e.MarginBounds.Top;
            Font headerfont    = new Font("宋体", 16.0F, FontStyle.Bold, GraphicsUnit.Point);
            int  TitleHeight   = (int)e.Graphics.MeasureString(pg.HeaderPrinter, headerfont, e.MarginBounds.Width).Height;
            int  TitleWidth    = (int)e.Graphics.MeasureString(pg.HeaderPrinter, headerfont, e.MarginBounds.Width).Width;
            int  PrinterHeight = (int)e.Graphics.MeasureString(pg.HeaderPrinter, new Font(pg.Grid.Font, FontStyle.Bold), e.MarginBounds.Width).Height;
            int  i             = 0;

            try
            {
                while (pg.RowPos <= pg.Grid.Rows.Count - 1)
                {
                    DataGridViewRow GridRow = pg.Grid.Rows[pg.RowPos];

                    CellHeight = GridRow.Height;

                    if (NowTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        DrawFooter(pg, e);
                        pg.NewPage = true;
                        pg.PageNo++;

                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        if (pg.NewPage)
                        {
                            // Draw Header
                            e.Graphics.DrawString(pg.HeaderTitle, headerfont, Brushes.Black,
                                                  (int)(e.MarginBounds.Left + e.MarginBounds.Width - TitleWidth) / 2,
                                                  e.MarginBounds.Top - TitleHeight - PrinterHeight - 26);

                            e.Graphics.DrawString(pg.HeaderPrinter, new Font(pg.Grid.Font, FontStyle.Bold), Brushes.Black,
                                                  e.MarginBounds.Left + 5,
                                                  e.MarginBounds.Top - PrinterHeight - 13);

                            String HeaderDate = "打印日期:" + DateTime.Now.ToString("yyyy年MM月dd日 hh:mm");

                            e.Graphics.DrawString(HeaderDate, new Font(pg.Grid.Font, FontStyle.Bold), Brushes.Black,
                                                  e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(HeaderDate, new Font(pg.Grid.Font, FontStyle.Bold), e.MarginBounds.Width).Width - 5),
                                                  e.MarginBounds.Top - PrinterHeight - 13);

                            // Draw Columns
                            NowTop = e.MarginBounds.Top;

                            i = 0;

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

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

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

                                i++;
                            }

                            pg.NewPage = false;
                            NowTop    += pg.HeaderHeight;
                        }

                        // Draw Columns Contents
                        i = 0;

                        StringFormat sf = new StringFormat();
                        sf.Alignment     = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Center;
                        sf.Trimming      = StringTrimming.None;

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

                            switch (((Type)pg.ColumnTypes[i]).Name)
                            {
                            case "DataGridViewCheckBoxColumn":
                                e.Graphics.DrawString((bool)Cel.Value ? "√" : "×", Cel.InheritedStyle.Font, new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)pg.ColumnLefts[i] + 3, (float)NowTop, (int)pg.ColumnWidths[i] - 3, (float)CellHeight), sf);
                                break;

                            case "DataGridViewImageColumn":
                                Rectangle CelSize = new Rectangle((int)pg.ColumnLefts[i], NowTop, (int)pg.ColumnWidths[i], CellHeight);
                                Size      ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                                     new Rectangle((int)pg.ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                                                   NowTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                                                   ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
                                break;

                            default:
                                e.Graphics.DrawString(Cel.Value != null ? Cel.Value.ToString() : String.Empty, Cel.InheritedStyle.Font, new SolidBrush(Cel.InheritedStyle.ForeColor),
                                                      new RectangleF((int)pg.ColumnLefts[i] + 3, (float)NowTop, (int)pg.ColumnWidths[i] - 3, (float)CellHeight), sf);
                                break;
                            }

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

                            i++;
                        }

                        NowTop += CellHeight;
                    }

                    pg.RowPos++;

                    if (pg.PageNo == 1)
                    {
                        pg.RowsPerPage++;
                    }
                }

                if (pg.RowsPerPage == 0)
                {
                    return;
                }

                DrawFooterBank(pg, e);

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