📄 frmprint.cs
字号:
for (int i = 0; i <= rows; i++)
{
if (i == 1)
g.DrawLine(pen, new Point(p.X + cellWidth, p.Y), new Point(p.X + cellWidth * cols, p.Y));
else
g.DrawLine(pen, p, new Point(p.X + cols * cellWidth, p.Y));
p.Y = p.Y + cellHeight;
}
p = new Point(rectBody.Left, rectBody.Top + cellHeight);
for (int i = 0; i <= cols; i++)
{
if (i == 0 || i == 1 || i == 5 || i == cols)
{
g.DrawLine(pen, p, new Point(p.X, p.Y + cellHeight * rows));
}
else
{
g.DrawLine(pen, new Point(p.X, p.Y + cellHeight), new Point(p.X, p.Y + cellHeight * rows));
}
p.X = p.X + cellWidth;
}
// 绘制“上午”和“下午”
s = GetTextSize(fontBody, "上 午", g);
Point tempP1 = new Point(rectBody.Left + cellWidth, rectBody.Top + cellHeight);
System.Drawing.Size tempS1 = new Size(cellWidth * 4, cellHeight);
p = GetTextLocation(tempP1, tempS1, s, ContentAlignment.MiddleCenter);
g.DrawString("上 午", fontBody, brush, p);
s = GetTextSize(fontBody, "下 午", g);
Point tempP2 = new Point(rectBody.Left + cellWidth * 5, rectBody.Top + cellHeight);
System.Drawing.Size tempS2 = new Size(cellWidth * 3, cellHeight);
p = GetTextLocation(tempP2, tempS2, s, ContentAlignment.MiddleCenter);
g.DrawString("下 午", fontBody, brush, p);
// 绘制表格中的数据
Point topLeftp = new Point(rectBody.Left + cellWidth, rectBody.Top + cellHeight * 2);
for (int i = 0; i < pc.CourseTable.Columns.Count; i++)
{
// 绘制第一行,列名称
if (i != 0)
{
string colName = pc.CourseTable.Columns[i].ToString();
s = GetTextSize(fontBody, colName, g);
p = GetTextLocation(topLeftp, new Size(cellWidth, cellHeight), s, pc.TableCellAlign);
g.DrawString(colName, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
topLeftp.X = topLeftp.X + cellWidth;
}
}
// 绘制星期
topLeftp = new Point(rectBody.Left, rectBody.Top + cellHeight * 3);
for (int i = 1; i <= 7; i++)
{
string textStr = Utility.IntToWeek(i).ToString();
s = GetTextSize(fontBody, textStr, g);
p = GetTextLocation(topLeftp, new Size(cellWidth, cellHeight), s, pc.TableCellAlign);
g.DrawString(textStr, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
topLeftp.Y = topLeftp.Y + cellHeight;
}
topLeftp = new Point(rectBody.Left, rectBody.Top + cellHeight * 3);
for (int i = 0; i < pc.CourseTable.Rows.Count; i++)
{
for (int j = 0; j < pc.CourseTable.Columns.Count; j++)
{
if (j != 0) // 不绘制星期
{
string textStr = pc.CourseTable.Rows[i][j].ToString();
s = GetTextSize(fontBody, textStr, g);
p = GetTextLocation(topLeftp, new Size(cellWidth, cellHeight), s, pc.TableCellAlign);
g.DrawString(textStr, fontBody, brush, new PointF(Convert.ToSingle(p.X), Convert.ToSingle(p.Y)));
}
topLeftp.X = topLeftp.X + cellWidth;
}
topLeftp.X = rectBody.Left;
topLeftp.Y = topLeftp.Y + cellHeight;
}
// 设置下一页的范围
//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);
}
#region 获取文字绘制位置
private Size GetTextSize(Font font, string text, Graphics g)
{
SizeF sf = g.MeasureString(text, font);
System.Drawing.Size size = new Size(Convert.ToInt32(sf.Width), Convert.ToInt32(sf.Height));
return size;
}
private Point GetTextLocation(Point location, Size containerSize, Size contentSize, ContentAlignment align)
{
Point p = Point.Empty;
switch (align)
{
case ContentAlignment.TopLeft:
p = GetLeftTopAlign(location, containerSize, contentSize);
break;
case ContentAlignment.TopCenter:
p = GetCenterTopAlign(location, containerSize, contentSize);
break;
case ContentAlignment.TopRight:
p = GetRightTopAlign(location, containerSize, contentSize);
break;
case ContentAlignment.MiddleLeft:
p = GetLeftMiddleAlign(location, containerSize, contentSize);
break;
case ContentAlignment.MiddleCenter:
p = GetCenterMiddleAlign(location, containerSize, contentSize);
break;
case ContentAlignment.MiddleRight:
p = GetRightMiddleAlign(location, containerSize, contentSize);
break;
case ContentAlignment.BottomLeft:
p = GetLeftBottomAlign(location, containerSize, contentSize);
break;
case ContentAlignment.BottomCenter:
p = GetCenterBottomAlign(location, containerSize, contentSize);
break;
case ContentAlignment.BottomRight:
p = GetRightBottomAlign(location, containerSize, contentSize);
break;
}
return p;
}
private Point GetLeftTopAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X;
p.Y = location.Y;
return p;
}
private Point GetLeftMiddleAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X;
p.Y = location.Y + ly / 2;
return p;
}
private Point GetLeftBottomAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X;
p.Y = location.Y + ly;
return p;
}
private Point GetCenterTopAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx / 2;
p.Y = location.Y;
return p;
}
private Point GetCenterMiddleAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx / 2;
p.Y = location.Y + ly / 2;
return p;
}
private Point GetCenterBottomAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx / 2;
p.Y = location.Y + ly;
return p;
}
private Point GetRightTopAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx;
p.Y = location.Y;
return p;
}
private Point GetRightMiddleAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx;
p.Y = location.Y + ly / 2;
return p;
}
private Point GetRightBottomAlign(Point location, Size containerSize, Size contentSize)
{
Point p = Point.Empty;
int lx = containerSize.Width - contentSize.Width;
int ly = containerSize.Height - contentSize.Height;
p.X = location.X + lx;
p.Y = location.Y + ly;
return p;
}
#endregion
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#endregion
}
public class PrintContent
{
private DataTable courseTable = null;
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 string term = "";
public string Term
{
get { return term; }
set { term = value; }
}
private ContentAlignment nameAlign = ContentAlignment.MiddleLeft;
public ContentAlignment NameAlign
{
get { return nameAlign; }
set { nameAlign = value; }
}
private ContentAlignment termAlign = ContentAlignment.MiddleRight;
public ContentAlignment TermAlign
{
get { return termAlign; }
set { termAlign = value; }
}
private ContentAlignment tableCellAlign = ContentAlignment.MiddleCenter;
public ContentAlignment TableCellAlign
{
get { return tableCellAlign; }
set { tableCellAlign = value; }
}
private CourseTableType courseTableType;
public CourseTableType CourseTableType
{
get { return courseTableType; }
set { courseTableType = value; }
}
}
public enum CourseTableType { 班级, 教师 }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -