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

📄 drawprinttable.cs

📁 实现课程表编排和打印功能,通过在候选列表中选择课程和教师(没有被排课且该教师教授所选择的课程)来完成排课,代码约8000行
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace 课程安排
{
    public class DrawPrintTable
    {
        private DataTable courseTable;
        public DataTable CourseTable
        {
            get { return courseTable; }
            set { courseTable = value; }
        }

        private string className;
        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }

        private string teacherName;
        public string TeacherName
        {
            get { return teacherName; }
            set { teacherName = value; }
        }

        private bool isClassCourseTable;
        public bool IsClassCourseTable
        {
            get { return isClassCourseTable; }
            set { isClassCourseTable = value; }
        }

        private string term;
        public string Term
        {
            get { return term; }
            set { term = value; }
        }

        private Graphics graphics;
        public DrawPrintTable( Graphics g)
        {
            graphics = g;
            InitPrintInfo();
        }

        public void DrawCourseTable()
        {
            if (this.graphics == null)
                return;
            if (this.courseTable != null)
            {
                DrawTitle(graphics);
                DrawTable(graphics);
            }
            else
            {

            }
        }
        public void DrawCourseTable(DataTable courseTable, string name, string term, bool isClassTable)
        {
            this.courseTable = courseTable;
            this.className = name;
            this.term = term;
            this.isClassCourseTable = isClassTable;

            if (this.graphics == null)
                return;
            if (this.courseTable != null)
            {
                DrawTitle(graphics);
                DrawTable(graphics);
            }
            else
            {

            }
        }

        #region  函数部分
        private const int topDistance = 100;  // 到顶端距离
        private const int leftDistance = 100; // 到左边的距离

        private readonly ContentAlignment nameAlign = ContentAlignment.MiddleLeft;      // 班级(教师)名称的位置
        private readonly ContentAlignment termAlign = ContentAlignment.MiddleRight;     // 学期说明的位置

        private readonly int cellWidth = 40;   // 单元格宽度
        private readonly int cellHeight = 24;  // 单元格高度

        private readonly int fontWidth = 8;    // 字体宽度
        private readonly int fontHeight = 8;   // 字体高度

        private int width;    // 内容部分的宽度(即为表格宽度)

        private int titleLeft;
        private int titleTop;
        private int titleHeight = 25;

        private int courseTableLeft;
        private int courseTableTop;

        private Pen pen;
        private Brush brush;
        private Font font;
        
        //  初始化绘制信息
        private void InitPrintInfo()
        {
            pen = new Pen(Color.Gray);
            brush = Brushes.White;
            Label l = new Label();
            font = l.Font;
        }

        //  计算表格宽度
        private int GetWidth()
        {
            return (cellWidth * courseTable.Columns.Count);
        }

        // 计算
        private Point GetCenterLocation(Point location, int stringLength)
        {
            Point p = new Point();
            p.X = width / 2 - (stringLength / 2) * fontWidth + location.X;
            p.Y = titleHeight / 2 - fontHeight / 2 + location.Y;
            return p;
        }
        private Point GetLeftLocation(Point location, int stringLength)
        {
            Point p = new Point();
            p.X = location.X;
            p.Y = titleHeight / 2 - fontHeight / 2 + location.Y;
            return p;
        }
        private Point GetRightLocation(Point location, int stringLength)
        {
            Point p = new Point();
            p.X = width + location.X;
            p.Y = titleHeight / 2 - fontHeight / 2 + location.Y;
            return p;
        }
        private Point GetCellCenterLocation(Point location, int stringLength)
        {
            Point p = new Point();
            p.X = cellWidth / 2 - (stringLength / 2) * fontWidth + location.X;
            p.Y = cellHeight / 2 - fontHeight / 2 + location.Y;
            return p;
        }
        


        private void DrawTitle(Graphics g)
        {
            string name = "";
            if (this.isClassCourseTable)
            {
                name = "班级:  ";
                if (this.className != null && this.className != "")
                    name = name + this.className;
            }
            else
            {
                name = "教师:";
                if (this.teacherName != null && this.teacherName != "")
                    name = name + this.teacherName;
            }
            if (this.nameAlign == this.termAlign)
            {
                name = name + this.term;
                Point p = Point.Empty; ;
                if (this.nameAlign == ContentAlignment.MiddleCenter)
                    p = GetCenterLocation(new Point(leftDistance, topDistance), name.Length);
                else if (this.nameAlign == ContentAlignment.MiddleLeft)
                    p = GetLeftLocation(new Point(leftDistance, topDistance), name.Length);
                else if (this.nameAlign == ContentAlignment.BottomRight)
                    p = GetRightLocation(new Point(leftDistance, topDistance), name.Length);
                g.DrawString(name, font, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
            }
            else
            {
                Point p = Point.Empty; ;
                if (this.nameAlign == ContentAlignment.MiddleCenter)
                    p = GetCenterLocation(new Point(leftDistance, topDistance), name.Length);
                else if (this.nameAlign == ContentAlignment.MiddleLeft)
                    p = GetLeftLocation(new Point(leftDistance, topDistance), name.Length);
                else if (this.nameAlign == ContentAlignment.BottomRight)
                    p = GetRightLocation(new Point(leftDistance, topDistance), name.Length);
                g.DrawString(name, font, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));

                if (this.nameAlign == ContentAlignment.MiddleCenter)
                    p = GetCenterLocation(new Point(leftDistance, topDistance), term.Length);
                else if (this.nameAlign == ContentAlignment.MiddleLeft)
                    p = GetLeftLocation(new Point(leftDistance, topDistance), term.Length);
                else if (this.nameAlign == ContentAlignment.BottomRight)
                    p = GetRightLocation(new Point(leftDistance, topDistance), term.Length);
                g.DrawString(term, font, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
            }

            titleLeft = leftDistance;
            titleTop = topDistance;
        }

        private void DrawTable(Graphics g)
        {
            int lineWidth = cellWidth * courseTable.Columns.Count;
            int lineHeight = cellHeight * courseTable.Rows.Count;
            int x = titleLeft;
            int y = titleTop + titleHeight + 5;
            courseTableLeft = x;
            courseTableTop = y;


            g.DrawLine(pen, x, y, x + cellWidth, y + cellHeight);
            for (int i = 0; i < courseTable.Rows.Count; i++)
            {
                // 绘制横线
                y = y + i * cellHeight;
                g.DrawLine(pen, x, y, x + lineWidth, y);
                for (int j = 0; j < courseTable.Columns.Count; j++)
                {
                    //  绘制内容
                    string text = courseTable.Rows[i][j].ToString();
                    Point p = new Point(x * j, y);
                    p = GetCellCenterLocation(p, text.Length);
                    g.DrawString(text, font, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
                }
            }

            x = courseTableLeft;
            y = courseTableTop;
            for (int i = 0; i < courseTable.Columns.Count; i++)
            {
                //  绘制纵线
                x = x + i * cellWidth;
                g.DrawLine(pen, x, y, x, y + lineHeight);
            }
        }
        #endregion
    }
}

⌨️ 快捷键说明

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