⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmprint.cs

📁 实现课程表编排和打印功能,通过在候选列表中选择课程和教师(没有被排课且该教师教授所选择的课程)来完成排课,代码约8000行
💻 CS
📖 第 1 页 / 共 3 页
字号:
                        }
                    }
                    else if (pi.Select == PrintInfo.PrintSelect.全年级)
                    {
                        Grade grade = School.GetGrade(pi.SelectInfo);
                        if (grade != null)
                        {
                            if (grade.Classes != null)
                            {
                                for (int i = 0; i < grade.Classes.Count; i++)
                                {
                                    Class cls = grade.Classes[i];
                                    if (cls.CourseTable != null)
                                    {
                                        PrintContent pc = new PrintContent();
                                        pc.ClassName = "班级: " + cls.ClassName;
                                        pc.TeacherName = "";
                                        pc.Term = School.Term;      
                                        pc.CourseTableType = CourseTableType.班级;
                                        pc.CourseTable = GetPrintTableContent(cls.CourseTable, pi.Format);
                                        this.printContents.Add(pc);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Class cls = School.GetClass(pi.SelectInfo);
                        if (cls != null)
                        {
                            if (cls.CourseTable != null)
                            {
                                PrintContent pc = new PrintContent();
                                pc.ClassName = "班级: " + cls.ClassName;
                                pc.TeacherName = "";
                                pc.Term = School.Term;      
                                pc.CourseTableType = CourseTableType.班级;
                                pc.CourseTable = GetPrintTableContent(cls.CourseTable, pi.Format);
                                this.printContents.Add(pc);
                            }
                        }
                    }
                }
            }
        }

        private DataTable GetPrintTableContent(CourseTable ct, PrintInfo.PrintFormat printFormat)
        {
            DataTable dt = null;
            if (ct.Courses != null)
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("column1", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第一节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第二节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第三节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第四节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第五节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第六节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第七节", typeof(System.String)));

                for (int i = 1; i <= 7; i++)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    string week = Utility.IntToWeek(i).ToString();
                    dr[0] = week;
                    dr[1] = "";
                    dr[2] = "";
                    dr[3] = "";
                    dr[4] = "";
                    dr[5] = "";
                    dr[6] = "";
                }


                for (int i = 0; i < ct.Courses.Count; i++)
                {
                    Course course = ct.Courses[i];
                    int rowIndex = Utility.WeekToInt(course.CourseTime.Week) - 1;
                    int colIndex = course.CourseTime.CourseNum;
                    DataRow dr = dt.Rows[rowIndex];
                    if (printFormat == PrintInfo.PrintFormat.班级角度)
                        dr[colIndex] = course.Subject.SubjectName;
                    else
                        dr[colIndex] = course.Class.ClassName + course.Subject.SubjectName;
                    dr[0] = Utility.WeekToString(course.CourseTime.Week);
                }
            }
            return dt;
        }
        #endregion


        #region  打印部分
        void printDocument1_BeginPrint(object sender, PrintEventArgs e)
        {
            InitPrintInfo(this.printDocument1);
            printIndex = 0;
        }

        private int printIndex = 0;
        void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (printIndex >= this.printContents.Count) return;
            DrawTable(e.Graphics, this.printContents[printIndex]);
            if (this.printContents.Count == 1)
                e.HasMorePages = false;
            else
                e.HasMorePages = true;
            printIndex++;
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.GetPrintInfo())
            {
                this.CreatePrintContents(this.printInformation);
                this.printDocument1.Print();
            }
        }

        private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.PageSettings = this.printDocument1.DefaultPageSettings;
            this.pageSetupDialog1.ShowDialog();
        }

        private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.printDialog1.PrinterSettings = this.printDocument1.PrinterSettings;
            this.printDialog1.ShowDialog();
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.GetPrintInfo())
            {
                this.CreatePrintContents(this.printInformation);
                this.printPreviewDialog1.Document = this.printDocument1;
                this.printPreviewDialog1.ShowDialog();
            }
        }

        #region 绘制打印界面
        private readonly int rectHeight = 600;
        private readonly int rectWidth = 1000;
        private readonly int titlePercent = 10;

        private Rectangle pageRect;
        private Rectangle rect;
        private Rectangle rectTitle;
        private Rectangle rectBody;

        private int cellWidth;
        private int cellHeight;

        private Pen pen;
        private Brush brush;
        private Font fontTitle;
        private Font fontBody;

        private void InitPrintInfo(PrintDocument pd)
        {
            //pageRect = pd.DefaultPageSettings.Bounds;
            //pageRect = new Rectangle();
            //pageRect.Location = new Point(Convert.ToInt32(pd.DefaultPageSettings.PrintableArea.Left), Convert.ToInt32(pd.DefaultPageSettings.PrintableArea.Top));
            //pageRect.Size = new Size(Convert.ToInt32(pd.DefaultPageSettings.PrintableArea.Width), Convert.ToInt32(pd.DefaultPageSettings.PrintableArea.Height));

            pageRect = new Rectangle();
            pageRect.Location = pd.DefaultPageSettings.Bounds.Location;
            pageRect.Size = pd.DefaultPageSettings.Bounds.Size;
            pageRect.Location = new Point(pageRect.Location.X + pd.DefaultPageSettings.Margins.Left, pageRect.Location.Y + pd.DefaultPageSettings.Margins.Top);
            pageRect.Width = pageRect.Width - pd.DefaultPageSettings.Margins.Left * 2;
            pageRect.Height = pageRect.Height - pd.DefaultPageSettings.Margins.Top * 2;

            rect = new Rectangle();
            rect.Width = rectWidth;
            rect.Height = rectHeight;
            rect.Location = GetCenterTopAlign(pageRect.Location, pageRect.Size, rect.Size);

            rectTitle = new Rectangle();
            rectTitle.Location = rect.Location;
            rectTitle.Width = rectWidth;
            rectTitle.Height = rectHeight * titlePercent / 100;

            rectBody = new Rectangle();
            rectBody.Location = new Point(rect.Left, rectTitle.Top + rectTitle.Height);
            rectBody.Width = rectWidth;
            rectBody.Height = rect.Height - rectTitle.Height;

            pen = new Pen(Color.Gray);
            brush = Brushes.Gray;
            fontTitle = new Font("宋体", 32);
            fontBody = new Font("宋体", 14);
        }

        private void LoadExampleData()
        {
            this.printContents = new List<PrintContent>();
            for (int number = 0; number < 3; number++)
            {
                PrintContent pc = new PrintContent();
                pc.ClassName = "班级: " + number.ToString() + "班";
                pc.TeacherName = "";
                pc.Term = "2007-2008年度下学期";

                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("column1", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第一节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第二节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第三节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第四节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第五节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第六节", typeof(System.String)));
                dt.Columns.Add(new DataColumn("第七节", typeof(System.String)));
                for (int i = 1; i <= 6; i++)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    dr[0] = "星期" + i.ToString();
                    dr[1] = "语文";
                    dr[2] = "数学";
                    dr[3] = "自然";
                    dr[4] = "地理";
                    dr[5] = "体育";
                    dr[6] = "音乐";
                    dr[7] = "美术";
                }

                pc.CourseTable = dt;

                this.printContents.Add(pc);
            }
        }

        private void DrawTable(Graphics g)
        {
            if (this.printContents != null)
            {
                for (int i = 0; i < this.printContents.Count; i++)
                {
                    if (i != 0)
                    {
                        this.rect.Location = new Point(rect.Left, rect.Top + pageRect.Height);
                        this.rectTitle.Location = new Point(rectTitle.Left, rectTitle.Top + pageRect.Height);
                        this.rectBody.Location = new Point(rectBody.Left, rectBody.Top + pageRect.Height);
                    }
                    DrawTable(g, this.printContents[i]);
                }
            }
        }

        private void DrawTable(Graphics g, PrintContent pc)
        {
            Point p = Point.Empty;
            System.Drawing.Size s = System.Drawing.Size.Empty;

            // 计算单元格大小
            int rows = pc.CourseTable.Rows.Count + 2;
            int cols = pc.CourseTable.Columns.Count;
            cellWidth = rectBody.Width / cols;
            cellHeight = rectBody.Height / rows;

            //  绘制标题"课程表"
            s = GetTextSize(fontTitle, "课 程 表", g);
            p = GetTextLocation(rectTitle.Location, rectTitle.Size, s, ContentAlignment.MiddleCenter);
            g.DrawString("课 程 表", fontTitle, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));

            //  绘制名称和学期
            string name = "";
            if (pc.CourseTableType == CourseTableType.班级)
                name = pc.ClassName;
            else
                name = pc.TeacherName;
            if (pc.NameAlign == pc.TermAlign)
            {
                name = name + pc.Term;
                s = GetTextSize(fontBody, name, g);
                p = GetTextLocation(rectBody.Location, new Size(rectBody.Width, cellHeight), s, pc.NameAlign);
                g.DrawString(name, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
            }
            else
            {
                s = GetTextSize(fontBody, name, g);
                p = GetTextLocation(rectBody.Location, new Size(rectBody.Width, cellHeight), s, pc.NameAlign);
                g.DrawString(name, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));

                s = GetTextSize(fontBody, pc.Term, g);
                p = GetTextLocation(rectBody.Location, new Size(rectBody.Width, cellHeight), s, pc.TermAlign);
                g.DrawString(pc.Term, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
            }


            //  绘制表格
            p = new Point(rectBody.Left, rectBody.Top + cellHeight);
            g.DrawLine(pen, p, new Point(p.X + cellWidth, p.Y + cellHeight * 2));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -